-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
colorbar with dates #17447
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
Can you reproduce a self contained example here? In general matplotlib does not do automatic conversion on the data that is run through a norm so expect to jump through some hoops to get a colorbar with dates on it. |
sure, here it is: import numpy as np
import matplotlib.pyplot as plt
dates = np.datetime64('2019-11') + np.arange(10)*np.timedelta64(1, 'D')
X= np.random.randn(10, 2)
plt.scatter(X[:, 0], X[:, 1], c= dates)
plt.colorbar()
plt.show() On the other hand, if I use the dates for some axis, they get printed just as dates: plt.scatter(X[:, 0], dates)
plt.show() Well actually that scale could be improved, this is another problem, but fact is I think there is an inconsistency here between how dates are treated to make scales in axes, and to make scales on colorbars. Dates are not readable anymore when turned into numbers, and a scale is useless if not readable. |
The following is basically what is done for the x and y axis if the numpy array is a date. Adding the same support to the third dimension is really hard - in general, colormaps and colorbars are also used for images, pcolors, and contours, and the idea of those having convertible units is non-trivial. Here, you just convert the dates to floats and then set the proper formatters and locators: dates = np.datetime64('2019-11-01') + np.arange(10)*np.timedelta64(1, 'D')
X= np.random.randn(10, 2)
plt.scatter(X[:, 0], X[:, 1], c=mdates.date2num(dates))
cb = plt.colorbar()
loc = mdates.AutoDateLocator()
cb.ax.yaxis.set_major_locator(loc)
cb.ax.yaxis.set_major_formatter(mdates.ConciseDateFormatter(loc))
plt.show() |
Yes/No - I think you might be able to get it by putting a convertor on NoNorm. |
Its not the conversion thats particularly the issue, its getting the unit info down to the colorbar. |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
When using a datetime vector for color mapping a plot, the colorbar treats it as a numerical vector where units are nanoseconds. You can see this in this discussion. It is possible however, to change the scale so that it shows human readable dates, but it's quite cumbersome and I had to try many times before finding a working solution here.
I think that the colorbar, in these cases, should have a readable scale by default, and matplotlib already uses it when one of the axes of a plot is datetime, so I think it should be natural to adopt it also for colorbars. Let me know if you agree
My matplotlib version
print(matplotlib.get_backend())
):module://ipykernel.pylab.backend_inline
I have installed python from default anaconda package
The text was updated successfully, but these errors were encountered: