Skip to content

Commit ae984b0

Browse files
committed
added labels to imshow
1 parent 1621271 commit ae984b0

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

doc/python/imshow.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,15 @@ fig.show()
112112

113113
### Display an xarray image with px.imshow
114114

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).
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 axis titles. If you don't want this behavior, you can pass `img.values` which is a NumPy array if `img` is an xarray. Alternatively, you can override axis titles hover labels and colorbar title using the `labels` attribute, as below.
116116

117117
```python
118118
import plotly.express as px
119119
import xarray as xr
120120
# Load xarray from dataset included in the xarray tutorial
121-
# We remove 273.5 to display Celsius degrees instead of Kelvin degrees
122121
airtemps = xr.tutorial.open_dataset('air_temperature').air.sel(lon=250.0)
123-
#airtemps.attrs['long_name'] = 'Temperature' # used for hover
124-
fig = px.imshow(airtemps.T, color_continuous_scale='RdBu_r', origin='lower')
122+
fig = px.imshow(airtemps.T, color_continuous_scale='RdBu_r', origin='lower',
123+
labels={'colorbar':airtemps.attrs['var_desc']})
125124
fig.show()
126125
```
127126

@@ -132,9 +131,10 @@ For xarrays, by default `px.imshow` does not constrain pixels to be square, sinc
132131
```python
133132
import plotly.express as px
134133
import xarray as xr
135-
airtemps = xr.tutorial.open_dataset('air_temperature').air.isel(time=500) - 273.5
136-
airtemps.attrs['long_name'] = 'Temperature' # used for hover
137-
fig = px.imshow(airtemps, color_continuous_scale='RdBu_r', aspect='equal')
134+
airtemps = xr.tutorial.open_dataset('air_temperature').air.isel(time=500)
135+
colorbar_title = airtemps.attrs['var_desc'] + '<br> (%s)'%airtemps.attrs['units']
136+
fig = px.imshow(airtemps, color_continuous_scale='RdBu_r', aspect='equal',
137+
labels={'colorbar':colorbar_title})
138138
fig.show()
139139
```
140140

packages/python/plotly/plotly/express/_imshow.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def imshow(
6868
zmin=None,
6969
zmax=None,
7070
origin=None,
71-
labels=None,
71+
labels={},
7272
color_continuous_scale=None,
7373
color_continuous_midpoint=None,
7474
range_color=None,
@@ -104,6 +104,14 @@ def imshow(
104104
position of the [0, 0] pixel of the image array, in the upper left or lower left
105105
corner. The convention 'upper' is typically used for matrices and images.
106106
107+
labels : dict with str keys and str values (default `{}`)
108+
Overrides names used in the figure for axis titles (keys ``x`` and ``y``),
109+
colorbar title (key ``colorbar``) and hover (key ``color``). The values
110+
should correspond to the desired label to be displayed. If ``img`` is an
111+
xarray, dimension names are used for axis titles, and long name for the
112+
colorbar title (unless overridden in ``labels``). Possible keys are:
113+
x, y, color and colorbar.
114+
107115
color_continuous_scale : str or list of str
108116
colormap used to map scalar data to colors (for a 2D image). This parameter is
109117
not used for RGB or RGBA images. If a string is provided, it should be the name
@@ -161,6 +169,7 @@ def imshow(
161169
args = locals()
162170
apply_default_cascade(args)
163171
img_is_xarray = False
172+
colorbar_title = ''
164173
if xarray_imported:
165174
if isinstance(img, xarray.DataArray):
166175
y_label, x_label = img.dims[0], img.dims[1]
@@ -173,8 +182,18 @@ def imshow(
173182
img_is_xarray = True
174183
if aspect is None:
175184
aspect = "auto"
176-
z_name = img.attrs["long_name"] if "long_name" in img.attrs else "z"
177-
#TODO if ...
185+
z_name = img.attrs["long_name"] if "long_name" in img.attrs else ""
186+
colorbar_title = z_name
187+
188+
if labels is not None:
189+
if 'x' in labels:
190+
y_label = labels['x']
191+
if 'y' in labels:
192+
y_label = labels['y']
193+
if 'color' in labels:
194+
z_name = labels['color']
195+
if 'colorbar' in labels:
196+
colorbar_title = labels['colorbar']
178197

179198
if not img_is_xarray:
180199
if aspect is None:
@@ -207,6 +226,7 @@ def imshow(
207226
cmid=color_continuous_midpoint,
208227
cmin=range_color[0],
209228
cmax=range_color[1],
229+
colorbar=dict(title=colorbar_title)
210230
)
211231

212232
# For 2D+RGB data, use Image trace

0 commit comments

Comments
 (0)