Skip to content

BUG: Subplot data is lost when sharex=True and latter DatetimeIndex has higher frequency #55110

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

Open
2 of 3 tasks
jluttine opened this issue Sep 12, 2023 · 6 comments
Open
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member Visualization plotting

Comments

@jluttine
Copy link

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 numpy as np
import pandas as pd

(_, axs) = plt.subplots(2, 1, sharex=True)

t0 = pd.date_range("2023-09-12 10:00", periods=48, freq="1H")
t1 = pd.date_range("2023-09-12 10:00", periods=12*48, freq="5Min")

x0 = pd.Series(
    index=t0,
    data=np.random.randn(48),
)
x1 = pd.Series(
    index=t1,
    data=np.random.randn(12*48),
)

x0.plot(ax=axs[0])
x1.plot(ax=axs[1])

Issue Description

When using subplots and sharing x axis, if a latter plot has a higher temporal resolution, the former plot gets emptied.

image

(Sorry, I checked the box that I've tested with the latest version of pandas but unfortunately I was able to test only 2.0.3 for now. I'll see if I can manage to test 2.1.0 some other day perhaps.)

Expected Behavior

Both plots should show data:

image

Installed Versions

INSTALLED VERSIONS

commit : 0f43794
python : 3.11.4.final.0
python-bits : 64
OS : Linux
OS-release : 6.3.3
Version : #1-NixOS SMP PREEMPT_DYNAMIC Wed May 17 12:02:08 UTC 2023
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.0.3
numpy : 1.25.1
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.4.0.post0
pip : None
Cython : 0.29.36
pytest : 7.4.0
hypothesis : 6.68.2
sphinx : 5.3.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : 2.9.5
jinja2 : 3.1.2
IPython : 8.11.0
pandas_datareader: None
bs4 : 4.12.2
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.2
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.1
snappy : None
sqlalchemy : 2.0.19
tables : 3.8.0
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2022.7
qtpy : None
pyqt5 : None

@jluttine jluttine added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 12, 2023
@jluttine jluttine changed the title BUG: BUG: Subplot data is lost when sharex=True and latter DatetimeIndex has higher frequency Sep 12, 2023
@paulreece
Copy link
Contributor

Hey there, I was attempting to run your test script on 2.1.0 but it's not actually rendering any plots, I only get <Axes: > as a return value for each call to plot().

@jluttine
Copy link
Author

Ok, before the plotting commands, try running:

plt.ion()

Does that help?

@paulreece
Copy link
Contributor

paulreece commented Sep 12, 2023

Does that help?

Unfortunately no.
To be clear I ran this as a separate script as well as in the REPL.
Here's my REPL output:

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> import pandas as pd
>>> (_, axs) = plt.subplots(2, 1, sharex=True)
>>> t0 = pd.date_range("2023-09-12 10:00", periods=48, freq="1H")
>>> t1 = pd.date_range("2023-09-12 10:00", periods=12*48, freq="5Min")
>>> x0 = pd.Series(
...     index=t0,
...     data=np.random.randn(48),
... )
>>> x1 = pd.Series(
...     index=t1,
...     data=np.random.randn(12*48),
... )
>>> plt.ion()
<contextlib.ExitStack object at 0x144769c50>
>>> x0.plot(ax=axs[0])
<Axes: >
>>> x1.plot(ax=axs[1])
<Axes: >

@jluttine
Copy link
Author

jluttine commented Sep 13, 2023

How about plt.show() after plotting? (And no plt.ion())

@paulreece
Copy link
Contributor

paulreece commented Sep 13, 2023

That did it!

I can confirm the bug is occurring on the main branch 2.1.0

Screenshot 2023-09-13 at 1 40 30 PM

@azjps
Copy link

azjps commented Dec 26, 2023

This is related to #52895 (and the other issues linked in #52895 (comment)), the issue is that pandas plots periodic datetime ranges in a different unit (which will work okay if nothing else is changed about the plot, but once there are multiple axes/plots involved or any other matplotlib date utilites are used, everything breaks down). It seems to look okay after fixed to not plot in different units:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member Visualization plotting
Projects
None yet
Development

No branches or pull requests

4 participants