-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Plotting of DatetimeIndex directly with matplotlib no longer gives datetime formatted axis (0.15) #8614
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
Maybe someone should bite the bullet and try to get plots of np.datetime64 working upstream in matplotlib? Here's the relevant issue: matplotlib/matplotlib#1097 |
yes, good point :-) |
I would just stick a note in the docs (we already have a small section about using matplotlib directly for certain higher perf plots). this is a matplotlib issue and the pandas behavior is correct (and was noted in the whatsnew for 0.15.0; PeriodIndex plotting already exhibited this) - I think you workaround is fine. |
@jreback It was not mentioned in the whatsnew docs AFAIK (or did I miss it somewhere? there is something about plotting a PeriodIndex, but this is about DatetimeIndex and about plotting directly with matplotlib), but I will add it when finishing #8586 I know it is matplotlib issue, but it is just that I think a lot of people relied on this behaviour (as I did myself in my code). But as I said, don't think there is much we can do about it |
@jorisvandenbossche oh, I meant that comment :). Ok, not sure much to do then except doc it. |
It seems simply doing |
So we can easily register a converter for Other possibility is to add to the docs that users who want to do this direct plotting with matplotlib, can always add |
+1 on this you can always disable by resetting the index and convert to ints. |
@jorisvandenbossche The chances of this being accepted upstream in matplotlib seem slim: matplotlib/matplotlib#1097 |
My guess is that matplotlib would like to have a fuller solution for automatic datetime64 conversion that handles other precisions than |
iirc there was also an issue with supporting older versions of numpy that don't have datetime64 (we go back to 1.6, but only recently dropped 1.5). The numpy docs ( http://docs.scipy.org/doc/numpy/reference/arrays.datetime.html) say the dtype is experimental, is that no longer true? I guess if both pandas and mpl start using it as-is we are a decent lobby to prevent breaking the API This is now near the top of my mid-term to-do list.... |
@tacaswell Numpy's datetime64 remains broken in a number of ways (e.g., NaT, timezone support, conversions, etc.). But we don't have any better alternative, and pandas has patched over most of it. From an design perspective, the custom precision available in datetime64 is regrettable because it adds a lot of complexity (pandas only uses |
@shoyer Actually, pandas' solution relies on converting Although this quick fix would work fine for most users, a nasty problem is that the resolution of matplotlib's current datetime format (float days since 0001) is on the order of ~10 microseconds (iirc) for the typical range of years and thus not sufficient to represent the full resolution of |
I could be wrong here, but I'm pretty sure I noticed a to_datetime hiding in DatetimeConverter. That gives you back a DatetimeIndex (with ns precision). The simple check would be to try registering this and see if something chokes on datetime64 that doesn't fit in ns precision. On Tue, Oct 28, 2014 at 11:17 AM, agijsberts [email protected]
|
@shoyer I'm not sure if I understood you correctly, but I meant that rudimentary support for
This approach does not rely in any way on pandas, though it's much like how it's done now in pandas (with added support for multiple resolutions). Of course the problem of matplotlib not being able to fully represent the possible resolutions remains a show-stopper. |
@agijsberts Ah, OK. Yes, I like your solution. Much simpler, doesn't need pandas. IMO this is still worth doing even if higher resolution dates with matplotlib are not possible. I would be happy just with a warning that triggers when the limits of datetime axis are too close together (e.g., less than 1 ms apart) to accurately plot the times. For most uses, us precision is not necessary. |
Things are slowly starting to make sense to me about this. Is the best (long term) solution here to rip out the float representation used in the dates module and replace it with datetime64 objects? cc @cimarronm |
@shoyer It indeed uses For matplotlib:
|
only think a tiny bit non- trivial is that so if a tz is attached you should use datetime.datetimes (but with a non-pandas array it's not possible anyhow so prob not a big deal) |
From http://stackoverflow.com/questions/26526230/plotting-datetimeindex-on-x-axis-with-matplotlib-creates-wrong-ticks-in-pandas-0
Plotting of course works:
But plotting with matplotlib's
plot
functions gives no longer a datetime formatted x-axis, but just ints:Reason: matplotlibs
plot
callsnp.atleast_1d
on the input (https://github.com/matplotlib/matplotlib/blob/v1.4.0/lib/matplotlib/axes/_base.py#L220). Previously this returned an Index, now it returns a array ofdatetime64
values, which matplotlib can't handle.Issues:
Index
also.It seems there was some discussion about this in the Index-no-subclass PR: #7891 (comment) and #7891 (comment) (I commented there about exactly this issue, but I don't know why we didn't look further in it then)
The text was updated successfully, but these errors were encountered: