Skip to content

Enable plotting of timedeltas #8869

Open
1 of 1 issue completed
Open

Description

@dstansby

This is a general issue to track progress in enabling the plotting of timedelta objects. The eventual aim is for code like this to work:

import matplotlib.pyplot as plt
from datetime import timedelta

x = [1, 2]
y = [timedelta(seconds=1), timedelta(seconds=2)]
fig, ax = plt.subplots()
ax.scatter(x, y)
plt.show()

TODO

Activity

  1. added this to the milestone on Jul 12, 2017
  2. anntzer commented on Jul 12, 2017

    @anntzer
    Contributor

    Quick question from the peanut gallery: could datetime/timedelta support be folded into unit support? (I have no idea.)

  3. dstansby commented on Jul 12, 2017

    @dstansby
    MemberAuthor

    (disclaimer: I don't really know what I'm doing with all this and am just copying datetime support and learning as I go along)

    I think datetime is registered in the unit framework. The last line of dates.py is units.registry[datetime.datetime] = DateConverter() which means a datetime passed to plot automatically uses all the converter/ticker stuff in dates.py. Is that what you meant?

  4. anntzer commented on Jul 12, 2017

    @anntzer
    Contributor

    I don't know anything about datetime plotting either, but the general idea was indeed whether we could end up with something like units.registry[timedelta] = TimeDeltaConverter.

  5. has2k1 commented on Jul 30, 2017

    @has2k1
    Contributor

    I dealt with timedelta breaks and formats in mizani. The implementation is not the cleanest but I have been satisfied with the results.

  6. pganssle commented on Aug 19, 2017

    @pganssle
    Member

    Is there something somewhere that details how unit support is handled? Folding timedelta directly into datetime seems weird - it seems like it would enable you to do things like plt.plot([timedelta(days=200), datetime(1970, 11, 14)], [0, 1])? Seems like you'd want to be kinda explicit about when you can use timedelta and when you can use datetime.

    That said, we're essentially never explicit about when you can use "meters" vs. "delta-meters", so maybe my concerns are a bit overblown? I guess it feels weirder because numbers are mostly unitless and these have explicit, non-interchangeable units.

  7. dstansby commented on Aug 22, 2017

    @dstansby
    MemberAuthor

    Thanks for all your comments @pganssle!

    I think there are two cases that I've probably accidentally folded into one here:

    1. Plotting timedelta objects
    2. Plotting datetime objects that need some sort of offsetting with timedelta (ie. plotting a square

    In case number 2., offsetting should be done before any conversion to internal representations, such that only datetime coordinates are ever plotted, and none of the converting timedelta machinery is needed.

    Case 1. is what should be tackled by this issue, and as is said above, we need to be very careful to keep the two cases distinct, and error when a code snippet like above (#8869 (comment)) is run.

  8. pganssle commented on Aug 22, 2017

    @pganssle
    Member

    @dstansby Yes, I think with these concerns cleanly separated, implementing timedelta handling becomes much easier, too. Other than the question of formatting (and if you have any references on timedelta formatting I'd like to implement that in dateutil, so I'm interested in what's the "best way"), so long as the units are being enforced, it's just timedelta.total_seconds(), none of the complicated nonsense about time zones and the like, since timedelta() by its nature represents a duration.

  9. dstansby commented on Aug 22, 2017

    @dstansby
    MemberAuthor

    Great, will put a comment on that issue about timedelta formatting. Would be much better to have it somewhere in dateutil than for us to roll our own version.

  10. dstansby commented on Aug 29, 2017

    @dstansby
    MemberAuthor

    Was easier to just open a new PR at #9120

  11. dstansby commented on Aug 29, 2017

    @dstansby
    MemberAuthor

    Woops, that comment was meant for the PR!

  12. 6 remaining items

  13. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on May 20, 2024
  14. gitsdfawfevi commented on Nov 14, 2024

    @gitsdfawfevi

    The ideas in #19236 were that we would use datetime-like Formatters and Locators to the timedeltas, but I'm no longer convinced that this is a good idea. If someone really wants time, they can add an origin datetime to each timedelta, either when their experiment started, or a nominal date and pass to our datetime converter.

    I evaluate simulations and write the values into a pandas DataFrame with timedelta as index. Using datetime is the wrong choice in this case, as there is no starting day on which the simulation starts (it is relatively at 0 seconds). see my whole comment: pandas-dev/pandas#60079 (comment)

  15. jklymak commented on Nov 14, 2024

    @jklymak
    Member

    @gitsdfawfevi Yes, we understand that use case. The question is if there is truly value in HH.MM.SS since simulation start, or if folks are OK with decimal seconds, hours, or days etc. Most fields I'm familiar with, the latter is fine.

  16. gitsdfawfevi commented on Nov 14, 2024

    @gitsdfawfevi

    @jklymak I assume that you mean the following representation 03:15:00 == 3.25 h, therefore an example of when the decimal representation is very unfavorable in a plot: For example, there is data from a year simulation, represented decimal in months (= 12 mo). Now you want to interactively enlarge the plot to a certain time range in seconds. Of course, this can also be achieved by converting units and changing the corresponding axis labeling, but it is very complex.

    Regardless of whether this is implemented or not, I find it very critical how matplotlib deals with timedelta “The critical point is that matplotlib pretends to understand timedelta, but the result is ‘wrong’ -- at least not what you would expect.” (pandas-dev/pandas#60079 (comment))

  17. jklymak commented on Nov 14, 2024

    @jklymak
    Member

    lets not mix concerns here - Matplotlib deals with np.timedelta64, we don't directly handle pandas Timedelta plotting, and we don't provide pd.df.plot. If there is something we can do to make that work more smoothly, we will, because pandas is used so extensively, but please express the feature request in terms of numpy objects and our plotting routines (which underpin the pandas ones)

  18. gitsdfawfevi commented on Nov 27, 2024

    @gitsdfawfevi

    Sorry, that was my mistake. I wasn't aware that there is a difference between pandas and numpy timedelta. Especially as both use the same dtype designation.

    In example #8869 , timedelta from python datetime is used, but it is also different from numpy datetime (https://numpy.org/doc/stable/reference/arrays.datetime.html). Would the implementations of datetime from numpy and python then be compatible with matplotlib?

  19. maoding commented on Dec 16, 2024

    @maoding

    lets not mix concerns here - Matplotlib deals with np.timedelta64, we don't directly handle pandas Timedelta plotting, and we don't provide pd.df.plot. If there is something we can do to make that work more smoothly, we will, because pandas is used so extensively, but please express the feature request in terms of numpy objects and our plotting routines (which underpin the pandas ones)

    @jklymak the example with numpy objects is the following:

    import numpy as np
    import matplotlib.pyplot as plt
    
    num_values = 10
    now = np.datetime64("2005-02-25")
    
    datetime_values = np.array(
        [now + np.timedelta64(i, "h") * i for i in range(num_values)]
    )
    timedelta_values = np.diff(datetime_values)
    zero_timedelta = np.array([np.timedelta64(0, "h")])
    timedelta_values = np.cumsum(np.insert(timedelta_values, 0, zero_timedelta))
    values = [i for i in range(num_values)]
    
    plt.figure()
    plt.plot(datetime_values, values, marker=".")
    plt.xticks(rotation=90)
    plt.title("Numpy Datetime")
    plt.tight_layout()
    plt.show()
    plt.close(plt.gcf())
    
    plt.figure()
    plt.plot(timedelta_values, values, marker=".")
    plt.xticks(rotation=90)
    plt.title("Numpy Timedelta")
    plt.tight_layout()
    plt.show()
    plt.close(plt.gcf())

    Resulting in
    image
    and
    image

    Here the context is displayed correctly in both cases. In pandas-dev/pandas#60079 the context isn't displayed properly, compare the linear context with pd.timedelta versus the curved with pd.datetime.

    So this is a problem of pandas using matplotlib (or anythin related) rather than a matplotlib problem, and thus should be fixed in pandas, isn't it?

  20. scottshambaugh commented on Mar 7, 2025

    @scottshambaugh
    Contributor
    1. Plotting datetime objects that need some sort of offsetting with timedelta

    I'm running into this use case right now. Allowing Axes.convert_xunits and Axes.convert_yunits to take in timedelta objects seems reasonable, for axes that already have a time scale.

  21. scottshambaugh commented on Mar 7, 2025

    @scottshambaugh
    Contributor

    As a workaround, the below shows how to convert a timedelta to matplotlib's numerical date coordinates.

    import matplotlib.dates as mdates
    from matplotlib.lines import Line2D
    from datetime import datetime, timedelta
    
    line: Line2D
    x_date: datetime
    x_offset: timedelta
    
    x_num = line.convert_xunits(x_date)
    xoffset_num = mdates.date2num(x_offset + x_date) - x_num
  22. jklymak commented on Mar 8, 2025

    @jklymak
    Member

    @scottshambaugh Thanks for this. Can you clarify the output you want and how you originally set the x-axis units?

  23. scottshambaugh commented on Mar 8, 2025

    @scottshambaugh
    Contributor

    Hm, that was driven by some subclassing of Text and manually setting positions. When working through the normal interfaces, datetimes as coordinates work fine. I'm having some trouble making a standalone script that shows the issue I was running into, it's probably fine as-is.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions