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
Prev Previous commit
Next Next commit
replacing transform example
  • Loading branch information
“mahdis-z” committed Nov 1, 2019
commit 85a47a73f6df8fc02c4b200df49eaf67bd3b5201
93 changes: 49 additions & 44 deletions python/hover-text-and-formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jupyter:
extension: .md
format_name: markdown
format_version: '1.1'
jupytext_version: 1.1.7
jupytext_version: 1.2.1
kernelspec:
display_name: Python 3
language: python
Expand All @@ -20,7 +20,7 @@ jupyter:
name: python
nbconvert_exporter: python
pygments_lexer: ipython3
version: 3.6.5
version: 3.7.3
plotly:
description: How to use hover text and formatting in Python with Plotly.
display_as: file_settings
Expand Down Expand Up @@ -127,49 +127,54 @@ fig.show()
### Format Hover Template

```python
import plotly.io as pio
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import math

data = px.data.gapminder()
df_2007 = data[data['year']==2007]
df_2007 = df_2007.sort_values(['continent', 'country'])

bubble_size = []

df = px.data.gapminder()
A = []
B = []

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

data = [{
'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)
for index, row in df_2007.iterrows():
bubble_size.append(math.sqrt(row['pop']))

df_2007['size'] = bubble_size
continent_names = ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
continent_data = {continent:df_2007.query("continent == '%s'" %continent)
for continent in continent_names}

fig = go.Figure()

for continent_name, continent in continent_data.items():
fig.add_trace(go.Scatter(
x=continent['gdpPercap'],
y=continent['lifeExp'],
name=continent_name,
text=df_2007['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=continent['size'],
))

fig.update_traces(
mode='markers',
marker={'sizemode':'area',
'sizeref':10})

fig.update_layout(
xaxis={
'title':'GDP per capita',
'type':'log'},
yaxis={'title':'Life Expectancy (years)'})

fig.show()
```

### Set Hover Template in Mapbox
Expand Down Expand Up @@ -199,4 +204,4 @@ fig.show()
```

#### Reference
See https://plot.ly/python/reference/ for more information and chart attribute options!
See https://plot.ly/python/reference/ for more information and chart attribute options!