Skip to content

Commit 85a47a7

Browse files
author
“mahdis-z”
committed
replacing transform example
1 parent 18ea22e commit 85a47a7

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

python/hover-text-and-formatting.md

Lines changed: 49 additions & 44 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
@@ -127,49 +127,54 @@ fig.show()
127127
### Format Hover Template
128128

129129
```python
130-
import plotly.io as pio
130+
import plotly.graph_objects as go
131131
import plotly.express as px
132+
import pandas as pd
133+
import math
134+
135+
data = px.data.gapminder()
136+
df_2007 = data[data['year']==2007]
137+
df_2007 = df_2007.sort_values(['continent', 'country'])
138+
139+
bubble_size = []
132140

133-
df = px.data.gapminder()
134-
A = []
135-
B = []
136-
137-
for i in range(5):
138-
A = {'target': df['continent'][[i]].unique()}
139-
B.append(A)
140-
141-
data = [{
142-
'type': 'scatter',
143-
'mode': 'markers',
144-
'x': df['lifeExp'],
145-
'y': df['gdpPercap'],
146-
'text': df['continent'],
147-
'hovertemplate':
148-
"<b>%{text}</b><br><br>" +
149-
"GDP per Capita: %{y:$,.0f}<br>" +
150-
"Life Expectation: %{x:.0%}<br>" +
151-
"Population: %{marker.size:,}" +
152-
"<extra></extra>",
153-
'marker': {
154-
'size': df['pop'],
155-
'sizemode': 'area',
156-
'sizeref': 200000
157-
},
158-
'transforms': [{
159-
'type': 'filter',
160-
'target': df['year'],
161-
'orientation': '=',
162-
'value': 2002
163-
},{
164-
'type': 'groupby',
165-
'groups': df['continent'],
166-
'styles': B
167-
}]
168-
}]
169-
170-
layout = {'yaxis': {'type': 'log'}}
171-
172-
pio.show({'data': data, 'layout': layout}, validate=False)
141+
for index, row in df_2007.iterrows():
142+
bubble_size.append(math.sqrt(row['pop']))
143+
144+
df_2007['size'] = bubble_size
145+
continent_names = ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania']
146+
continent_data = {continent:df_2007.query("continent == '%s'" %continent)
147+
for continent in continent_names}
148+
149+
fig = go.Figure()
150+
151+
for continent_name, continent in continent_data.items():
152+
fig.add_trace(go.Scatter(
153+
x=continent['gdpPercap'],
154+
y=continent['lifeExp'],
155+
name=continent_name,
156+
text=df_2007['continent'],
157+
hovertemplate=
158+
"<b>%{text}</b><br><br>" +
159+
"GDP per Capita: %{y:$,.0f}<br>" +
160+
"Life Expectation: %{x:.0%}<br>" +
161+
"Population: %{marker.size:,}" +
162+
"<extra></extra>",
163+
marker_size=continent['size'],
164+
))
165+
166+
fig.update_traces(
167+
mode='markers',
168+
marker={'sizemode':'area',
169+
'sizeref':10})
170+
171+
fig.update_layout(
172+
xaxis={
173+
'title':'GDP per capita',
174+
'type':'log'},
175+
yaxis={'title':'Life Expectancy (years)'})
176+
177+
fig.show()
173178
```
174179

175180
### Set Hover Template in Mapbox
@@ -199,4 +204,4 @@ fig.show()
199204
```
200205

201206
#### Reference
202-
See https://plot.ly/python/reference/ for more information and chart attribute options!
207+
See https://plot.ly/python/reference/ for more information and chart attribute options!

0 commit comments

Comments
 (0)