Skip to content

colorbar with dates #17447

Open
Open

Description

@c-foschi

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

Activity

  1. jklymak commented on May 18, 2020

    @jklymak
    Member

    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.

  2. c-foschi commented on May 19, 2020

    @c-foschi
    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.

  3. jklymak commented on May 19, 2020

    @jklymak
    Member

    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

  4. story645 commented on May 19, 2020

    @story645
    Member

    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.

  5. jklymak commented on May 19, 2020

    @jklymak
    Member

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

  6. github-actions commented on Aug 2, 2023

    @github-actions

    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!

  7. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions