Skip to content

Commit 172c0e6

Browse files
hover and date axis doc upgrades
1 parent e982ec6 commit 172c0e6

File tree

3 files changed

+238
-78
lines changed

3 files changed

+238
-78
lines changed

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

+119-43
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.0
8+
format_version: '1.2'
9+
jupytext_version: 1.3.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.7.3
23+
version: 3.6.8
2424
plotly:
2525
description: How to use hover text and formatting in Python with Plotly.
2626
display_as: file_settings
@@ -32,82 +32,100 @@ jupyter:
3232
thumbnail: thumbnail/hover-text.png
3333
---
3434

35-
#### Hover text with Plotly Express
35+
### Hover Labels
3636

37-
Many Plotly Express functions support configurable hover text. The `hover_data` argument accepts a list of column names to be added to the hover tooltip. The `hover_name` property controls which column is displayed in bold as the tooltip title.
37+
One of the most deceptively-power features of interactive visualization using Plotly is the ability for the user to reveal more information about a data point by moving their mouse cursort over the point and having a hover label appear.
3838

39-
Here is an example that creates a scatter plot using Plotly Express with custom hover data and a custom hover name.
39+
There are three hover modes available in Plotly. The default setting is `layout.hovermode='closest'`, wherein a single hover label appears for the point directly underneat the cursor:
4040

4141
```python
4242
import plotly.express as px
4343

44-
df_2007 = px.data.gapminder().query("year==2007")
44+
df = px.data.gapminder().query("continent=='Oceania'")
4545

46-
fig = px.scatter(df_2007, x="gdpPercap", y="lifeExp", log_x=True,
47-
hover_name="country", hover_data=["continent"])
46+
fig = px.line(df, x="year", y="lifeExp", color="country", title="layout.hovermode='closest' (the default)")
47+
fig.update_traces(mode="markers+lines")
4848

4949
fig.show()
5050
```
5151

52-
#### Add Hover Text
52+
If `layout.hovermode='x'` (or `'y'`), a single hover label appears per trace, for points at the same `x` (or `y`) value as the cursor. If multiple points in a given trace exist at the same coordinate, only one will get a hover label. In the line plot below we have forced markers to appear, to make it clearer what can be hovered over, and we have disabled the built-in Plotly Express `hovertemplate` by setting it to `None`, resulting in a more compact hover label per point:
5353

5454
```python
55-
import plotly.graph_objects as go
55+
import plotly.express as px
5656

57-
fig = go.Figure()
57+
df = px.data.gapminder().query("continent=='Oceania'")
5858

59-
fig.add_trace(go.Scatter(
60-
x=[1, 2, 3, 4, 5],
61-
y=[2, 1, 6, 4, 4],
62-
hovertext=["Text A", "Text B", "Text C", "Text D", "Text E"],
63-
hoverinfo="text",
64-
marker=dict(
65-
color="green"
66-
),
67-
showlegend=False
68-
))
59+
fig = px.line(df, x="year", y="lifeExp", color="country", title="layout.hovermode='x'")
60+
fig.update_traces(mode="markers+lines", hovertemplate=None)
61+
fig.update_layout(hovermode="x")
6962

7063
fig.show()
7164
```
7265

73-
#### Format Hover Text
66+
If `layout.hovermode='x unified'` (or `'y unified'`), a single hover label appear, describing one point per trace, for points at the same `x` (or `y`) value as the cursor. If multiple points in a given trace exist at the same coordinate, only one will get an entry in the hover label. In the line plot below we have forced markers to appear, to make it clearer what can be hovered over, and we have disabled the built-in Plotly Express `hovertemplate` by setting it to `None`, resulting in a more compact entry per point in the hover label:
7467

7568
```python
76-
import plotly.graph_objects as go
69+
import plotly.express as px
7770

71+
df = px.data.gapminder().query("continent=='Oceania'")
7872

79-
fig = go.Figure(go.Scatter(
80-
x=[1, 2, 3, 4, 5],
81-
y=[2.02825, 1.63728, 6.83839, 4.8485, 4.73463],
82-
hoverinfo="y",
83-
marker=dict(
84-
color="green"
85-
),
86-
showlegend=False
87-
))
73+
fig = px.line(df, x="year", y="lifeExp", color="country", title="layout.hovermode='x unified'")
74+
fig.update_traces(mode="markers+lines", hovertemplate=None)
75+
fig.update_layout(hovermode="x unified")
76+
77+
fig.show()
78+
```
79+
80+
### Customizing Hover Label Appearance
81+
82+
Hover label text and colors default to trace colors in hover modes other than `unified`, and can be globally set via the `layout.hoverlabel` attributes. Hover label appearance can also be controlled per trace in `<trace>.hoverlabel`.
83+
84+
```python
85+
import plotly.express as px
86+
87+
df = px.data.gapminder().query("continent=='Oceania'")
88+
89+
fig = px.line(df, x="year", y="lifeExp", color="country", title="Custom layout.hoverlabel formatting")
90+
fig.update_traces(mode="markers+lines")
8891

8992
fig.update_layout(
90-
title_text=("Set hover text formatting<br>" +
91-
"<a href= https://github.com/d3/d3-time-format/blob/master/README.md#locale_format>" +
92-
"https://github.com/d3/d3-time-format/blob/master/README.md#locale_format</a>"),
93-
title_font=dict(
94-
size=10
95-
),
93+
hoverlabel=dict(
94+
bgcolor="white",
95+
font_size=16,
96+
font_family="Rockwell"
97+
)
9698
)
9799

98-
fig.update_xaxes(zeroline=False)
99-
fig.update_yaxes(hoverformat=".2f")
100+
fig.show()
101+
```
102+
103+
### Customizing Hover text with Plotly Express
104+
105+
Plotly Express functions automatically add all the data being plotted (x, y, color etc) to the hover label. Many Plotly Express functions also support configurable hover text. The `hover_data` argument accepts a list of column names to be added to the hover tooltip. The `hover_name` property controls which column is displayed in bold as the tooltip title.
106+
107+
Here is an example that creates a scatter plot using Plotly Express with custom hover data and a custom hover name.
108+
109+
```python
110+
import plotly.express as px
111+
112+
df_2007 = px.data.gapminder().query("year==2007")
113+
114+
fig = px.scatter(df_2007, x="gdpPercap", y="lifeExp", log_x=True,
115+
hover_name="country", hover_data=["continent", "pop"])
100116

101117
fig.show()
102118
```
103119

104-
### Customize tooltip text with a hovertemplate
120+
### Customizing hover text with a hovertemplate
105121

106122
To customize the tooltip on your graph you can use [hovertemplate](https://plotly.com/python/reference/#pie-hovertemplate), which is a template string used for rendering the information that appear on hoverbox.
107123
This template string can include `variables` in %{variable} format, `numbers` in [d3-format's syntax](https://github.com/d3/d3-3.x-api-reference/blob/master/Formatting.md#d3_forma), and `date` in [d3-time-format's syntax](https://github.com/d3/d3-3.x-api-reference/blob/master/Time-Formatting.md#format).
108124
Hovertemplate customize the tooltip text vs. [texttemplate](https://plotly.com/python/reference/#pie-texttemplate) which customizes the text that appears on your chart. <br>
109125
Set the horizontal alignment of the text within tooltip with [hoverlabel.align](https://plotly.com/python/reference/#layout-hoverlabel-align).
110126

127+
Plotly Express automatically sets the `hovertemplate`, but you can set it manually when using `graph_objects`.
128+
111129
```python
112130
import plotly.graph_objects as go
113131

@@ -231,7 +249,7 @@ fig.update_layout(title_text='Hover to see the value of z1, z2 and z3 together')
231249
fig.show()
232250
```
233251

234-
### Set Hover Template in Mapbox
252+
### Setting the Hover Template in Mapbox Maps
235253

236254
```python
237255
import plotly.graph_objects as go
@@ -258,6 +276,64 @@ fig.update_layout(
258276
fig.show()
259277
```
260278

279+
### Controlling Hover Text with `graph_objects` and `hoverinfo`
280+
281+
Prior to the addition of `hovertemplate`, hover text was controlled via the now-deprecated `hoverinfo` attribute.
282+
283+
```python
284+
import plotly.graph_objects as go
285+
286+
fig = go.Figure()
287+
288+
fig.add_trace(go.Scatter(
289+
x=[1, 2, 3, 4, 5],
290+
y=[2, 1, 6, 4, 4],
291+
hovertext=["Text A", "Text B", "Text C", "Text D", "Text E"],
292+
hoverinfo="text",
293+
marker=dict(
294+
color="green"
295+
),
296+
showlegend=False
297+
))
298+
299+
fig.show()
300+
```
301+
302+
### Spike lines
303+
304+
Plotly supports "spike lines" which link a point to the axis on hover, and can be configured per axis.
305+
306+
```python
307+
import plotly.express as px
308+
309+
df = px.data.gapminder().query("continent=='Oceania'")
310+
311+
fig = px.line(df, x="year", y="lifeExp", color="country", title="Spike lines active")
312+
fig.update_traces(mode="markers+lines")
313+
314+
fig.update_xaxes(showspikes=True)
315+
fig.update_yaxes(showspikes=True)
316+
317+
fig.show()
318+
```
319+
320+
Spike lines can be styled per axis as well, and the cursor distance setting can be controlled via `layout.spikedistance`.
321+
322+
```python
323+
import plotly.express as px
324+
325+
df = px.data.gapminder().query("continent=='Oceania'")
326+
327+
fig = px.line(df, x="year", y="lifeExp", color="country", title="Styled Spike Lines")
328+
fig.update_traces(mode="markers+lines")
329+
330+
fig.update_xaxes(showspikes=True, spikecolor="green", spikesnap="cursor", spikemode="across")
331+
fig.update_yaxes(showspikes=True, spikecolor="orange", spikethickness=2)
332+
fig.update_layout(spikedistance=1000, hoverdistance=100)
333+
334+
fig.show()
335+
```
336+
261337
#### Reference
262338

263339
See https://plotly.com/python/reference/ for more information and chart attribute options!

0 commit comments

Comments
 (0)