Skip to content

BUG: fails to plot 2 plots with secondary y axis when index type is PeriodIndex and plot kinds are different #61161

Closed
@Ev2geny

Description

@Ev2geny

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

print(pd.__version__)

index = pd.period_range('2023', periods=3, freq='Y')
df = pd.DataFrame({
    'col1': [10, 20, 30],
    'col2': [40, 25, 10]
}, index=index)

# Workaround !!!!
# df.index = df.index.astype(str)

print(type(df.index))

fig, ax = plt.subplots()

df['col1'].plot(kind='bar', ax=ax)

ax2 = ax.twinx()

df['col2'].plot(kind='line', ax=ax2, color = 'r')

plt.show()

Issue Description

I seem to have found the following bug:

It is not possible to plot 2 plots with different Y axis when the following 2 conditions are met:

  • plot kinds are different (e.g. 'bar' and 'line')
  • index type is PeriodIndex

Image

The easy workaround is to convert index to string:

Image

If the parameter 'kind' of both axis is identical, then it works as well without workound

Image

Expected Behavior

Both plots shall be possible to be seen

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.12.7
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 170 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Netherlands.1252

pandas : 2.2.3
numpy : 2.2.1
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : 8.31.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2024.10.0
html5lib : 1.1
hypothesis : None
gcsfs : None
jinja2 : 3.1.4
lxml.etree : 5.3.0
matplotlib : 3.10.0
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.3
python-calamine : None
pyxlsb : 1.0.10
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions