Skip to content

Commit 4f3bc89

Browse files
emmanuellenicolaskruchten
authored andcommitted
new tutorial on displaying image data
1 parent 878d394 commit 4f3bc89

File tree

2 files changed

+191
-2
lines changed

2 files changed

+191
-2
lines changed

python/images.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.1'
9-
jupytext_version: 1.1.7
9+
jupytext_version: 1.1.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -20,7 +20,7 @@ jupyter:
2020
name: python
2121
nbconvert_exporter: python
2222
pygments_lexer: ipython3
23-
version: 3.6.5
23+
version: 3.7.3
2424
plotly:
2525
description: How to add images to charts as background images or logos.
2626
display_as: file_settings
@@ -34,6 +34,8 @@ jupyter:
3434

3535
#### Add a Background Image
3636

37+
In this page we explain how to add static, non-interactive images as background, logo or annotation images to a figure. For exploring image data in interactive charts, see the [tutorial on displaying image data](./imshow/).
38+
3739
```python
3840
import plotly.graph_objects as go
3941

python/imshow.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
notebook_metadata_filter: all
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.1'
9+
jupytext_version: 1.1.1
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
language_info:
15+
codemirror_mode:
16+
name: ipython
17+
version: 3
18+
file_extension: .py
19+
mimetype: text/x-python
20+
name: python
21+
nbconvert_exporter: python
22+
pygments_lexer: ipython3
23+
version: 3.7.3
24+
plotly:
25+
description: How to display image data in Python with Plotly.
26+
display_as: scientific
27+
has_thumbnail: true
28+
ipynb: ~notebook_demo/34
29+
language: python
30+
layout: base
31+
name: Imshow
32+
order: 3
33+
page_type: example_index
34+
permalink: python/imshow/
35+
redirect_from: python/imshow/
36+
thumbnail: thumbnail/imshow.jpg
37+
v4upgrade: true
38+
---
39+
40+
### Displaying RBG image data with px.imshow
41+
42+
`px.imshow` displays multichannel (RGB) or single-channel ("grayscale") image data.
43+
44+
```python
45+
import plotly.express as px
46+
import numpy as np
47+
img_rgb = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
48+
[[0, 255, 0], [0, 0, 255], [255, 0, 0]]
49+
], dtype=np.uint8)
50+
fig = px.imshow(img_rgb)
51+
fig.show()
52+
```
53+
54+
### Read image arrays from image files
55+
56+
In order to create a numerical array to be passed to `px.imshow`, you can use a third-party library like [PIL](https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.open), [scikit-image](https://scikit-image.org/docs/dev/user_guide/getting_started.html) or [opencv](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html). We show below how to open an image from a file with `skimage.io.imread`, and alternatively how to load a demo image from `skimage.data`.
57+
58+
```python
59+
import plotly.express as px
60+
from skimage import io
61+
img = io.imread('https://upload.wikimedia.org/wikipedia/commons/thumb/0/00/Crab_Nebula.jpg/240px-Crab_Nebula.jpg')
62+
fig = px.imshow(img)
63+
fig.show()
64+
```
65+
66+
```python
67+
import plotly.express as px
68+
from skimage import data
69+
img = data.astronaut()
70+
fig = px.imshow(img)
71+
fig.show()
72+
```
73+
74+
### Display single-channel 2D image as grayscale
75+
76+
For a 2D image, `px.imshows` uses a colorscale to map scalar data to colors. The default colorscale is `gray`, ie grayscale images.
77+
78+
```python
79+
import plotly.express as px
80+
import numpy as np
81+
img = np.arange(100).reshape((10, 10))
82+
fig = px.imshow(img)
83+
fig.show()
84+
```
85+
86+
### Choose the colorscale to display a single-channel image
87+
88+
For a 2D image, `px.imshows` uses a colorscale to map scalar data to colors. The default colorscale is `gray`, ie grayscale images.
89+
90+
```python
91+
import plotly.express as px
92+
import numpy as np
93+
img = np.arange(100).reshape((10, 10))
94+
fig = px.imshow(img, colorscale='Viridis')
95+
fig.show()
96+
```
97+
98+
### Display multichannel image data with go.Image
99+
100+
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`](https://plot.ly/python/heatmaps/).
101+
102+
Note that the `go.Image` trace is different from the `go.layout.Image` class, which can be used for [adding background images or logos to figures](./images/).
103+
104+
```python
105+
import plotly.graph_objects as go
106+
img_rgb = [[[255, 0, 0], [0, 255, 0], [0, 0, 255]],
107+
[[0, 255, 0], [0, 0, 255], [255, 0, 0]]]
108+
fig = go.Figure(go.Image(z=img_rgb))
109+
fig.show()
110+
```
111+
112+
### Defining the data range covered by the color range with zmin and zmax
113+
114+
The data range and color range are mapped together using the parameters `zmin` and `zmax`, which correspond respectively to the data values mapped to black `[0, 0, 0]` and white `[255, 255, 255]`, or to the extreme colors of the colorscale in the case on single-channel data.
115+
116+
`px.imshow` and `go.Image` use slightly different default values for `zmin` and `zmax`. For `go.Image`, the default value is `zmin=[0, 0, 0]` and `zmax=[255, 255, 255]`, no matter the data type. On the other hand, `px.imshow` adapts the default `zmin` and `zmax` to the data type:
117+
- for integer data types, `zmin` and `zmax` correspond to the extreme values of the data type, for example 0 and 255 for `uint8`, 0 and 65535 for `uint16`, etc.
118+
- for float numbers, when the data range is more arbitrary, the minimum and maximum values of the image (across all channels) is used.
119+
120+
These defaults can be overriden by setting the values of `zmin` and `zmax`. For `go.Image`, `zmin` and `zmax` need to be given for all channels, whereas it is also possible to pass a scalar value (used for all channels) go `px.imshow`.
121+
122+
```python
123+
import plotly.express as px
124+
from skimage import data
125+
img = data.astronaut()
126+
# Increase contrast by clipping the data range between 50 and 200
127+
fig = px.imshow(img, zmin=50, zmax=200)
128+
# We customize the hovertemplate to show both the data and the color values
129+
# See https://plot.ly/python/v3/hover-text-and-formatting/#hovertemplate
130+
fig.update_traces(hovertemplate="x: %{x} <br> y: %{y} <br> z: %{z} <br> color: %{color}")
131+
fig.show()
132+
```
133+
134+
```python
135+
import plotly.express as px
136+
from skimage import data
137+
img = data.astronaut()
138+
# Stretch the contrast of the red channel only, resulting in a more red image
139+
fig = px.imshow(img, zmin=[50, 0, 0], zmax=[200, 255, 255])
140+
fig.show()
141+
```
142+
143+
### Ticks and margins around image data
144+
145+
```python
146+
import plotly.express as px
147+
from skimage import data
148+
img = data.astronaut()
149+
fig = px.imshow(img, showticks=False)
150+
fig.update_layout(width=400, height=400, margin=dict(l=10, r=10, b=10, t=10))
151+
fig.show()
152+
```
153+
154+
### Combining image charts and other traces
155+
156+
```python
157+
import plotly.express as px
158+
import plotly.graph_objects as go
159+
from skimage import data
160+
img = data.camera()
161+
fig = px.imshow(img)
162+
fig.add_trace(go.Contour(z=img, showscale=False,
163+
contours=dict(start=0, end=70, size=70, coloring='lines'),
164+
line_width=2))
165+
fig.add_trace(go.Scatter(x=[230], y=[100], marker=dict(color='red', size=16)))
166+
fig.show()
167+
```
168+
169+
### Displaying an image and the histogram of color values
170+
171+
```python
172+
from plotly.subplots import make_subplots
173+
from skimage import data
174+
img = data.chelsea()
175+
fig = make_subplots(1, 2)
176+
# We use go.Image because at the moment px functions are not compatible with subplots
177+
fig.add_trace(go.Image(z=img), 1, 1)
178+
for channel, color in enumerate(['red', 'green', 'blue']):
179+
fig.add_trace(go.Histogram(x=img[..., channel].ravel(), opacity=0.5,
180+
marker_color=color, name='%s channel' %color), 1, 2)
181+
fig.update_layout(height=400)
182+
fig.show()
183+
```
184+
185+
#### Reference
186+
See https://plot.ly/python/reference/#image for more information and chart attribute options!
187+

0 commit comments

Comments
 (0)