-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
Problems plotting resampled time series when using loffset #18467
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
Comments
I think this is a duplicate of #9053, and if so, very much a coincidence, as I was just coding on a fix :-) |
See explanation in #9053 (comment), it's a not as easy a fix as I thought due to how matplotlib updates the units of the x-axis in a second plot. However, given that here in this issue both plotting calls are using a pandas |
I'm a pandas beginner and found this in search for a workaround. I've noticed that things seem to work if you plot a non-resampled timeseries first. In example above, it seems to work if you just swap the calls to the Plot 2original# This does not work
plt.figure()
plt.title('2: Resampled unshifted and loffset')
d_r.plot(color='green', label='unshifted')
d_ro.plot(color='brown', label='loffset')
plt.legend()
plt.savefig('2.png') order changedNow # This does not work
plt.figure()
plt.title('2: Resampled unshifted and loffset')
d_ro.plot(color='brown', label='loffset')
d_r.plot(color='green', label='unshifted')
plt.legend()
plt.savefig('2.png') Plot 4original# This does not work
plt.figure()
plt.title('4: Full data and resampled loffset')
d.plot(color='blue', label='full')
d_ro.plot(color='brown', label='loffset')
plt.legend()
plt.savefig('4.png') order changed# This does not work
plt.figure()
plt.title('4: Full data and resampled loffset')
d_ro.plot(color='brown', label='loffset')
d.plot(color='blue', label='full')
plt.legend()
plt.savefig('4.png') |
FWIW... After dumping out the problematic data series to a csv file, I've found that data with an inconsistent datetime delta causes problems (sensitivity to plot order, causes data to be shifted to the year ~1970, and/or causes the notebook to crash):
But manually fixing the datetime interval resolves the issues:
Not sure if that sheds any light on this bug or not. I'm running Pandas 0.21. |
Code Sample, a copy-pastable example if possible
Problem description
When plotting resampled time series with and without shift (loffset parameter to resample()) something goes wrong with the time axis.
Example data:
d
time series, resolution 1 minuted_r
resampled to 1 hourd_ro
resampled to 1 hour, index shifted 1/2 hour usingloffset
parameter toresample()
d_rs
isd_r
with index manually shifted 1/2 hourPlotting these graphs on top of each other doesn't work as expected.
Figure 5 shows the result I expected, figures 1-4 show various combinations. Figure 6 shows each series plotted individually, with something odd going on in the time axis.
When I inspect the various objects in IPython, there's nothing in the datetime index that looks off.
In Figure 5, i use Matplotlib to plot the un-resampled data, manually converting the date axis. Once I've done that, all the resampled data plots nicely, even though "unshifted" and "loffset" did not work together in Figure 2. I formatted the xaxis manually, and clearly something is odd with the axes of the first two subplots. If I use automatic formatting, the top two subplots have the same xaxis and formatting, and the lower two likewise.
Expected Output
I expected a result like Figure 5, but without needing to do anything to set the xaxis.
Output of
pd.show_versions()
pandas: 0.21.0
pytest: 3.2.1
pip: 9.0.1
setuptools: 36.5.0.post20170921
Cython: 0.26.1
numpy: 1.13.3
scipy: 1.0.0
pyarrow: 0.7.1
xarray: 0.9.6
IPython: 6.2.1
sphinx: 1.6.3
patsy: 0.4.1
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.2
feather: 0.4.0
matplotlib: 2.1.0
openpyxl: 2.4.8
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 0.9.8
lxml: 3.8.0
bs4: 4.6.0
html5lib: 0.999999999
sqlalchemy: 1.1.13
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: