Skip to content

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

Open
c-foschi opened this issue May 18, 2020 · 6 comments · May be fixed by #27721
Open

colorbar with dates #17447

c-foschi opened this issue May 18, 2020 · 6 comments · May be fixed by #27721
Labels
keep Items to be ignored by the “Stale” Github Action New feature topic: units and array ducktypes

Comments

@c-foschi
Copy link

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

  • Operating system: windows 10
  • Matplotlib version: 3.1.3
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.7.3
  • Jupyter version (if applicable): 6.0.3

I have installed python from default anaconda package

@jklymak
Copy link
Member

jklymak commented May 18, 2020

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.

@c-foschi
Copy link
Author

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()

image

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()

image

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.

@jklymak
Copy link
Member

jklymak commented May 19, 2020

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()

boo

@story645
Copy link
Member

story645 commented May 19, 2020

and the idea of those having convertible units is non-trivial.

Yes/No - I think you might be able to get it by putting a convertor on NoNorm.

@jklymak
Copy link
Member

jklymak commented May 19, 2020

Its not the conversion thats particularly the issue, its getting the unit info down to the colorbar.

@github-actions
Copy link

github-actions bot commented Aug 2, 2023

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!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Aug 2, 2023
@story645 story645 added keep Items to be ignored by the “Stale” Github Action and removed status: inactive Marked by the “Stale” Github Action labels Aug 2, 2023
@dstansby dstansby linked a pull request Jan 30, 2024 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keep Items to be ignored by the “Stale” Github Action New feature topic: units and array ducktypes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants