[英]How to draw bounderies between countries and countries' states on pyplot.go.Figure with go.Scattergeo
So, I am doing a map that shows the flow of people among some cities in Brazil by drawing lines on the map, representing the path, and setting its opacity according to the count of occurrences.所以,我正在做一个 map,它通过在 map 上画线来显示巴西一些城市之间的人流,代表路径,并根据出现次数设置其不透明度。 To do so, I am following this code (third map, the one about flights on US).
为此,我遵循此代码(第三个 map,关于美国航班的代码)。
My question is, can I draw the borders between countries?我的问题是,我可以画出国家之间的边界吗? And, if possible, also between Brazil states?
并且,如果可能的话,也在巴西各州之间?
In the documentation , there is an argument of the function called "geojson", but I'am not sure on how to use it, or if it is even useful for me.在文档中,有一个名为“geojson”的 function 的参数,但我不确定如何使用它,或者它是否对我有用。
Note that I have GeoJSON data for both countries and states.请注意,我有国家和州的 GeoJSON 数据。
Here's the code to generate the my map:这是生成我的 map 的代码:
import pandas as pd
import plotly.graph_objects as go
fig = go.Figure()
for i in range(len(my_df)):
fig.add_trace(
go.Scattergeo(
lon = [my_df['res_longitude'][i], my_df['nasc_longitude'][i]],
lat = [my_df['res_latitude'][i], my_df['nasc_latitude'][i]],
mode = 'lines',
line = dict(width = 1,color = 'red'),
opacity = min(1, float(my_df['flow'][i]) / float(my_df['flow'].quantile(.95))),
)
)
fig.update_layout(
showlegend = False,
margin ={'l':0,'t':0,'b':0,'r':0},
mapbox = {
'center': {'lon': -50.3206, 'lat': -16.4984},
'style': "stamen-terrain",
'zoom': 3}
)
and here's the result:结果如下:
Since I don't have the geojson data and the latitude and longitude information to draw the line, I'll use the official reference you referenced to answer your question.由于我没有 geojson 数据和经纬度信息来画线,我将使用您引用的官方参考资料来回答您的问题。
geojson=usa_geo
.geojson=usa_geo
。fitbounds
for the location.fitbounds
的中心设置是自动计算的,因为我们使用的是定位边界。from urllib import request
import json
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
# usa geojson data
# https://eric.clst.org/tech/usgeojson/
usa_json = open('./data/gz_2010_us_040_00_500k.json', 'r')
usa_geo = json.load(usa_json)
# Choropleth Maps with go.Choropleth
# https://plotly.com/python/choropleth-maps/
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
# https://plotly.com/python/lines-on-maps/
df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
# dummy data column
dfs = pd.concat([df, pd.Series([0]*len(df),name='count')], axis=1)
fig = go.Figure()
fig.add_trace(go.Choropleth(
geojson=usa_geo,
locations=df['state'],
z = dfs['count'].astype(float),
featureidkey='properties.NAME',
colorscale = [[0,'rgb(200, 200, 200)']],
showlegend=False,
coloraxis=None,
colorbar=None
))
fig.update_traces(showscale=False)
flight_paths = []
for i in range(len(df_flight_paths)):
fig.add_trace(
go.Scattergeo(
#locationmode = 'USA-states',
lon = [df_flight_paths['start_lon'][i], df_flight_paths['end_lon'][i]],
lat = [df_flight_paths['start_lat'][i], df_flight_paths['end_lat'][i]],
mode = 'lines',
line = dict(width = 1,color = 'red'),
opacity = float(df_flight_paths['cnt'][i]) / float(df_flight_paths['cnt'].max()),
showlegend=False
)
)
fig.update_layout(
autosize=False,
width=1000,
height=600,
margin={"r":0,"t":0,"l":0,"b":0},
geo=dict(
scope='north america', # you chenge 'south america'
fitbounds="locations", # It is associated width 'px.Choropleth'
visible=True,
showland=True,
#center=dict(lon=34.05795, lat=-179.25450),
# The center designation of the map has no effect as this is automatically calculated
)
)
fig.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.