-
Notifications
You must be signed in to change notification settings - Fork 12
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 a plotting example #61
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ba6ef40
add a plotting example
keewis 524c48c
add matplotlib to the dependencies
keewis 8e4714a
configure nbsphinx
keewis 8719576
fail RTD build on warnings
keewis 9832782
use the correct name for matplotlib
keewis 391afff
also depend on ipykernel and jupyter_client
keewis 1b57f07
use alert-warning for the note
keewis d0bd134
use "alert alert-info" instead
keewis d831053
Merge branch 'master' into plotting-example
keewis fab0a4c
try to fix the info block
keewis 9a11619
try if removing the ' helps
keewis 6a73709
fix the alert markup
keewis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
Examples | ||
======== | ||
There are no examples yet. | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
examples/plotting |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "round-optimization", | ||
"metadata": {}, | ||
"source": [ | ||
"# plotting quantified data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "greatest-smart", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import xarray as xr\n", | ||
"import pint_xarray" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fuzzy-maintenance", | ||
"metadata": {}, | ||
"source": [ | ||
"## load the data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "proved-racing", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"ds = xr.tutorial.open_dataset(\"air_temperature\")\n", | ||
"data = ds.air\n", | ||
"data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "medium-backup", | ||
"metadata": {}, | ||
"source": [ | ||
"## convert units into a format understood by pint\n", | ||
"\n", | ||
"<div class=\"alert alert-info\">\n", | ||
"<strong>Note:</strong> this example uses the data provided by the <code>xarray.tutorial</code> functions. As such, the <code>units</code> attributes follow the CF conventions, which <code>pint</code> does not understand by default. To work around that, we are modifying the <code>units</code> attributes here, but in general it is better to use a library that adds support for the units used by the CF conventions to <code>pint</code>.\n", | ||
"</div>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "published-powell", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data.lat.attrs[\"units\"] = \"degree\"\n", | ||
"data.lon.attrs[\"units\"] = \"degree\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "banned-tolerance", | ||
"metadata": {}, | ||
"source": [ | ||
"## quantify the data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "divine-boost", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"quantified = data.pint.quantify()\n", | ||
"quantified" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "whole-momentum", | ||
"metadata": {}, | ||
"source": [ | ||
"## work with the data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "dried-friday", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"monthly_means = (\n", | ||
" quantified\n", | ||
" .pint.to(\"degC\")\n", | ||
" .sel(time=\"2013\")\n", | ||
" .groupby(\"time.month\").mean()\n", | ||
")\n", | ||
"monthly_means" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "still-ebony", | ||
"metadata": {}, | ||
"source": [ | ||
"## plot\n", | ||
"\n", | ||
"`xarray`'s plotting functions will cast the data to `numpy.ndarray`, so we need to \"dequantify\" first." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "united-machine", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"monthly_means.pint.dequantify(format=\"~P\").plot.imshow(col=\"month\", col_wrap=4)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,6 @@ python: | |
- requirements: docs/requirements.txt | ||
- method: pip | ||
path: . | ||
|
||
sphinx: | ||
fail_on_warning: true |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 it would be totally fine to modify
xarray.plot.utils.label_from_attrs
to do this for pint arrays with some reasonable-default choice of format.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.
indeed. However, I would like to keep the tight coupling to a minimum. Maybe hooks or entrypoints would help?
For now, I think asking people to
dequantify
before plotting / saving the data is fine.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.
@keewis I feel like this is an example of "you ain't gonna need it". I would be in favour of special-casing
xarray.plot.utils.label_from_attrs
until such a time that we actually need an entrypoint for other array libraries. After all, xarray's plotting methods already special-case dask arrays by calling.compute()
, it's not that big of a deal if they call.dequantify()
too, surely?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.
The issue with hard-coding is that we do expect more duck arrays in the future (e.g.
pytorch
and maybeuncertainties
), and adding special cases for all of them would quickly become impossible.I was hoping to make use of
matplotlib.units
for this. For example, using:would be pretty nice. However, while investigating #91 I found that right now the
xarray
plotting code (sometimes? always?) converts tonp.ma.masked_array
so we can't use this, yet. Additionally, this would bematplotlib
specific, so once we add the plotting entrypoints we would need to find something new.Which makes me wonder: should we introduce hooks based on the type of
.data
to preparexarray
objects for plotting? I think these hooks could be used to callcupy.Array.get
,sparse.COO.todense
orda.Array.compute
before plotting, and because the argument to the hook would be axarray
object we could also have the hook forpint
call.pint.dequantify
. I'll open a new issue on thexarray
issue tracker to see if I'm missing anything.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 know, I'm not suggesting we special-case every combination forever, what I'm saying is that we don't always need to make something totally general right at the start. If we can make this work for pint and dask quickly I think we should, and worry about entrypoints when other duck-array libraries get to a similar point in their integration. We're not introducing much technical debt because this would be a small change for us and no external API change for the user (we will always want
.plot()
to work bare, however it is implemented behind the scenes).This would be pretty nice. But again, I think we should make what we have work, then improve it later.
I think this is a good idea for a long-term solution, which deserves it's own issue.
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 pretty much already happens in
.values
hereit's just hardcoded instead of an entrypoint.
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 implemented this in pydata/xarray#5561