6
6
extension : .md
7
7
format_name : markdown
8
8
format_version : ' 1.1'
9
- jupytext_version : 1.1.7
9
+ jupytext_version : 1.2.1
10
10
kernelspec :
11
11
display_name : Python 3
12
12
language : python
@@ -20,7 +20,7 @@ jupyter:
20
20
name : python
21
21
nbconvert_exporter : python
22
22
pygments_lexer : ipython3
23
- version : 3.6.5
23
+ version : 3.7.3
24
24
plotly :
25
25
description : How to use hover text and formatting in Python with Plotly.
26
26
display_as : file_settings
@@ -127,49 +127,54 @@ fig.show()
127
127
### Format Hover Template
128
128
129
129
``` python
130
- import plotly.io as pio
130
+ import plotly.graph_objects as go
131
131
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 = []
132
140
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()
173
178
```
174
179
175
180
### Set Hover Template in Mapbox
@@ -199,4 +204,4 @@ fig.show()
199
204
```
200
205
201
206
#### 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