Skip to content

Commit 8282e0a

Browse files
committed
update tutorials + use long_name
1 parent 7563750 commit 8282e0a

File tree

3 files changed

+30
-31
lines changed

3 files changed

+30
-31
lines changed

doc/python/datashader.md

+10-25
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ jupyter:
55
text_representation:
66
extension: .md
77
format_name: markdown
8-
format_version: "1.2"
9-
jupytext_version: 1.3.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,10 +20,9 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.8
23+
version: 3.7.3
2424
plotly:
25-
description:
26-
How to use datashader to rasterize large datasets, and visualize
25+
description: How to use datashader to rasterize large datasets, and visualize
2726
the generated raster data with plotly.
2827
display_as: scientific
2928
language: python
@@ -98,32 +97,18 @@ fig.show()
9897
```
9998

10099
```python
101-
import plotly.graph_objects as go
100+
import plotly.express as px
102101
import pandas as pd
103102
import numpy as np
104103
import datashader as ds
105104
df = pd.read_parquet('https://raw.githubusercontent.com/plotly/datasets/master/2015_flights.parquet')
106105

107106
cvs = ds.Canvas(plot_width=100, plot_height=100)
108107
agg = cvs.points(df, 'SCHEDULED_DEPARTURE', 'DEPARTURE_DELAY')
109-
x = np.array(agg.coords['SCHEDULED_DEPARTURE'])
110-
y = np.array(agg.coords['DEPARTURE_DELAY'])
111-
112-
# Assign nan to zero values so that the corresponding pixels are transparent
113-
agg = np.array(agg.values, dtype=np.float)
114-
agg[agg<1] = np.nan
115-
116-
fig = go.Figure(go.Heatmap(
117-
z=np.log10(agg), x=x, y=y,
118-
hoverongaps=False,
119-
hovertemplate='Scheduled departure: %{x:.1f}h <br>Depature delay: %{y} <br>Log10(Count): %{z}',
120-
colorbar=dict(title='Count (Log)', tickprefix='1.e')))
121-
fig.update_xaxes(title_text='Scheduled departure')
122-
fig.update_yaxes(title_text='Departure delay')
108+
agg.values = np.log10(agg.values)
109+
agg.attrs['long_name'] = 'Log10(count)'
110+
fig = px.imshow(agg, origin='lower')
111+
fig.update_traces(hoverongaps=False)
112+
fig.update_layout(coloraxis_colorbar=dict(title='Count (Log)', tickprefix='1.e'))
123113
fig.show()
124-
125-
```
126-
127-
```python
128-
129114
```

doc/python/imshow.md

+2
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ import xarray as xr
120120
# Load xarray from dataset included in the xarray tutorial
121121
# We remove 273.5 to display Celsius degrees instead of Kelvin degrees
122122
airtemps = xr.tutorial.open_dataset('air_temperature').air.isel(lon=20) - 273.5
123+
airtemps.attrs['long_name'] = 'Temperature' # used for hover
123124
fig = px.imshow(airtemps.T, color_continuous_scale='RdBu_r', origin='lower')
124125
fig.show()
125126
```
@@ -132,6 +133,7 @@ For xarrays, by default `px.imshow` does not constrain pixels to be square, sinc
132133
import plotly.express as px
133134
import xarray as xr
134135
airtemps = xr.tutorial.open_dataset('air_temperature').air.isel(time=500) - 273.5
136+
airtemps.attrs['long_name'] = 'Temperature' # used for hover
135137
fig = px.imshow(airtemps, color_continuous_scale='RdBu_r', aspect='equal')
136138
fig.show()
137139
```

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

+18-6
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ def imshow(
7575
template=None,
7676
width=None,
7777
height=None,
78-
aspect=None
78+
aspect=None,
7979
):
8080
"""
8181
Display an image, i.e. data on a 2D regular raster.
8282
8383
Parameters
8484
----------
8585
86-
img: array-like image
86+
img: array-like image, or xarray
8787
The image data. Supported array shapes are
8888
8989
- (M, N): an image with scalar data. The data is visualized
@@ -153,6 +153,9 @@ def imshow(
153153
154154
In order to update and customize the returned figure, use
155155
`go.Figure.update_traces` or `go.Figure.update_layout`.
156+
157+
If an xarray is passed, dimensions names and coordinates are used for
158+
axes labels and ticks.
156159
"""
157160
args = locals()
158161
apply_default_cascade(args)
@@ -168,11 +171,12 @@ def imshow(
168171
y = img.coords[y_label]
169172
img_is_xarray = True
170173
if aspect is None:
171-
aspect = 'auto'
174+
aspect = "auto"
175+
z_name = img.attrs["long_name"] if "long_name" in img.attrs else "z"
172176

173177
if not img_is_xarray:
174178
if aspect is None:
175-
aspect = 'equal'
179+
aspect = "equal"
176180

177181
img = np.asanyarray(img)
178182

@@ -185,7 +189,7 @@ def imshow(
185189
trace = go.Heatmap(z=img, coloraxis="coloraxis1")
186190
autorange = True if origin == "lower" else "reversed"
187191
layout = dict(yaxis=dict(autorange=autorange))
188-
if aspect == 'equal':
192+
if aspect == "equal":
189193
layout["xaxis"] = dict(scaleanchor="y", constrain="domain")
190194
layout["yaxis"]["constrain"] = "domain"
191195
colorscale_validator = ColorscaleValidator("colorscale", "imshow")
@@ -228,7 +232,15 @@ def imshow(
228232
fig.update_layout(layout_patch)
229233
if img_is_xarray:
230234
if img.ndim <= 2:
231-
fig.update_traces(x=x, y=y)
235+
hovertemplate = (
236+
x_label
237+
+ ": %{x} <br>"
238+
+ y_label
239+
+ ": %{y} <br>"
240+
+ z_name
241+
+ " : %{z}<extra></extra>"
242+
)
243+
fig.update_traces(x=x, y=y, hovertemplate=hovertemplate)
232244
fig.update_xaxes(title_text=x_label)
233245
fig.update_yaxes(title_text=y_label)
234246
fig.update_layout(template=args["template"], overwrite=True)

0 commit comments

Comments
 (0)