[英]Plotly Bubble map bubbles not displaying
Currently working with a US Cities dataset for some further analysis in a Jupyter Notebook project.目前正在使用美国城市数据集在 Jupyter Notebook 项目中进行进一步分析。 Still an amateur in Python DS, want to make an interactive bubble map with plotly, displaying population per US city.仍然是 Python DS 的业余爱好者,想制作一个交互式气泡 map 和 plotly,显示每个美国城市的人口。 Provided output of Code in photo.提供照片中的Code output。 Structure of DataFrame: DataFrame的结构:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 19500 entries, 0 to 19499
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 US Pop Rank 19500 non-null int64
1 City 19500 non-null object
2 State 19500 non-null object
3 2019 Population 19500 non-null int64
4 Pop Growth 2000-2019 19383 non-null object
5 Longitude 19500 non-null float64
6 Latitude 19500 non-null float64
dtypes: float64(2), int64(2), object(3)
memory usage: 1.2+ MB
And the following code for the plot:以及 plot 的以下代码:
Geographics['text'] = Geographics['City'] + '<br>Population ' + (Geographics['2019 Population']/1e6).astype(str)+' million'
limits = [(0,2),(3,10),(11,20),(21,50),(50,3000)]
colors = ["royalblue","crimson","lightseagreen","orange","lightgrey"]
cities = []
scale = 5000
fig = go.Figure()
for i in range(len(limits)):
lim = limits[i]
Geographics_sub = Geographics[lim[0]:lim[1]]
fig.add_trace(go.Scattergeo(
locationmode = 'USA-states',
lon = Geographics_sub['Longitude'],
lat = Geographics_sub['Latitude'],
text = Geographics_sub['text'],
marker = dict(
size = Geographics_sub['2019 Population']/scale,
color = colors[i],
line_color = 'rgb(40,40,40)',
line_width = 0.5,
sizemode = 'area'
),
name = '{0} - {1}'.format(lim[0],lim[1])))
fig.update_layout(
title_text = '2019 US city populations<br>(Click legend to toggle traces)',
showlegend = True,
geo = dict(
scope = 'usa',
landcolor = 'rgb(219, 219, 219)',
)
)
fig.show()
Order can matter, try swapping these sections of code as you may have the map on top of the scatters.顺序可能很重要,请尝试交换这些代码部分,因为您可能在散点上方有 map。
Geographics['text'] = Geographics['City'] + '<br>Population ' + (Geographics['2019 Population']/1e6).astype(str)+' million'
limits = [(0,2),(3,10),(11,20),(21,50),(50,3000)]
colors = ["royalblue","crimson","lightseagreen","orange","lightgrey"]
cities = []
scale = 5000
fig = go.Figure()
fig.update_layout(
title_text = '2019 US city populations<br>(Click legend to toggle traces)',
showlegend = True,
geo = dict(
scope = 'usa',
landcolor = 'rgb(219, 219, 219)',
)
)
for i in range(len(limits)):
lim = limits[i]
Geographics_sub = Geographics[lim[0]:lim[1]]
fig.add_trace(go.Scattergeo(
locationmode = 'USA-states',
lon = Geographics_sub['Longitude'],
lat = Geographics_sub['Latitude'],
text = Geographics_sub['text'],
marker = dict(
size = Geographics_sub['2019 Population']/scale,
color = colors[i],
line_color = 'rgb(40,40,40)',
line_width = 0.5,
sizemode = 'area'
),
name = '{0} - {1}'.format(lim[0],lim[1])))
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.