Skip to content

Commit 1f71115

Browse files
authored
added example about customdata and hovertemplate (#1977)
1 parent a3844c4 commit 1f71115

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

doc/python/hover-text-and-formatting.md

Lines changed: 30 additions & 2 deletions
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.1'
9-
jupytext_version: 1.2.1
8+
format_version: '1.2'
9+
jupytext_version: 1.3.0
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -201,6 +201,34 @@ fig.update_layout(
201201
fig.show()
202202
```
203203

204+
### Adding other data to the hover with customdata and a hovertemplate
205+
206+
`go` traces have a `customdata` argument in which you can add an array, which outer dimensions should have the same dimensions as the plotted data. You can then use `customdata` inside a `hovertemplate` to display the value of customdata.
207+
208+
```python
209+
import plotly.graph_objects as go
210+
from plotly.subplots import make_subplots
211+
import numpy as np
212+
np.random.seed(0)
213+
z1, z2, z3 = np.random.random((3, 7, 7))
214+
customdata = np.dstack((z2, z3))
215+
fig = make_subplots(1, 2, subplot_titles=['z1', 'z2'])
216+
fig.add_trace(go.Heatmap(
217+
z=z1,
218+
customdata=np.dstack((z2, z3)),
219+
hovertemplate='<b>z1:%{z:.3f}</b><br>z2:%{customdata[0]:.3f} <br>z3: %{customdata[1]:.3f} ',
220+
coloraxis="coloraxis1", name=''),
221+
1, 1)
222+
fig.add_trace(go.Heatmap(
223+
z=z2,
224+
customdata=np.dstack((z1, z3)),
225+
hovertemplate='z1:%{customdata[0]:.3f} <br><b>z2:%{z:.3f}</b><br>z3: %{customdata[1]:.3f} ',
226+
coloraxis="coloraxis1", name=''),
227+
1, 2)
228+
fig.update_layout(title_text='Hover to see the value of z1, z2 and z3 together')
229+
fig.show()
230+
```
231+
204232
### Set Hover Template in Mapbox
205233
```python
206234
import plotly.graph_objects as go

0 commit comments

Comments
 (0)