Skip to content

BUG: Inconsistent plotting behaviour. #59374

@maddytae

Description

@maddytae

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 matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

mpg = sns.load_dataset("mpg")
tips = sns.load_dataset("tips")



# Plot 1: mpg data
fig, ax = plt.subplots(figsize=(3,3))
ax2 = ax.twinx()

k = mpg.pivot_table(index='model_year', columns='cylinders', values='mpg', aggfunc='sum', dropna=True)
k.plot(ax=ax, kind='bar')

k = k.fillna(10) 
k.plot(ax=ax2, kind='line')

plt.show() 

# Plot 2: tips data
fig, ax = plt.subplots(figsize=(3,3))
ax2 = ax.twinx()

k = tips.pivot_table(index='size', columns='smoker', values='total_bill', aggfunc='sum', dropna=True)
k.plot(ax=ax, kind='bar')

k = k.fillna(10)  
k.plot(ax=ax2, kind='line')

plt.show()

Issue Description

I am trying to plot bar chart in primary axis and line chart in secondary axis. The plotting works fine for tips dataset but not for mpg dataset.
Screenshot 2024-07-31 at 2 22 12 PM

Expected Behavior

I should see both bar chart and line chart irrespective of underlying data source.

Installed Versions

INSTALLED VERSIONS

commit : a671b5a
python : 3.9.6.final.0
python-bits : 64
OS : Darwin
OS-release : 23.5.0
Version : Darwin Kernel Version 23.5.0: Wed May 1 20:19:05 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8112
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_CA.UTF-8
LOCALE : en_CA.UTF-8

pandas : 2.1.4
numpy : 1.26.3
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 58.0.4
pip : 21.2.4
Cython : None
pytest : 8.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.1.1
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.18.1
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 15.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.4
qtpy : 2.4.1
pyqt5 : None

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