Skip to content

Commit b2ee809

Browse files
committed
imshow tutorial: add xarray example
1 parent 4954663 commit b2ee809

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/python/imshow.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,32 @@ fig.update_layout(coloraxis_showscale=False)
110110
fig.show()
111111
```
112112

113+
### Display an xarray image with px.imshow
114+
115+
[xarrays](http://xarray.pydata.org/en/stable/) are labeled arrays (with labeled axes and coordinates). If you pass an xarray image to `px.imshow`, its axes labels and coordinates will be used for ticks. (If you don't want this behavior, just pass `img.values` which is a NumPy array if `img` is an xarray).
116+
117+
```python
118+
import plotly.express as px
119+
import xarray as xr
120+
# Load xarray from dataset included in the xarray tutorial
121+
# We remove 273.5 to display Celsius degrees instead of Kelvin degrees
122+
airtemps = xr.tutorial.open_dataset('air_temperature').air.isel(lon=20) - 273.5
123+
fig = px.imshow(airtemps.T, color_continuous_scale='RdBu_r', origin='lower')
124+
fig.show()
125+
```
126+
127+
### Display an xarray image with square pixels
128+
129+
For xarrays, by default `px.imshow` does not constrain pixels to be square, since axes often correspond to different physical quantities (e.g. time and space), contrary to a plain camera image where pixels are square (most of the time). If you want to impose square pixels, set the parameter `aspect` to "equal" as below.
130+
131+
```python
132+
import plotly.express as px
133+
import xarray as xr
134+
airtemps = xr.tutorial.open_dataset('air_temperature').air.isel(time=500) - 273.5
135+
fig = px.imshow(airtemps, color_continuous_scale='RdBu_r', aspect='equal')
136+
fig.show()
137+
```
138+
113139
### Display multichannel image data with go.Image
114140

115141
It is also possible to use the `go.Image` trace from the low-level `graph_objects` API in order to display image data. Note that `go.Image` only accepts multichannel images. For single images, use [`go.Heatmap`](/python/heatmaps).

0 commit comments

Comments
 (0)