-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add a new time_support
context manager for plotting times
#8782
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks very nice! Some in-line comments, the largest one of which is that it would be nice if the default scale and format were taken from the (first) input times.
On the names: I guess one could go for mpl_quantity_support
, but that certainly doesn't seem worth the trouble of changing the name.
Should there be an matplotlib_astropy_support
, though, where all these options get turned on?
But those questions are probably best addressed separately.
astropy/visualization/time.py
Outdated
YMDHMS_FORMATS = ('fits', 'iso', 'isot') | ||
|
||
|
||
def time_support(scale='utc', format='isot', simplify=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to have the default set by the times first used? (say, scale=None
here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, this will be consistent with quantity_support
- fixed
astropy/visualization/time.py
Outdated
""" | ||
scaled = getattr(value, self.scale) | ||
if self.format in YMDHMS_FORMATS: | ||
return scaled.mjd |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this done to make the values numerical? But, if so, shouldn't it happen for all string formats? For best precision for relevant times, one might consider using cxcsec
or so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've now fixed compatibility for all string formats (and have tests for all of them).
@@ -0,0 +1,121 @@ | |||
.. _quantity: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This link seems wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
@mhvk - thanks for the initial review! You mentioned that using MJD might not be ideal, so I was wondering whether, since you are intimately familiar with astropy.time, you would mind trying out this PR and finding cases where the precision doesn't work properly? One use case I know of already that needs to be fixed is when looking at very small time intervals, the string-based formats need to have their precision updated (they are currently fixed to millisecond precision). But it would be great if you could let me know if you can find other cases that won't work properly/optimally! |
This whole functionality doesn't work very well with Matplotlib 2.0 just because of the way that it dealt with non-numpy arrays back then (it split the Time array into a list of many times, and same for Quantity). Since Matplotlib 2.1, 2.2, 3.0 and 3.1 are out now and 2.0 is over two years old, maybe we can just drop it? (rather than have to add convoluted workarounds here). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@astrofrog LGTM, thanks! I'm fine with dropping support for matplotlib 2.0
. I don't think it's worth the trouble.
The |
@astrofrog - using
This is probably good enough for most purposes... But it brings up a different question: do you also handle |
@mhvk - just to check, do you mean we should support it in |
@astrofrog - yes, you're right, I do think an overall |
… with various formats/scales in Matplotlib
…manager is defined
ae97d26
to
899f0ee
Compare
@mhvk @larrybradley - this is now ready for a final review (I think |
Codecov Report
@@ Coverage Diff @@
## master #8782 +/- ##
==========================================
+ Coverage 86.99% 87.01% +0.02%
==========================================
Files 400 401 +1
Lines 59469 59598 +129
Branches 1100 1100
==========================================
+ Hits 51736 51861 +125
- Misses 7092 7096 +4
Partials 641 641
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #8782 +/- ##
==========================================
+ Coverage 86.99% 87.01% +0.02%
==========================================
Files 400 401 +1
Lines 59469 59598 +129
Branches 1100 1100
==========================================
+ Hits 51736 51861 +125
- Misses 7092 7096 +4
Partials 641 641
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! One in-line question about whether we should raise the minimum version of matplotlib to 2.2 for astropy 4.0. But obviously that's no reason to hold up this PR, so approving.
Would be good to raise separate issues for
- Support for
TimeDelta
- in the next matplotlib release, these will be captured by thistime_support
(as a subclass ofTime
), so we need an explicit solution (this probably needs a 4.0 milestone). - Combined
astropy_support
. Possibly with the question of whether we should turn both on by default...
Convert a Time value to a scalar or array. | ||
""" | ||
|
||
# For Matplotlib < 2.2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we increase our minimum version?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already just increased it to 2.1 from 2.0, but we could consider increasing further especially given the 2 year support for 4.0. I wish we had a written down policy for this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably OK to leave for later: #8822 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @astrofrog! Just one little inline nit about the parameter order in the docstring.
Thanks for raising the issues. SInce @larrybradley's comment was addressed, and he was happy otherwise, I'll merge now - very nice to have this!! |
Hmm, having done it, not sure that the right way is to "dismiss a review".... |
and remove unnecessary code added in astropy#8818 and astropy#8782
and remove unnecessary code added in astropy#8818 and astropy#8782
and remove unnecessary code added in astropy#8818 and astropy#8782
and remove unnecessary code added in astropy#8818 and astropy#8782 Add new JSON for figure tests.
and remove unnecessary code added in astropy#8818 and astropy#8782 Add new JSON for figure tests.
Matplotlib natively provides a mechanism for plotting dates and times on one or both of the axes, as described in Date tick labels. To make use of this, users need to explicitly access
plot_date
on time instances they want to plot. While this might be ok for a number of situations, I thought it would be nice to have a way of more seamlessly using times when plotting that gives more control over the time scale and format.This PR implements a
time_support
context manager/function that is meant to be similar toquantity_support
:(by default the time is removed from the ISO string if it's always midnight for all tick labels, this can be controlled with the
simplify
argument).This can be used as a context manager and can take the format and scale to use for the axis:
Note that this also allows
Time
to be used inset_xlim/set_ylim
for example. One of the benefits of this approach is that all times are automatically converted to a common scale/format.A few notes:
Something which I'm not entirely happy with, which also affects
quantity_support
, is that even when using the context manager, once an axis has been used inside the context manager, it has a cache of the converter and it's impossible to change it again later. I think this is something that would need to be fixed in MPL though. As long as a new axis/figure is created, then the context manager works fine.I'm also not really fond of the current names
quantity_support
andtime_support
, I feel there should be aplot
ormpl
in there somewhere so it's clear that it's related to plotting.I've consolidated the docs to mention
quantity_support
andtime_support
in one place, in the visualization docs.There is still some cleanup to do in various places and I think I can expand the docs and tests a little, but I don't want to invest too much time until I hear whether people are interested in this. I need this anyway for another project so I decided to implement it here, but it's ok if people would prefer not to include it!