如何在 pyplot.go.Figure 和 go.Scattergeo 上划定国家和国家/地区之间的界限

How to draw bounderies between countries and countries' states on pyplot.go.Figure with go.Scattergeo

所以,我正在做一张地图,通过在地图上画线,代表路径,并根据出现的次数设置其不透明度来显示巴西一些城市之间的人流。为此,我遵循 this 代码(第三张地图,关于美国航班的地图)。

我的问题是,我可以划定国家之间的边界吗?并且,如果可能的话,巴西各州之间也是如此吗?

documentation中,有一个名为“geojson”的函数参数,但我不确定如何使用它,或者它是否对我有用。

请注意,我有国家和州的 GeoJSON 数据。

这是生成我的地图的代码:

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}
)

结果如下:

由于我没有 geojson 数据和经纬度信息来划线,我将使用您引用的官方参考来回答您的问题。

  • 使用 choropleth map,将带有 0 的总和列添加到此示例中使用的数据。
  • Specify the geojson 你获得了 geojson=usa_geo.
  • 我们将 geojson 状态名称与数据中的状态相关联。
  • 我将地图填充设置为浅灰色。
  • 注意:地图的中心设置是自动计算的,因为我们使用 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()