-
Notifications
You must be signed in to change notification settings - Fork 1
hovertemplate.py #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hovertemplate.py #154
Changes from 1 commit
bfaf23d
234fb0a
f072031
18ea22e
85a47a7
1e62371
b9700e6
ceeaf7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
--- | ||
jupyter: | ||
jupytext: | ||
notebook_metadata_filter: all | ||
text_representation: | ||
extension: .md | ||
format_name: markdown | ||
format_version: '1.1' | ||
jupytext_version: 1.2.1 | ||
kernelspec: | ||
display_name: Python 3 | ||
language: python | ||
name: python3 | ||
language_info: | ||
codemirror_mode: | ||
name: ipython | ||
version: 3 | ||
file_extension: .py | ||
mimetype: text/x-python | ||
name: python | ||
nbconvert_exporter: python | ||
pygments_lexer: ipython3 | ||
version: 3.7.3 | ||
plotly: | ||
description: How to use hover template in Python with Plotly. | ||
display_as: file_settings | ||
has_thumbnail: true | ||
ipynb: ~notebook_demo/60 | ||
language: python | ||
layout: base | ||
name: Hover Template | ||
order: 40 | ||
page_type: u-guide | ||
permalink: python/hovertemplate/ | ||
thumbnail: thumbnail/hovertemplate.jpg | ||
title: Hover Template and Formatting| plotly | ||
v4upgrade: true | ||
--- | ||
|
||
### Add Hover Template to Pie Chart | ||
|
||
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. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a see also link to the tutorial on texttemplate you just wrote! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please explain here how to write the template string: which labels can be used, which formatting etc |
||
```python | ||
import plotly.graph_objects as go | ||
|
||
fig = go.Figure(go.Pie( | ||
name = "", | ||
values = [2, 5, 3, 2.5], | ||
labels = ["R", "Python", "Java Script", "Matlab"], | ||
text = ["textA", "TextB", "TextC", "TextD"], | ||
hovertemplate = "%{label}: <br>Popularity: %{percent} </br> %{text}" | ||
)) | ||
|
||
fig.show() | ||
``` | ||
|
||
### Format Hover Template | ||
|
||
```python | ||
import plotly.io as pio | ||
import pandas as pd | ||
|
||
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to use |
||
A = [] | ||
B = [] | ||
|
||
for i in range(5): | ||
A = {'target': df['continent'][[i]].unique()} | ||
B.append(A) | ||
|
||
data = [{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you define a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah that's our preference, but we can't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for the explanation, now I understand better. We might need to ask @nicolaskruchten for advice here: do we want to insist of |
||
'type': 'scatter', | ||
'mode': 'markers', | ||
'x': df['lifeExp'], | ||
'y': df['gdpPercap'], | ||
'text': df['continent'], | ||
'hovertemplate': | ||
"<b>%{text}</b><br><br>" + | ||
"GDP per Capita: %{y:$,.0f}<br>" + | ||
"Life Expectation: %{x:.0%}<br>" + | ||
"Population: %{marker.size:,}" + | ||
"<extra></extra>", | ||
'marker': { | ||
'size': df['pop'], | ||
'sizemode': 'area', | ||
'sizeref': 200000 | ||
}, | ||
'transforms': [{ | ||
'type': 'filter', | ||
'target': df['year'], | ||
'orientation': '=', | ||
'value': 2002 | ||
},{ | ||
'type': 'groupby', | ||
'groups': df['continent'], | ||
'styles': B | ||
}] | ||
}] | ||
|
||
layout = {'yaxis': {'type': 'log'}} | ||
|
||
pio.show({'data': data, 'layout': layout}, validate=False) | ||
``` | ||
|
||
### Set Hover Template to Mapbox | ||
|
||
```python | ||
import plotly.graph_objects as go | ||
|
||
token = open(".mapbox_token").read() # you need your own token | ||
|
||
fig = go.Figure(go.Scattermapbox( | ||
name = "", | ||
mode = "markers+text+lines", | ||
lon = [-75, -80, -50], | ||
lat = [45, 20, -20], | ||
marker = {'size': 20, 'symbol': ["bus", "harbor", "airport"]}, | ||
hovertemplate = | ||
"<b>%{marker.symbol} </b><br><br>" + | ||
"longitude: %{lon}<br>" + | ||
"latitude: %{lat}<br>" )) | ||
|
||
fig.update_layout( | ||
mapbox = { | ||
'accesstoken': token, | ||
'style': "outdoors", 'zoom': 1}, | ||
showlegend = False) | ||
|
||
fig.show() | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you change the title to something more general about hovertemplate like "Customize tooltip text with a hovertemplate"?