Skip to content

[MNT]: Alternate unit conversion and array duck typing.  #23015

Closed

Description

@jklymak

Summary

Units are inconsistent and only partially functional, particularly when combined with duck-typing array types.

Proposed fix

Matplotlib has had support for units for many years. There have been two main uses for this functionality,

  1. to convert data objects to numpy arrays
  2. to allow the units to change on an axis (ax.xaxis.set_units('feet')).

The first functionality generally works, and works well. However the second is only supported by plot. That is because plot retains the unit information, and Line2d reevaluates it at draw time. Unfortunately, no other plotting methods do this.

Here we propose to back this out of Line2D and instead convert at the beginning. So at the beginning of any plotting routine we will call x = ax.xaxis.convert_units(x). If xaxis already has a unit converter, that will get called with x as an argument. If not, then the units registry will be consulted and the unit converter added to xaxis. Data will always be stored as a numpy array with some base units. So if we have 'feet', 'miles', 'meters', we would convert and store x in units of meters (or whatever the converter prefers).

This leaves dealing with changing the units on the axis, which works for plot/Line2D but not other artists. This can be done one of two ways

  1. Just change the axis Locator and Formatter to the new units. This is currently what dates.py does already for different date resolutions. This has the benefit of keeping the data units the same as the numpy values stored by the Artist
  2. The more complicated method would be via another transform going from internal data to display data
    Overall, I think 1) is the easiest, but it does require folks with unit converters writing more flexible locators and formatters.

I note that this is the opposite direction being suggested by recent proposals to pipe the raw object all the way to the draw stage. But I'm not sure if that will end up successful, so thought I'd make this alternate suggestion.

Activity

  1. github-actions commented on Dec 5, 2025

    @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!

  2. timhoffm commented on Dec 5, 2025

    @timhoffm
    Member

    Hi does this relate to the data prototype work?

  3. story645 commented on Dec 5, 2025

    @story645
    Member

    Hi does this relate to the data prototype work?

    Its supposed to, the new data objects are supposed to I think move the unit handling to the data object or better sync it. @ksunden?

  4. timhoffm commented on Dec 6, 2025

    @timhoffm
    Member

    This means unit handling is addressed there and we do not need to track this separately.

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