Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better datetime axis compatibility #312

Merged
merged 4 commits into from
Feb 23, 2024
Merged

Conversation

nvaytet
Copy link
Member

@nvaytet nvaytet commented Feb 22, 2024

Computing limits on datetime axes is a little funny with data has that passed through matplotlib.
Datetimes are converted to floats for pcolormesh coordinates (2d images) but not for lines.

This PR tries to handle the different cases in a more consistent way, which allows us to plot 1d and 2d data on the same axes with datetime coords (which failed previously). It is not a common use case but was requested by users.

Example:

import plopp as pp
import numpy as np
import scipp as sc

# 2d data
t = np.arange(
    np.datetime64('2017-03-16T20:58:17'), np.datetime64('2017-03-16T21:15:17'), 20
)
time = sc.array(dims=['time'], values=t)
z = sc.arange('z', 50.0, unit='m')
v = np.random.random(z.shape + time.shape)
da = sc.DataArray(
    data=sc.array(dims=['z', 'time'], values=10 * v, variances=v),
    coords={'time': time, 'z': z},
)
f = pp.plot(da)

# 1d data
v = np.random.rand(time.sizes['time'])
a = sc.DataArray(
    data=sc.array(dims=['time'], values=10 * v, variances=v),
    coords={'time': time},
)
a.plot(ax=f.ax, color='r')

Screenshot at 2024-02-22 16-51-54

@@ -8,17 +8,22 @@

import numpy as np
import scipp as sc
from matplotlib import dates as mdates
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in the common backend module. Shouldn't it be in the matplotlib module?

Copy link
Member Author

@nvaytet nvaytet Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, good point.

That means we need a different bounding box implementation for each backend

We can do it with a common bounding box, but it just means we have to convert all the values in the coordinate to floats instead of converting only the min and max. The performance penalty is probably minimal, considering all the other things we do for every update.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test?

@nvaytet nvaytet merged commit c00c439 into main Feb 23, 2024
3 checks passed
@nvaytet nvaytet deleted the datetime-axis-compatibility branch February 23, 2024 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants