Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2047dd

Browse files
authored
Merge pull request#27 from plotly/bubble-maps
bubble map
2 parentsa5234af +a804238 commitb2047dd

File tree

1 file changed

+79
-109
lines changed

1 file changed

+79
-109
lines changed

‎notebooks/bubble-maps.md

Lines changed: 79 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ jupyter:
1111
display_name:Python 3
1212
language:python
1313
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
1424
plotly:
1525
description:How to make bubble maps in Python with Plotly.
1626
display_as:maps
@@ -23,22 +33,36 @@ jupyter:
2333
permalink:python/bubble-maps/
2434
thumbnail:thumbnail/bubble-map.jpg
2535
title:Python Bubble Maps | Plotly
36+
v4upgrade:true
2637
---
2738

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
3240

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.
3342

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
3653

3754
```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()
4062
```
4163

64+
###Bubble Map with go.Scattergeo
65+
4266
####United States Bubble Map
4367

4468
Note about`sizeref`:
@@ -52,8 +76,7 @@ Note that setting `sizeref` to a value greater than $1$, decreases the rendered
5276
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.
5377

5478
```python
55-
import plotly.plotlyas py
56-
import plotly.graph_objsas go
79+
import plotly.graph_objectsas go
5780

5881
import pandasas pd
5982

@@ -62,108 +85,95 @@ df.head()
6285

6386
df['text']= df['name']+'<br>Population'+ (df['pop']/1e6).astype(str)+' million'
6487
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"]
6689
cities= []
6790
scale=5000
6891

92+
fig= go.Figure()
93+
6994
for iinrange(len(limits)):
7095
lim= limits[i]
7196
df_sub= df[lim[0]:lim[1]]
72-
city=go.Scattergeo(
97+
fig.add_trace(go.Scattergeo(
7398
locationmode='USA-states',
7499
lon= df_sub['lon'],
75100
lat= df_sub['lat'],
76101
text= df_sub['text'],
77-
marker=go.scattergeo.Marker(
102+
marker=dict(
78103
size= df_sub['pop']/scale,
79104
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,
83107
sizemode='area'
84108
),
85-
name='{0} -{1}'.format(lim[0],lim[1]) )
86-
cities.append(city)
109+
name='{0} -{1}'.format(lim[0],lim[1])))
87110

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)',
92113
showlegend=True,
93-
geo=go.layout.Geo(
114+
geo=dict(
94115
scope='usa',
95-
projection= go.layout.geo.Projection(
96-
type='albers usa'
97-
),
98-
showland=True,
99116
landcolor='rgb(217, 217, 217)',
100-
subunitwidth=1,
101-
countrywidth=1,
102-
subunitcolor="rgb(255, 255, 255)",
103-
countrycolor="rgb(255, 255, 255)"
104117
)
105118
)
106119

107-
fig= go.Figure(data=cities,layout=layout)
108-
py.iplot(fig,filename='d3-bubble-map-populations')
120+
fig.show()
109121
```
110122

111123
####Ebola Cases in West Africa
112124

113125
```python
114-
import plotly.plotlyas py
115-
import plotly.graph_objsas go
126+
import plotly.graph_objectsas go
116127

117128
import pandasas pd
118129

119130
df= pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_ebola.csv')
120131
df.head()
121132

122-
cases= []
123133
colors= ['rgb(239,243,255)','rgb(189,215,231)','rgb(107,174,214)','rgb(33,113,181)']
124134
months= {6:'June',7:'July',8:'Aug',9:'Sept'}
125135

136+
fig= go.Figure()
137+
126138
for iinrange(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'],
131144
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,
134147
color= colors[i-6],
135-
line= go.scattergeo.marker.Line(width=0)
136-
)
137-
)
138-
)
148+
line_width=0
149+
)))
139150

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'])
144155

145-
inset= [
146-
go.Choropleth(
156+
# Inset
157+
fig.add_trace(go.Choropleth(
147158
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'],
151162
colorscale= [[0,'rgb(0, 0, 0)'],[1,'rgb(0, 0, 0)']],
152163
autocolorscale=False,
153164
showscale=False,
154165
geo='geo2'
155-
),
156-
go.Scattergeo(
166+
))
167+
fig.add_trace(go.Scattergeo(
157168
lon= [21.0936],
158169
lat= [7.1881],
159170
text= ['Africa'],
160171
mode='text',
161172
showlegend=False,
162173
geo='geo2'
163-
)
164-
]
174+
))
165175

166-
layout= go.Layout(
176+
fig.update_layout(
167177
title= go.layout.Title(
168178
text='Ebola cases reported by month in West Africa 2014<br>\
169179
Source: <a href="https://data.hdx.rwlabs.org/dataset/rowca-ebola-cases">\
@@ -173,67 +183,27 @@ HDX</a>'),
173183
scope='africa',
174184
showframe=False,
175185
showcoastlines=True,
176-
showland=True,
177186
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 ])
193193
),
194194
geo2= go.layout.Geo(
195195
scope='africa',
196196
showframe=False,
197-
showland=True,
198197
landcolor="rgb(229, 229, 229)",
199198
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 ]),
204200
bgcolor='rgba(255, 255, 255, 0.0)',
205201
),
206-
legend= go.layout.Legend(
207-
traceorder='reversed'
208-
)
202+
legend_traceorder='reversed'
209203
)
210204

211-
fig= go.Figure(layout=layout,data=cases+inset)
212-
py.iplot(fig,filename='West Africa Ebola cases 2014')
205+
fig.show()
213206
```
214207

215208
####Reference
216209
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-
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp