Skip to content

xaxis limits change when plotting time series of different resolution as secondary_y #3490

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

Closed
bmu opened this issue Apr 30, 2013 · 3 comments · Fixed by #7322
Closed

xaxis limits change when plotting time series of different resolution as secondary_y #3490

bmu opened this issue Apr 30, 2013 · 3 comments · Fixed by #7322
Labels
Milestone

Comments

@bmu
Copy link

bmu commented Apr 30, 2013

In [1]: N = 10000

In [2]: rng = pd.date_range('1/1/2000', periods=N, freq='min')

In [3]: ts = pd.Series(np.random.randn(N), index=rng)

In [4]: ax = ts.plot()

In [5]: ax.get_xlim()
Out[5]: (15778080.0, 15788079.0)

In [6]: ts.resample('D').plot(style='ro', ax=ax)
Out[6]: <matplotlib.axes.AxesSubplot at 0x4482c50>

In [7]: ax.get_xlim()
Out[7]: (15778080.0, 15788079.0)

In [8]: ts.resample('D').plot(style='ro', secondary_y=True, ax=ax)
Out[8]: <matplotlib.axes.AxesSubplot at 0x497e450>

In [9]: ax.get_xlim()
Out[9]: (10957.0, 10963.0)

As the xlim changes, the original data is not visible any more.

Could be related to #2185

@rosnfeld
Copy link
Contributor

Possibly related to #2960 ?

@rosnfeld
Copy link
Contributor

Replying to my own comment - given a bit of insight into what's going on in #2960 , I think this is a separate issue.

@rosnfeld
Copy link
Contributor

I think the different resolution is a red herring, it seems basically time-series plotting (in tseries/plotting.py) generally doesn't play well with secondary axes.

This is an example with no resampling that has the same behavior:

index_1 = pd.date_range(start='2000-01-01', periods=4, freq='D')
index_2 = pd.date_range(start='2000-01-05', periods=4, freq='D')

s1 = pd.Series(5, index=index_1)
s2 = pd.Series(7, index=index_2)

ax = s1.plot()
s2.plot(secondary_y=True, ax=ax)

The second plot call overwrites the x_lim, probably because (internal details of tseries/plotting) _get_xlim(ax.get_lines()) only references the current axis, and secondary_y creates a second axis object (via twinx()), and the two axes aren't considered together. This doesn't happen with a non-timeseries plot, e.g. if you change the indices to [1, 2, 3, 4] and [5, 6, 7, 8], though I'm not sure why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants