You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/python/hover-text-and-formatting.md
+119-43
Original file line number
Diff line number
Diff line change
@@ -5,8 +5,8 @@ jupyter:
5
5
text_representation:
6
6
extension: .md
7
7
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
10
10
kernelspec:
11
11
display_name: Python 3
12
12
language: python
@@ -20,7 +20,7 @@ jupyter:
20
20
name: python
21
21
nbconvert_exporter: python
22
22
pygments_lexer: ipython3
23
-
version: 3.7.3
23
+
version: 3.6.8
24
24
plotly:
25
25
description: How to use hover text and formatting in Python with Plotly.
26
26
display_as: file_settings
@@ -32,82 +32,100 @@ jupyter:
32
32
thumbnail: thumbnail/hover-text.png
33
33
---
34
34
35
-
####Hover text with Plotly Express
35
+
### Hover Labels
36
36
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.
38
38
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:
fig = px.line(df, x="year", y="lifeExp", color="country", title="layout.hovermode='closest' (the default)")
47
+
fig.update_traces(mode="markers+lines")
48
48
49
49
fig.show()
50
50
```
51
51
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:
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:
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`.
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.
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.
107
123
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).
108
124
Hovertemplate customize the tooltip text vs. [texttemplate](https://plotly.com/python/reference/#pie-texttemplate) which customizes the text that appears on your chart. <br>
109
125
Set the horizontal alignment of the text within tooltip with [hoverlabel.align](https://plotly.com/python/reference/#layout-hoverlabel-align).
110
126
127
+
Plotly Express automatically sets the `hovertemplate`, but you can set it manually when using `graph_objects`.
128
+
111
129
```python
112
130
import plotly.graph_objects as go
113
131
@@ -231,7 +249,7 @@ fig.update_layout(title_text='Hover to see the value of z1, z2 and z3 together')
231
249
fig.show()
232
250
```
233
251
234
-
### Set Hover Template in Mapbox
252
+
### Setting the Hover Template in Mapbox Maps
235
253
236
254
```python
237
255
import plotly.graph_objects as go
@@ -258,6 +276,64 @@ fig.update_layout(
258
276
fig.show()
259
277
```
260
278
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.
0 commit comments