Skip to content

BUG: AttributeError: 'NoneType' object has no attribute 'is_first_col' #53553

Closed
@set-leanix

Description

@set-leanix

Pandas version checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of pandas.
  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Random data
df_1 = pd.DataFrame(np.random.randint(0,100,size=(1000, 1)), columns=list('v'))
df_2 = pd.DataFrame(np.random.randint(0,100,size=(1000, 1)), columns=list('v'))

# Create a basic figure
fig = plt.figure()

# Add first axis
ax_1 = fig.add_axes([
    0.0, # left
    0.0, # bottom
    0.5, # width
    1.0  # height
])
df_1.plot(ax=ax_1)

# Add second axis
ax_2 = fig.add_axes([0.5, 0.0, 0.5, 1.0])
df_2.plot(ax=ax_2)

Issue Description

Consider the minimal code example using pandas (2.0.2) along matplotlib (3.7.1). numpy is not relevant, as it's only used to generate random data. When executing the code the following error appears:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[8], line 23
     21 # Add second axis
     22 ax_2 = fig.add_axes([0.5, 0.0, 0.5, 1.0])
---> 23 df_2.plot(legend=False, ax=ax_2)

File .venv/lib/python3.10/site-packages/pandas/plotting/_core.py:1000, in PlotAccessor.__call__(self, *args, **kwargs)
    997             label_name = label_kw or data.columns
    998             data.columns = label_name
-> 1000 return plot_backend.plot(data, kind=kind, **kwargs)

File .venv/lib/python3.10/site-packages/pandas/plotting/_matplotlib/__init__.py:71, in plot(data, kind, **kwargs)
     69         kwargs["ax"] = getattr(ax, "left_ax", ax)
     70 plot_obj = PLOT_CLASSES[kind](data, **kwargs)
---> 71 plot_obj.generate()
     72 plot_obj.draw()
     73 return plot_obj.result

File .venv/lib/python3.10/site-packages/pandas/plotting/_matplotlib/core.py:455, in MPLPlot.generate(self)
    453 self._add_table()
    454 self._make_legend()
--> 455 self._adorn_subplots()
    457 for ax in self.axes:
    458     self._post_plot_logic_common(ax, self.data)
...
--> 400     is_first_col = lambda x: x.get_subplotspec().is_first_col()
    401 else:
    402     is_first_col = lambda x: x.is_first_col()

AttributeError: 'NoneType' object has no attribute 'is_first_col'

However if I pin the matplotlib version to <3.7 it works correctly and my plot is shown.

Expected Behavior

Plot is shown properly regardless of the underlying matplotlib version.

Installed Versions

INSTALLED VERSIONS

commit : 965ceca
python : 3.10.8.final.0
python-bits : 64
OS : Darwin
OS-release : 22.5.0
Version : Darwin Kernel Version 22.5.0: Mon Apr 24 20:52:24 PDT 2023; root:xnu-8796.121.2~5/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 2.0.2
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.8.0
pip : 22.3
Cython : None
pytest : 7.3.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.2
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCompatpandas objects compatability with Numpy or Python functionsVisualizationplotting

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions