Skip to content

Commit e078df4

Browse files
authored
Merge pull request #154 from plotly/hovertext_py
hovertemplate.py
2 parents bad012c + ceeaf7b commit e078df4

File tree

1 file changed

+126
-2
lines changed

1 file changed

+126
-2
lines changed

python/hover-text-and-formatting.md

Lines changed: 126 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.2.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 use hover text and formatting in Python with Plotly.
2626
display_as: file_settings
@@ -104,5 +104,129 @@ fig.update_yaxes(hoverformat=".2f")
104104
fig.show()
105105
```
106106

107+
### Customize tooltip text with a hovertemplate
108+
109+
To customize the tooltip on your graph you can use [hovertemplate](https://plot.ly/python/reference/#pie-hovertemplate), which is a template string used for rendering the information that appear on hoverbox.
110+
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-fomrat's syntax](https://github.com/d3/d3-3.x-api-reference/blob/master/Time-Formatting.md#format).
111+
Hovertemplate customize the tooltip text vs. [texttemplate](https://plot.ly/python/reference/#pie-texttemplate) which customizes the text that appears on your chart.
112+
113+
```python
114+
import plotly.graph_objects as go
115+
116+
fig = go.Figure(go.Scatter(
117+
x = [1,2,3,4,5],
118+
y = [2.02825,1.63728,6.83839,4.8485,4.73463],
119+
hovertemplate =
120+
'<i>Price</i>: $%{y:.2f}'+
121+
'<br><b>X</b>: %{x}<br>'+
122+
'<b>%{text}</b>',
123+
text = ['Custom text {}'.format(i + 1) for i in range(5)],
124+
showlegend = False))
125+
126+
fig.add_trace(go.Scatter(
127+
x = [1,2,3,4,5],
128+
y = [3.02825,2.63728,4.83839,3.8485,1.73463],
129+
hovertemplate = 'Price: %{y:$.2f}<extra></extra>',
130+
showlegend = False))
131+
132+
fig.update_layout(title = "Set hover text with hovertemplate")
133+
134+
fig.show()
135+
```
136+
137+
```python
138+
import plotly.graph_objects as go
139+
140+
fig = go.Figure(go.Pie(
141+
name = "",
142+
values = [2, 5, 3, 2.5],
143+
labels = ["R", "Python", "Java Script", "Matlab"],
144+
text = ["textA", "TextB", "TextC", "TextD"],
145+
hovertemplate = "%{label}: <br>Popularity: %{percent} </br> %{text}"
146+
))
147+
148+
fig.show()
149+
```
150+
151+
### Advanced Hover Template
152+
The following example shows how to format hover template. [Here](https://plot.ly/python/v3/hover-text-and-formatting/#dash-example) is an example to see how to format hovertemplate in Dash.
153+
154+
```python
155+
import plotly.graph_objects as go
156+
import plotly.express as px
157+
import pandas as pd
158+
import math
159+
160+
data = px.data.gapminder()
161+
df_2007 = data[data['year']==2007]
162+
df_2007 = df_2007.sort_values(['continent', 'country'])
163+
164+
bubble_size = []
165+
166+
for index, row in df_2007.iterrows():
167+
bubble_size.append(math.sqrt(row['pop']))
168+
169+
df_2007['size'] = bubble_size
170+
continent_names = ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
171+
continent_data = {continent:df_2007.query("continent == '%s'" %continent)
172+
for continent in continent_names}
173+
174+
fig = go.Figure()
175+
176+
for continent_name, continent in continent_data.items():
177+
fig.add_trace(go.Scatter(
178+
x=continent['gdpPercap'],
179+
y=continent['lifeExp'],
180+
name=continent_name,
181+
text=df_2007['continent'],
182+
hovertemplate=
183+
"<b>%{text}</b><br><br>" +
184+
"GDP per Capita: %{y:$,.0f}<br>" +
185+
"Life Expectation: %{x:.0%}<br>" +
186+
"Population: %{marker.size:,}" +
187+
"<extra></extra>",
188+
marker_size=continent['size'],
189+
))
190+
191+
fig.update_traces(
192+
mode='markers',
193+
marker={'sizemode':'area',
194+
'sizeref':10})
195+
196+
fig.update_layout(
197+
xaxis={
198+
'title':'GDP per capita',
199+
'type':'log'},
200+
yaxis={'title':'Life Expectancy (years)'})
201+
202+
fig.show()
203+
```
204+
205+
### Set Hover Template in Mapbox
206+
```python
207+
import plotly.graph_objects as go
208+
209+
token = open(".mapbox_token").read() # you need your own token
210+
211+
fig = go.Figure(go.Scattermapbox(
212+
name = "",
213+
mode = "markers+text+lines",
214+
lon = [-75, -80, -50],
215+
lat = [45, 20, -20],
216+
marker = {'size': 20, 'symbol': ["bus", "harbor", "airport"]},
217+
hovertemplate =
218+
"<b>%{marker.symbol} </b><br><br>" +
219+
"longitude: %{lon}<br>" +
220+
"latitude: %{lat}<br>" ))
221+
222+
fig.update_layout(
223+
mapbox = {
224+
'accesstoken': token,
225+
'style': "outdoors", 'zoom': 1},
226+
showlegend = False)
227+
228+
fig.show()
229+
```
230+
107231
#### Reference
108232
See https://plot.ly/python/reference/ for more information and chart attribute options!

0 commit comments

Comments
 (0)