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
Add declarative attribute plot_units #1314
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.
I can merge as-is if need be, but I think using the accessor for unit-handling would make the code much simpler.
src/metpy/plots/declarative.py
Outdated
data_subset = data.metpy.sel(**subset).squeeze() | ||
|
||
if self.plot_units is not None: | ||
data_subset.values = (data_subset.values |
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 think you should be able to do this as data_subset.metpy.convert_units(self.plot_units)
. That would eliminate the need to then set the unit attribute below I believe.
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 concur, as this will prevent the need for an immediate refactor after #1304.
With the decision made in #1304, I would definitely recommend that the units of a DataArray should only be handled by the accessor (since the units will soon be part of the variable data inside of the DataArray rather than an attribute on the DataArray).
src/metpy/plots/declarative.py
Outdated
data_subset_v = v.metpy.sel(**subset).squeeze() | ||
|
||
if self.plot_units is not None: | ||
data_subset_u.values = (data_subset_u.values |
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.
Here too.
Well that was a lot cleaner and more robust! |
Looks like some changes to underlying versions changed our coverage, but unrelated to your changes. I'll open an issue about coverage, but we can merge over. |
@kgoebber Was there a reason we didn't add such support to |
Not really, just didn’t focus on that originally, but would be great to
have. Can look at it tomorrow and potentially get a PR in.
|
So investigating this morning this would be a bit more involved on two fronts. First, there is not as easy of a conversion mechanism yet for non-vector observation quantities within Overall it shouldn't be too hard and I can probably get a PR done by the end of the day, but will involve modifying Currently there is the ability to convert the units for vectors (both barbs and arrows) in |
This PR partially resoled an issue in #1248 . |
Description Of Changes
This adds a new attribute,
plot_units
, to help with simple unit conversions for plotting. It is available for allPlot2D()
subclasses includingContourPlot()
,FilledContourPlot()
,ImagePlot()
, andBarbPlot()
.Two new tests have been added to cover unit conversations for scalar and barb plots.
Running declarative tests fail likely due to running Matplotlib 3.1.2 locally and positions of text on some plots (especially those tests using
PlotObs()
) change slightly. Don't know if they will fail on Travis, so we'll see.This is an immediate need for teaching class in just over a week in order to not have to over complicate making simple maps with our common units, which are not the stored SI units in our data files (e.g., Celsius instead of Kelvin and Knots instead of m/s).
Checklist