Skip to content

BUG: Exception on drawing on multiple axes created by add_axes #54577

@Hagai

Description

@Hagai

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 matplotlib.pyplot as plt

fig = plt.Figure()
ax = fig.add_axes((0, 0, .5, 1))
ax = fig.add_axes((.5, 0, .5, 1))
df = pd.DataFrame({'col1': [1,2], 'col2':[3,4]})
df.plot(ax=ax)

Issue Description

Instead of creating a plot on the provided axes the code crash with the exception:

/usr/local/lib/python3.9/site-packages/pandas/plotting/_matplotlib/tools.py in (x)
398
399 if compat.mpl_ge_3_4_0():
--> 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'

It seems that pandas expect get_subplotspec to return SubplotSpec but it got None, which is a valid return value based on the function documentation

Expected Behavior

The plot should be drawn on the axes

Also, None value should be handled correctly every location where get_subplotspec is called.

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.9.13.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-213-generic
Version : #224-Ubuntu SMP Mon Jun 19 13:30:12 UTC 2023
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.24.4
pytz : 2022.5
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.2.1
Cython : None
pytest : 7.1.3
hypothesis : None
sphinx : 4.3.0
blosc : None
feather : None
xlsxwriter : 3.0.3
lxml.etree : 4.9.1
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.1.2
IPython : 7.29.0
pandas_datareader: None
bs4 : None
bottleneck : 1.3.5
brotli : 1.0.9
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.2
numba : 0.57.1
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.1
snappy : None
sqlalchemy : 2.0.19
tables : None
tabulate : 0.9.0
xarray : 2023.7.0
xlrd : 2.0.1
xlwt : None
zstandard : None
tzdata : 2023.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions