Skip to content

Plotting with fill_between and timedelta on yaxis throws TypeError #8352

Closed as not planned

Description

@SebastianRzk

Bug report

Hi everyone,
I don't know whether it is a bug or a missing feature in numpy or matplotlib, but plotting with fill_between and a simple datetime.timedelta (x axis) throws the following exception:

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Here is a simple (runnable) example:

from datetime import timedelta
from matplotlib import pyplot
from pandas import DataFrame

if __name__ == '__main__':
    df = DataFrame([[timedelta(seconds=0), 4], [timedelta(seconds=10), 5]], columns=['TIME', 'VALUE'])
    df = df.set_index('TIME')
    pyplot.fill_between(df.index, 0, df.VALUE)

Full stacktrace:

Traceback (most recent call last):
  File "/de/test.py", line 11, in <module>
    pyplot.fill_between(df.TIME, 0, df.VALUE)
  File "/usr/lib/python3.6/site-packages/matplotlib/pyplot.py", line 2896, in fill_between
    **kwargs)
  File "/usr/lib/python3.6/site-packages/matplotlib/__init__.py", line 1819, in inner
    return func(ax, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4581, in fill_between
    x = ma.masked_invalid(self.convert_xunits(x))
  File "/usr/lib/python3.6/site-packages/numpy/ma/core.py", line 2340, in masked_invalid
    condition = ~(np.isfinite(a))
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

With dates, everthing works as expected:

from matplotlib import pyplot
from pandas import DataFrame
import pandas


if __name__ == '__main__':
    df = DataFrame([[1488364614802, 4], [1488364615802, 5]], columns=['TIME', 'VALUE'])
    df.TIME = pandas.to_datetime(df.TIME, unit='ms')
    df = df.set_index('TIME')
    print(df)
    pyplot.fill_between(df.index, 0, df.VALUE)

Matplotlib version
Matplotlub version: Version: 2.0.0 (via pip)
Python 3.6.0
Plattform: Linux 4.10.1-1 x86_64

Activity

  1. phobson commented on Mar 21, 2017

    @phobson
    Member

    I wouldn't expect this to work, though that doesn't mean that it shouldn't. But it's just not obvious to me how timedeltas should be handled. For example, your x-values were instead:

    x = [timedelta(seconds=0), timedelta(seconds=10), timedelta(seconds=5)]

    should the values be plotted at something analogous to [0, 10, 5], or [0, 10, 15]?

    My intuition is that you should be using a pandas PeriodIndex, rather than stdlib timedeltas.

  2. SebastianRzk commented on Mar 24, 2017

    @SebastianRzk
    Author

    Sorry, but I don't see any difference between the behavior with datetimes and timedeltas. Both are complex datatypes which can be transformed in various "simple" operations, e.g.:
    df.TIME = df.TIME - df.TIME.min()
    If a datatype is sortable and all the items are unique (here: index), there should be a intuitive way to plot it? Timedelta similar to datetime?

  3. phobson commented on Mar 24, 2017

    @phobson
    Member

    @RosesTheN00b from that response, it's still unclear to me if my examples should be plotted as [0, 10, 5], or [0, 10, 15]. Which is it?

  4. SebastianRzk commented on Mar 24, 2017

    @SebastianRzk
    Author

    Okay, I realized the problem. Are all timedeltas absolute to one "imaginary" origin, or describe they the interval to the last element.

  5. anntzer commented on Apr 15, 2017

    @anntzer
    Contributor

    I think the only realistic interpretation is elapsed time since a common start point (e.g. start of a physical measurement).

  6. dstansby commented on Jul 11, 2017

    @dstansby
    Member

    It should definitely be [0, 10, 5] @phobson. Timedeltas have a 1 to 1 mapping to total seconds (eg. timedelta(minutes=1).total_seconds() returns 60).

    I think fixing this depends on #8730 (as does plotting timedeltas in general)

  7. github-actions commented on Apr 9, 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!

  8. astrojuanlu commented on Apr 9, 2023

    @astrojuanlu

    IIUC #9120 was not merged and probably this issue still stands

  9. jklymak commented on Apr 9, 2023

    @jklymak
    Member

    The following works fine:

    import numpy as np
    import matplotlib.pyplot as plt
    from datetime import timedelta
    
    x = np.array([0, 10, 5], dtype='timedelta64[s]')
    # x = [timedelta(seconds=0), timedelta(seconds=10), timedelta(seconds=5)]
    y = [1, 2, 3]
    
    fig, ax = plt.subplots()
    ax.plot(x, y)
    ax.fill_between(x, 0, y)

    Removing the comment above, replacing x with a list of timedeltas fails plot as well as fill_between with:

     File "/Users/jklymak/matplotlib/lib/matplotlib/cbook.py", line 1280, in _to_unmasked_float_array
    
        return np.asarray(x, float)
               ^^^^^^^^^^^^^^^^^^^^
    TypeError: float() argument must be a string or a real number, not 'datetime.timedelta'
    

    That happens because you can convert a timedelta object directly to float: float(timedelta).

    In my opinion this is all fine. If you can't do float(x[0]) we can't handle it. Numpy timedeltas work because as floats they return their "seconds since" values.

    As for the pandas integration, again, if you can't do float(x[0]) we can't help you. It's up to the user to convert their data to something Matplotlib can handle.

    My vote is to close as won't-fix and/or add more documentation on this. The converse argument is that we should add tiemdeltas to our date handling, but I actually am now convinced that is a mistake - timedeltas are not dates, they are the difference between dates. If you want dates do [date0 + td for td in x] where date0 is a your origin.

  10. phobson commented on Jun 21, 2023

    @phobson
    Member

    agree with @jklymak here. I'd close this myself, but that feels presumptuous given how sparse my involvement as been for the past ::mumbles:: years

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