Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
set-leanix opened this issue Jun 7, 2023 · 4 comments
Closed
3 tasks done
Labels
Bug Compat pandas objects compatability with Numpy or Python functions Visualization plotting
Milestone

Comments

@set-leanix
Copy link

set-leanix commented Jun 7, 2023

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

@set-leanix set-leanix added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 7, 2023
@topper-123
Copy link
Contributor

I tried this on both v2.0.1 and main dev branch using matplotliv v7.1.2 and it failed as described on v2.0.1 but works for me on the dev branch.

This has been fixed probably in ##52150. This should be back-ported to v2.0.3, @datapythonista?

@topper-123 topper-123 added Visualization plotting Compat pandas objects compatability with Numpy or Python functions Still Needs Manual Backport and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 7, 2023
@topper-123 topper-123 added this to the 2.0.3 milestone Jun 7, 2023
@lithomas1
Copy link
Member

Seems reasonable to me. I've backported that PR.

@lithomas1
Copy link
Member

Closing for now.

I'm planning on releasing 2.0.3 this week (probably Wednesday). Let me know if there's still an issue after that, and I can reopen.

@set-leanix
Copy link
Author

Works as expected, @lithomas1. Thanks for back porting. 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Compat pandas objects compatability with Numpy or Python functions Visualization plotting
Projects
None yet
Development

No branches or pull requests

3 participants