【问题标题】:How to draw bounderies between countries and countries' states on pyplot.go.Figure with go.Scattergeo如何使用 go.Scattergeo 在 pyplot.go.Figure 上绘制国家和国家之间的边界
【发布时间】:2022-01-23 19:30:42
【问题描述】:

所以,我正在制作一张地图,通过在地图上绘制线条、表示路径并根据出现次数设置其不透明度来显示巴西一些城市之间的人流。为此,我关注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}
)

结果如下:

【问题讨论】:

    标签: python matplotlib plotly.graph-objects


    【解决方案1】:

    由于我没有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()
    

    【讨论】:

    • 有一些使用 geojson 的示例,但我很难找到任何使用 geojson 进行散点图的示例。答案仅适用于北美,所以我不知道这段代码对您的数据的处理效果如何。我会尽力提供帮助。
    • 效果很好。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    相关资源
    最近更新 更多