Skip to content

Commit cdbfefe

Browse files
committed
improve examples
1 parent 32ca251 commit cdbfefe

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

doc/python/imshow.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ import plotly.express as px
213213
from skimage import data
214214
img = data.astronaut()
215215
# Increase contrast by clipping the data range between 50 and 200
216-
fig = px.imshow(img, zmin=50, zmax=200, binary_string=False)
216+
fig = px.imshow(img, zmin=50, zmax=200)
217217
# We customize the hovertemplate to show both the data and the color values
218218
# See https://plotly.com/python/hover-text-and-formatting/#customize-tooltip-text-with-a-hovertemplate
219-
fig.update_traces(hovertemplate="x: %{x} <br> y: %{y} <br> z: %{z} <br> color: %{color}")
219+
#fig.update_traces(hovertemplate="x: %{x} <br> y: %{y} <br> z: %{z} <br> color: %{color}")
220220
fig.show()
221221
```
222222

@@ -355,6 +355,36 @@ fig = px.imshow(img, binary_string=True)
355355
fig.show()
356356
```
357357

358+
### Contrast rescaling im imshow with binary string
359+
360+
When the image is passed to the plotly figure as a binary string (which is the default mode for RGB images), and when the image is rescaled to adjust the contrast (for example when setting `zmin` and `zmax`), the original intensity values are not passed to the plotly figure and therefore no intensity value is displayed in the hover.
361+
362+
```python
363+
import plotly.express as px
364+
from skimage import data
365+
import numpy as np
366+
img = np.arange(100).reshape((10, 10))
367+
fig = px.imshow(img, binary_string=True)
368+
# You can check that only x and y are displayed in the hover
369+
# You can use a hovertemplate to override the hover information
370+
# See https://plotly.com/python/hover-text-and-formatting/#customize-tooltip-text-with-a-hovertemplate
371+
fig.show()
372+
```
373+
374+
You can set `binary_string=False` if you want the intensity value to appear in the hover even for a rescaled image. In the example below we also modify the hovertemplate to display both `z` (the data of the original image array) and `color` (the pixel value displayed in the figure).
375+
376+
```python
377+
import plotly.express as px
378+
from skimage import data
379+
img = data.chelsea()
380+
# Increase contrast by clipping the data range between 50 and 200
381+
fig = px.imshow(img, binary_string=False, zmin=50, zmax=200)
382+
# We customize the hovertemplate to show both the data and the color values
383+
# See https://plotly.com/python/hover-text-and-formatting/#customize-tooltip-text-with-a-hovertemplate
384+
fig.update_traces(hovertemplate="x: %{x} <br> y: %{y} <br> z: %{z} <br> color: %{color}")
385+
fig.show()
386+
```
387+
358388
### Changing the level of compression of the binary string in `px.imshow`
359389

360390
The `binary_compression_level` parameter controls the level of compression to be used by the backend creating the png string. Two different backends can be used, `pypng` (which is a dependency of `plotly` and is therefore always available), and `pil` for Pillow, which is often more performant. The compression level has to be between 0 (no compression) and 9 (highest compression), although increasing the compression above 4 and 5 usually only offers diminishing returns (no significant compression gain, at the cost of a longer execution time).

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ def _array_to_b64str(img, backend="pil", compression=4):
7979
return base64_string
8080

8181

82-
def _vectorize_zvalue(z, mode='max'):
83-
alpha = 255 if mode == 'max' else 0
82+
def _vectorize_zvalue(z, mode="max"):
83+
alpha = 255 if mode == "max" else 0
8484
if z is None:
8585
return z
8686
elif np.isscalar(z):
@@ -383,7 +383,10 @@ def imshow(
383383
elif img.ndim == 3 and img.shape[-1] in [3, 4] or (img.ndim == 2 and binary_string):
384384
rescale_image = True # to check whether image has been modified
385385
if zmin is not None and zmax is not None:
386-
zmin, zmax = _vectorize_zvalue(zmin, mode='min'), _vectorize_zvalue(zmax, mode='max')
386+
zmin, zmax = (
387+
_vectorize_zvalue(zmin, mode="min"),
388+
_vectorize_zvalue(zmax, mode="max"),
389+
)
387390
if binary_string:
388391
if zmin is None and zmax is None: # no rescaling, faster
389392
img_rescaled = img
@@ -393,7 +396,6 @@ def imshow(
393396
img, in_range=(zmin[0], zmax[0]), out_range=np.uint8
394397
)
395398
else:
396-
print(zmin, zmax)
397399
img_rescaled = np.dstack(
398400
[
399401
rescale_intensity(
@@ -433,7 +435,7 @@ def imshow(
433435
fig = go.Figure(data=trace, layout=layout)
434436
fig.update_layout(layout_patch)
435437
# Hover name, z or color
436-
if binary_string and rescale_image and not (img_rescaled.max() == img.max()):
438+
if binary_string and rescale_image and not np.all(img == img_rescaled):
437439
# we rescaled the image, hence z is not displayed in hover since it does
438440
# not correspond to img values
439441
hovertemplate = "%s: %%{x}<br>%s: %%{y}<extra></extra>" % (

0 commit comments

Comments
 (0)