Skip to content

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

Merged
merged 8 commits into from
Nov 2, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions python/hovertemplate.md
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
Copy link
Contributor

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"?


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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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!

Copy link
Contributor

Choose a reason for hiding this comment

The 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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to use px.data.gapminder so that there's no need to download a file here?

A = []
B = []

for i in range(5):
A = {'target': df['continent'][[i]].unique()}
B.append(A)

data = [{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you define a Figure and a 'Scattertrace usinggoconstructors? Also usefig.showinstead ofpio.show`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that's our preference, but we can't use graph_objects if we wanna use transform! Using transform helps to show information nicely in hovertemplate... let me see what would be the work around

Copy link
Contributor

Choose a reason for hiding this comment

The 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 transform with this strange thing that we can't use go, or should we rather have people use px here (and hence do not have many examples with transform)?

'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()
```