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
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ One of the most deceptively-powerful features of interactive visualization using
38
38
39
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 underneath the cursor.
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:
Customize the title shown in unified hovermode, by specifing `unifiedhovertitle.text`.
90
+
Customize the title shown in unified hover mode, by specifing `unifiedhovertitle.text`.
91
91
92
92
The unified hover title is a template string that supports using variables from the data. Numbers are formatted using d3-format's syntax `%{variable:d3-format}`, for `example \"Price: %{y:$.2f}\"`. Dates are formatted using d3-time-format's syntax `%{variable|d3-time-format}`, for example `\"Day: %{2019-01-01|%A}\"`.
93
93
@@ -134,13 +134,13 @@ fig.show()
134
134
135
135
```
136
136
137
-
#### Control hovermode with Dash
137
+
#### Control Hover Mode with Dash
138
138
139
139
[Dash](https://plotly.com/dash/) is the best way to build analytical apps in Python using Plotly figures. To run the app below, run `pip install dash`, click "Download" to get the code and run `python app.py`.
140
140
141
141
Get started with [the official Dash docs](https://dash.plotly.com/installation) and **learn how to effortlessly [style](https://plotly.com/dash/design-kit/) & publish apps like this with <aclass="plotly-red"href="https://plotly.com/dash/">Dash Enterprise</a> or <aclass="plotly-red"href="https://plotly.com/cloud/">Plotly Cloud</a>.**
142
142
143
-
Change the hovermode below and try hovering over the points:
143
+
Change the hover mode below and try hovering over the points:
#### Selecting a hovermode in a figure created with `plotly.graph_objects`
155
+
#### Selecting a hover mode in a figure created with `plotly.graph_objects`
156
156
157
-
The hovermode is a property of the figure layout, so you can select a hovermode no matter how you created the figure, either with `plotly.express` or with `plotly.graph_objects`. Below is an example with a figure created with `plotly.graph_objects`. If you're not familiar with the structure of plotly figures, you can read [the tutorial on creating and updating plotly figures](/python/creating-and-updating-figures/).
157
+
The hover mode is a property of the figure layout, so you can select a hover mode no matter how you created the figure, either with `plotly.express` or with `plotly.graph_objects`. Below is an example with a figure created with `plotly.graph_objects`. If you're not familiar with the structure of plotly figures, you can read [the tutorial on creating and updating plotly figures](/python/creating-and-updating-figures/).
To customize the tooltip on your graph you can use the [hovertemplate](https://plotly.com/python/reference/pie/#pie-hovertemplate) attribute of `graph_objects` traces, which is a template string used for rendering the information that appear on hoverbox.
273
-
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_format), and `date` in [d3-time-format's syntax](https://github.com/d3/d3-time-format). In the example below, the empty `<extra></extra>` tag removes the part of the hover where the trace name is usually displayed in a contrasting color. The `<extra>` tag can be used to display other parts of the hovertemplate, it is not reserved for the trace name.
273
+
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_format), and `date` in [d3-time-format's syntax](https://github.com/d3/d3-time-format). In the example below, the empty `<extra></extra>` tag removes the part of the hover where the trace name is usually displayed in a contrasting color. The `<extra>` tag can be used to display other parts of the hover template, it is not reserved for the trace name.
274
274
275
-
Note that a hovertemplate customizes the tooltip text, while a [texttemplate](https://plotly.com/python/reference/pie/#pie-texttemplate) customizes the text that appears on your chart. <br>
275
+
Note that a hover template customizes the tooltip text, while a [texttemplate](https://plotly.com/python/reference/pie/#pie-texttemplate) customizes the text that appears on your chart. <br>
276
276
277
277
Set the horizontal alignment of the text within tooltip with [hoverlabel.align](https://plotly.com/python/reference/layout/#layout-hoverlabel-align).
278
278
@@ -297,7 +297,7 @@ fig.add_trace(go.Scatter(
297
297
298
298
fig.update_layout(
299
299
hoverlabel_align='right',
300
-
title="Set hover text with hovertemplate")
300
+
title="Set hover text with hover template")
301
301
302
302
fig.show()
303
303
```
@@ -316,9 +316,9 @@ fig = go.Figure(go.Pie(
316
316
fig.show()
317
317
```
318
318
319
-
### Modifying the hovertemplate of a plotly express figure
319
+
### Modifying the hover template of a plotly express figure
320
320
321
-
`plotly.express` automatically sets the hovertemplate but you can modify it using the `update_traces` method of the generated figure. It helps to print the hovertemplate generated by `plotly.express` in order to be able to modify it. One can also revert to the default hover information of traces by setting the hovertemplate to `None`.
321
+
`plotly.express` automatically sets the hover template but you can modify it using the `update_traces` method of the generated figure. It helps to print the hover template generated by `plotly.express` in order to be able to modify it. One can also revert to the default hover information of traces by setting the hover template to `None`.
print("user defined hover template:", fig.data[0].hovertemplate)
334
334
fig.show()
335
335
```
336
336
337
-
### Specifying the formatting and labeling of custom fields in a Plotly Express figure using a hovertemplate
337
+
### Specifying the formatting and labeling of custom fields in a Plotly Express figure using a hover template
338
338
339
339
This example adds custom fields to a Plotly Express figure using the `custom_data` parameter and then adds a hover template that applies d3 formats to each element of the `customdata[n]` array and uses HTML to customize the fonts and spacing.
340
340
@@ -357,13 +357,13 @@ fig=px.scatter(df,
357
357
y='Life Expectancy (years)',
358
358
color='continent',
359
359
size=np.sqrt(df['pop']),
360
-
# Specifying data to make available to the hovertemplate
360
+
# Specifying data to make available to the hover template
361
361
# The px custom_data parameter has an underscore, while the analogous graph objects customdata parameter has no underscore.
362
362
# The px custom_data parameter is a list of column names in the data frame, while the graph objects customdata parameter expects a data frame or a numpy array.
363
363
custom_data=['country', 'continent', 'pop'],
364
364
)
365
365
366
-
# Plotly express does not have a hovertemplate parameter in the graph creation function, so we apply the template with update_traces
366
+
# Plotly express does not have a hover template parameter in the graph creation function, so we apply the template with update_traces
367
367
fig.update_traces(
368
368
hovertemplate=
369
369
"<b>%{customdata[0]}</b><br>"+
@@ -390,7 +390,7 @@ fig.show()
390
390
391
391
*New in v5.0*
392
392
393
-
When [displaying periodic data](https://plotly.com/python/time-series/#displaying-period-data) with mixed-sized periods (i.e. quarterly and monthly) in conjunction with `x` or `x unified`hovermodes and using `hovertemplate`, the `xhoverformat` attribute can be used to control how each period's X value is displayed, and the special `%{xother}` hover-template directive can be used to control how the X value is displayed for points that do not share the exact X coordinate with the point that is being hovered on. `%{xother}` will return an empty string when the X value is the one being hovered on, otherwise it will return `(%{x})`. The special `%{_xother}`, `%{xother_}` and `%{_xother_}` variations will display with spaces before, after or around the parentheses, respectively.
393
+
When [displaying periodic data](https://plotly.com/python/time-series/#displaying-period-data) with mixed-sized periods (i.e. quarterly and monthly) in conjunction with `x` or `x unified`hover modes and using `hovertemplate`, the `xhoverformat` attribute can be used to control how each period's X value is displayed, and the special `%{xother}` hover-template directive can be used to control how the X value is displayed for points that do not share the exact X coordinate with the point that is being hovered on. `%{xother}` will return an empty string when the X value is the one being hovered on, otherwise it will return `(%{x})`. The special `%{_xother}`, `%{xother_}` and `%{_xother_}` variations will display with spaces before, after or around the parentheses, respectively.
394
394
395
395
```python
396
396
import plotly.graph_objects as go
@@ -422,7 +422,7 @@ fig.show()
422
422
423
423
### Advanced Hover Template
424
424
425
-
This produces the same graphic as in "Specifying the formatting and labeling of custom fields in a Plotly Express figure using a hovertemplate" above, but does so with the `customdata` and `text` parameters of `graph_objects`. It shows how to specify columns from a dataframe to include in the `customdata` array using the `df[["col_i", "col_j"]]` subsetting notation. It then references those variables using e.g. `%{customdata[0]}` in the hovertemplate. It includes comments about major differences between the parameters used by `graph_objects` and `plotly.express`.
425
+
This produces the same graphic as in "Specifying the formatting and labeling of custom fields in a Plotly Express figure using a hover template" above, but does so with the `customdata` and `text` parameters of `graph_objects`. It shows how to specify columns from a dataframe to include in the `customdata` array using the `df[["col_i", "col_j"]]` subsetting notation. It then references those variables using e.g. `%{customdata[0]}` in the hover template. It includes comments about major differences between the parameters used by `graph_objects` and `plotly.express`.
426
426
427
427
```python
428
428
import plotly.graph_objects as go
@@ -458,7 +458,7 @@ for continent_name, df in continent_data.items():
458
458
# The next three parameters specify the hover text
459
459
# Text supports just one customized field per trace
460
460
# and is implemented here with text=df['continent'],
461
-
# Custom data supports multiple fields through numeric indices in the hovertemplate
461
+
# Custom data supports multiple fields through numeric indices in the hover template
462
462
# In we weren't using the text parameter in our example,
463
463
# we could instead add continent as a third customdata field.
464
464
customdata=df[['country','pop']],
@@ -486,7 +486,7 @@ fig.update_layout(
486
486
fig.show()
487
487
```
488
488
489
-
### Adding other data to the hover with customdata and a hovertemplate
489
+
### Adding other data to the hover with customdata and a hover template
490
490
491
491
`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.
0 commit comments