Skip to content

[ENH]: Better support for stackplot with units [particularly datetimes] #27129

Open

Description

@ksunden

Problem

As noted during the review of #27114, the current support of units in stackplot is subpar, particularly for the y axis.

For datetimes in particular, the implicit 0 of the API is problematic, introducing 1970-01-01 when that is not a relevant date.

Therefore I suggest allowing baseline to be not only the current supported string literals, but also a scalar value (or potentially an array, I guess) such that a "sensible" baseline can be provided for cases where 0 is not sensible. (even the other options for baseline assume you want your plot centered around 0... I'm not concerned about wanting "wiggle centered at 2023-02-28" or anything like that, that seems perhaps a bridge too far.)

I also think it is important to explicitly state that the y values are in delta units (similar to how we do for e.g. bar for the width parameter. This is already true in that adding actual datetime values will fail, but passing timedelta objects will work.

Finally, the cumsum call contains a promote_types(y.dtype, np.float32), which will fail for np.timedelta64 arrays (Edit, to be consistent with other points, clarified that I really meant timedelta instead of datetime64 here):

stack = np.cumsum(y, axis=0, dtype=np.promote_types(y.dtype, np.float32))

So either removing that or at least handling the error case is needed. It's not fully clear to me what we are guarding against there, as cumsum of integers is fine being integral, though I guess we could run into overflow issues? Passing datetime.timedelta as object arrays (or as lists which get turned into object arrays) will work and produce a plot, just without a sensible baseline, currently.

Proposed solution

No response

Activity

  1. anntzer commented on Oct 18, 2023

    @anntzer
    Contributor

    I didn't really think about this in depth, but does a stackplot centered on a time/date actually ever make sense? I think stackplots centered on the zero timedelta does make sense, though that goes back to the long-standing issue of adding a unit converter for timedeltas. Centering on a time/date, I'm not so sure.

  2. ksunden commented on Oct 18, 2023

    @ksunden
    MemberAuthor

    I think that allowing baseline to be a scalar is a relatively easy change that makes sense and makes it more consistent with other plot types that have implicit zeros (e.g. bar graphs, stem plots, etc) While I do not have a specific usecase in mind, I think that is a good change regardless.

    Beyond that, clarifying in docs that units are in delta units I think makes sense because they are processed through cumsum. and while I also agree that datetime is an odd one, it is a specific case of the general class of unit-ful data that has a different unit for delta vs absolute (and it is the only one that is baked in to the core library).

    The other common unit that has such a distinction is temperature units (at least in some unit libraries) and I can easily think of plot that is using stackplot to e.g. plot the contribution of different energy sources to the temperature of something, and wanting that baseline to be the starting temperature, e.g.

    So, in summary I agree that the specific case is unusual, but the general case that can be enabled by relatively small changes may be useful. (And the specific case just gives us the ability to test it)

  3. EricRLeao1311 commented on Nov 12, 2023

    @EricRLeao1311

    Hey guys! I think that I got what you mean. I'm going to open a PR implementing the possibility of the baseline receiving a scalar value. About it receiving an array, as far as I understood, the possibility is still open. Also, with the PR, I believe that more fruitful discussion will occur, which is a plus. Glad to close it or do any other action based on your feedback!

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