@@ -11,6 +11,16 @@ jupyter:
11
11
display_name :Python 3
12
12
language :python
13
13
name :python3
14
+ language_info :
15
+ codemirror_mode :
16
+ name :ipython
17
+ version :3
18
+ file_extension :.py
19
+ mimetype :text/x-python
20
+ name :python
21
+ nbconvert_exporter :python
22
+ pygments_lexer :ipython3
23
+ version :3.6.7
14
24
plotly :
15
25
description :How to make bubble maps in Python with Plotly.
16
26
display_as :maps
@@ -23,22 +33,36 @@ jupyter:
23
33
permalink :python/bubble-maps/
24
34
thumbnail :thumbnail/bubble-map.jpg
25
35
title :Python Bubble Maps | Plotly
36
+ v4upgrade :true
26
37
---
27
38
28
- ####New to Plotly?
29
- Plotly's Python library is free and open source![ Get started] ( https://plot.ly/python/getting-started/ ) by downloading the client and[ reading the primer] ( https://plot.ly/python/getting-started/ ) .
30
- <br >You can set up Plotly to work in[ online] ( https://plot.ly/python/getting-started/#initialization-for-online-plotting ) or[ offline] ( https://plot.ly/python/getting-started/#initialization-for-offline-plotting ) mode, or in[ jupyter notebooks] ( https://plot.ly/python/getting-started/#start-plotting-online ) .
31
- <br >We also have a quick-reference[ cheatsheet] ( https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf ) (new!) to help you get started!
39
+ ###Bubble map with plotly express
32
40
41
+ Plotly express functions take as argument a tidy[ pandas DataFrame] ( https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html ) . With`` px.scatter_geo `` , each line of the dataframe is represented as a marker point. The column set as the` size ` argument gives the size of markers.
33
42
34
- ####Version Check
35
- Plotly's python package is updated frequently. Run` pip install plotly --upgrade ` to use the latest version.
43
+ ``` python
44
+ import plotly.expressas px
45
+ gapminder= px.data.gapminder().query(" year==2007" )
46
+ fig= px.scatter_geo(gapminder,locations = " iso_alpha" ,color = " continent" ,
47
+ hover_name = " country" ,size = " pop" ,
48
+ projection = " natural earth" )
49
+ fig.show()
50
+ ```
51
+
52
+ ### Bubble Map with animation
36
53
37
54
``` python
38
- import plotly
39
- plotly.__version__
55
+ import plotly.expressas px
56
+ gapminder= px.data.gapminder()
57
+ fig= px.scatter_geo(gapminder,locations = " iso_alpha" ,color = " continent" ,
58
+ hover_name = " country" ,size = " pop" ,
59
+ animation_frame = " year" ,
60
+ projection = " natural earth" )
61
+ fig.show()
40
62
```
41
63
64
+ ###Bubble Map with go.Scattergeo
65
+
42
66
####United States Bubble Map
43
67
44
68
Note about` sizeref ` :
@@ -52,8 +76,7 @@ Note that setting `sizeref` to a value greater than $1$, decreases the rendered
52
76
Seehttps://plot.ly/python/reference/#scatter-marker-sizeref for more information. Additionally, we recommend setting the sizemode attribute:https://plot.ly/python/reference/#scatter-marker-sizemode to area.
53
77
54
78
``` python
55
- import plotly.plotlyas py
56
- import plotly.graph_objsas go
79
+ import plotly.graph_objectsas go
57
80
58
81
import pandasas pd
59
82
@@ -62,108 +85,95 @@ df.head()
62
85
63
86
df[' text' ]= df[' name' ]+ ' <br>Population' + (df[' pop' ]/ 1e6 ).astype(str )+ ' million'
64
87
limits= [(0 ,2 ),(3 ,10 ),(11 ,20 ),(21 ,50 ),(50 ,3000 )]
65
- colors= [" rgb(0,116,217) " ," rgb(255,65,54) " ," rgb(133,20,75) " ," rgb(255,133,27) " ," lightgrey" ]
88
+ colors= [" royalblue " ," crimson " ," lightseagreen " ," orange " ," lightgrey" ]
66
89
cities= []
67
90
scale= 5000
68
91
92
+ fig= go.Figure()
93
+
69
94
for iin range (len (limits)):
70
95
lim= limits[i]
71
96
df_sub= df[lim[0 ]:lim[1 ]]
72
- city = go.Scattergeo(
97
+ fig.add_trace( go.Scattergeo(
73
98
locationmode = ' USA-states' ,
74
99
lon = df_sub[' lon' ],
75
100
lat = df_sub[' lat' ],
76
101
text = df_sub[' text' ],
77
- marker = go.scattergeo.Marker (
102
+ marker = dict (
78
103
size = df_sub[' pop' ]/ scale,
79
104
color = colors[i],
80
- line = go.scattergeo.marker.Line(
81
- width = 0.5 ,color = ' rgb(40,40,40)'
82
- ),
105
+ line_color = ' rgb(40,40,40)' ,
106
+ line_width = 0.5 ,
83
107
sizemode = ' area'
84
108
),
85
- name = ' {0} -{1} ' .format(lim[0 ],lim[1 ]) )
86
- cities.append(city)
109
+ name = ' {0} -{1} ' .format(lim[0 ],lim[1 ])))
87
110
88
- layout= go.Layout(
89
- title = go.layout.Title(
90
- text = ' 2014 US city populations<br>(Click legend to toggle traces)'
91
- ),
111
+ fig.update_layout(
112
+ title_text = ' 2014 US city populations<br>(Click legend to toggle traces)' ,
92
113
showlegend = True ,
93
- geo = go.layout.Geo (
114
+ geo = dict (
94
115
scope = ' usa' ,
95
- projection = go.layout.geo.Projection(
96
- type = ' albers usa'
97
- ),
98
- showland = True ,
99
116
landcolor = ' rgb(217, 217, 217)' ,
100
- subunitwidth = 1 ,
101
- countrywidth = 1 ,
102
- subunitcolor = " rgb(255, 255, 255)" ,
103
- countrycolor = " rgb(255, 255, 255)"
104
117
)
105
118
)
106
119
107
- fig= go.Figure(data = cities,layout = layout)
108
- py.iplot(fig,filename = ' d3-bubble-map-populations' )
120
+ fig.show()
109
121
```
110
122
111
123
####Ebola Cases in West Africa
112
124
113
125
``` python
114
- import plotly.plotlyas py
115
- import plotly.graph_objsas go
126
+ import plotly.graph_objectsas go
116
127
117
128
import pandasas pd
118
129
119
130
df= pd.read_csv(' https://raw.githubusercontent.com/plotly/datasets/master/2014_ebola.csv' )
120
131
df.head()
121
132
122
- cases= []
123
133
colors= [' rgb(239,243,255)' ,' rgb(189,215,231)' ,' rgb(107,174,214)' ,' rgb(33,113,181)' ]
124
134
months= {6 :' June' ,7 :' July' ,8 :' Aug' ,9 :' Sept' }
125
135
136
+ fig= go.Figure()
137
+
126
138
for iin range (6 ,10 )[::- 1 ]:
127
- cases.append(go.Scattergeo(
128
- lon = df[ df[' Month' ]== i ][' Lon' ],# -(max(range(6,10))-i),
129
- lat = df[ df[' Month' ]== i ][' Lat' ],
130
- text = df[ df[' Month' ]== i ][' Value' ],
139
+ df_month= df.query(' Month ==%d ' % i)
140
+ fig.add_trace(go.Scattergeo(
141
+ lon = df_month[' Lon' ],
142
+ lat = df_month[' Lat' ],
143
+ text = df_month[' Value' ],
131
144
name = months[i],
132
- marker = go.scattergeo.Marker (
133
- size = df[ df[ ' Month ' ] == i ] [' Value' ]/ 50 ,
145
+ marker = dict (
146
+ size = df_month [' Value' ]/ 50 ,
134
147
color = colors[i- 6 ],
135
- line = go.scattergeo.marker.Line(width = 0 )
136
- )
137
- )
138
- )
148
+ line_width = 0
149
+ )))
139
150
140
- cases[ 0 ][ ' text ' ] = df[ df[ ' Month' ] == 9 ][ ' Value ' ].map( ' { :.0f } ' .format).astype( str ) + ' ' + \
141
- df[ df[ ' Month ' ] == 9 ][ ' Country ' ]
142
- cases[ 0 ][ ' mode ' ] = ' markers+text '
143
- cases[ 0 ][ ' textposition ' ] = ' bottom center '
151
+ df_sept = df.query( ' Month == 9' )
152
+ fig[ ' data ' ][ 0 ].update( mode = ' markers+text ' , textposition = ' bottom center ' ,
153
+ text = df_sept[ ' Value ' ].map( ' { :.0f } ' .format).astype( str ) + ' ' + \
154
+ df_sept[ ' Country ' ])
144
155
145
- inset = [
146
- go.Choropleth(
156
+ # Inset
157
+ fig.add_trace( go.Choropleth(
147
158
locationmode = ' country names' ,
148
- locations = df[ df[ ' Month ' ] == 9 ] [' Country' ],
149
- z = df[ df[ ' Month ' ] == 9 ] [' Value' ],
150
- text = df[ df[ ' Month ' ] == 9 ] [' Country' ],
159
+ locations = df_sept [' Country' ],
160
+ z = df_sept [' Value' ],
161
+ text = df_sept [' Country' ],
151
162
colorscale = [[0 ,' rgb(0, 0, 0)' ],[1 ,' rgb(0, 0, 0)' ]],
152
163
autocolorscale = False ,
153
164
showscale = False ,
154
165
geo = ' geo2'
155
- ),
156
- go.Scattergeo(
166
+ ))
167
+ fig.add_trace( go.Scattergeo(
157
168
lon = [21.0936 ],
158
169
lat = [7.1881 ],
159
170
text = [' Africa' ],
160
171
mode = ' text' ,
161
172
showlegend = False ,
162
173
geo = ' geo2'
163
- )
164
- ]
174
+ ))
165
175
166
- layout = go.Layout (
176
+ fig.update_layout (
167
177
title = go.layout.Title(
168
178
text = ' Ebola cases reported by month in West Africa 2014<br>\
169
179
Source: <a href="https://data.hdx.rwlabs.org/dataset/rowca-ebola-cases">\
@@ -173,67 +183,27 @@ HDX</a>'),
173
183
scope = ' africa' ,
174
184
showframe = False ,
175
185
showcoastlines = True ,
176
- showland = True ,
177
186
landcolor = " rgb(229, 229, 229)" ,
178
- countrycolor = " rgb(255, 255, 255)" ,
179
- coastlinecolor = " rgb(255, 255, 255)" ,
180
- projection = go.layout.geo.Projection(
181
- type = ' mercator'
182
- ),
183
- lonaxis = go.layout.geo.Lonaxis(
184
- range = [- 15.0 ,- 5.0 ]
185
- ),
186
- lataxis = go.layout.geo.Lataxis(
187
- range = [0.0 ,12.0 ]
188
- ),
189
- domain = go.layout.geo.Domain(
190
- x = [0 ,1 ],
191
- y = [0 ,1 ]
192
- )
187
+ countrycolor = " white" ,
188
+ coastlinecolor = " white" ,
189
+ projection_type = ' mercator' ,
190
+ lonaxis_range = [- 15.0 ,- 5.0 ],
191
+ lataxis_range = [0.0 ,12.0 ],
192
+ domain = dict (x = [0 ,1 ],y = [0 ,1 ])
193
193
),
194
194
geo2 = go.layout.Geo(
195
195
scope = ' africa' ,
196
196
showframe = False ,
197
- showland = True ,
198
197
landcolor = " rgb(229, 229, 229)" ,
199
198
showcountries = False ,
200
- domain = go.layout.geo.Domain(
201
- x = [0 ,0.6 ],
202
- y = [0 ,0.6 ]
203
- ),
199
+ domain = dict (x = [0 ,0.6 ],y = [0 ,0.6 ]),
204
200
bgcolor = ' rgba(255, 255, 255, 0.0)' ,
205
201
),
206
- legend = go.layout.Legend(
207
- traceorder = ' reversed'
208
- )
202
+ legend_traceorder = ' reversed'
209
203
)
210
204
211
- fig= go.Figure(layout = layout,data = cases+ inset)
212
- py.iplot(fig,filename = ' West Africa Ebola cases 2014' )
205
+ fig.show()
213
206
```
214
207
215
208
####Reference
216
209
Seehttps://plot.ly/python/reference/#choropleth andhttps://plot.ly/python/reference/#scattergeo for more information and chart attribute options!
217
-
218
- ``` python
219
- from IPython.displayimport display,HTML
220
-
221
- display(HTML(' <link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />' ))
222
- display(HTML(' <link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">' ))
223
-
224
- ! pip install git+ https:// github.com/ plotly/ publisher.git-- upgrade
225
- import publisher
226
- publisher.publish(
227
- ' bubble-maps.ipynb' ,' python/bubble-maps/' ,' Bubble Maps' ,
228
- ' How to make bubble maps in Python with Plotly.' ,
229
- title = ' Python Bubble Maps | Plotly' ,
230
- has_thumbnail = ' true' ,thumbnail = ' thumbnail/bubble-map.jpg' ,
231
- language = ' python' ,
232
- page_type = ' example_index' ,
233
- display_as = ' maps' ,order = 3 ,
234
- uses_plotly_offline = False )
235
- ```
236
-
237
- ``` python
238
-
239
- ```