如何在世界 scatter_geo 上仅显示美国的国家边界?

发布于 2025-01-09 13:56:36 字数 414 浏览 1 评论 0原文

我正在使用plotly express scattergeo 在世界地图上绘制一些数据点,但我也希望在州一级显示美国。当我使用时,“世界”范围不会对 showsubunits 参数做出反应:

fig.update_geos(
   visible=True, resolution=50, scope='world',
   showcountires=True, countrycolor="Black",
   showsubunits=True, subunitcolor='Brown')

但是,当我将范围仅限于美国时,“美国”范围会做出反应并显示州界线。 有没有一种方法可以以某种方式覆盖仅包含国家边界的世界地图和包含国家边界的美国地图,以获取同一组散布纬度/经度数据?作为一个更普遍的问题,我可以有选择地定义在州一级显示哪些国家/地区吗?

I'm using plotly express scattergeo to plot some data points on a world map but I also like to have the US be shown at the state level. The 'world' scope doesn't react to the showsubunits argument when I use:

fig.update_geos(
   visible=True, resolution=50, scope='world',
   showcountires=True, countrycolor="Black",
   showsubunits=True, subunitcolor='Brown')

However, the 'usa' scope does and shows the state lines when I limit my scope only to US.
Is there a way to somehow overlay a world map with only country borders and also a US map with state borders for the same set of scatter lat/lon data? As a more general question, can I selectively define which countries to show at the state level?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2025-01-16 13:56:36
import requests
import numpy as np
import plotly.express as px
import plotly.graph_objects as go

states_geojson = requests.get(
    "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_1_states_provinces_lines.geojson"
).json()


df = px.data.gapminder().query("year == 2007")
fig = px.scatter_geo(
    df,
    locations="iso_alpha",
    size="pop",  # size of markers, "pop" is one of the columns of gapminder
)


fig = fig.add_trace(
    go.Scattergeo(
        lat=[
            v
            for sub in [
                np.array(f["geometry"]["coordinates"])[:, 1].tolist() + [None]
                for f in states_geojson["features"]
            ]
            for v in sub
        ],
        lon=[
            v
            for sub in [
                np.array(f["geometry"]["coordinates"])[:, 0].tolist() + [None]
                for f in states_geojson["features"]
            ]
            for v in sub
        ],
        line_color="brown",
        line_width=1,
        mode="lines",
        showlegend=False,
    )
)

fig.update_geos(
    visible=True, resolution=50, scope="world", showcountries=True, countrycolor="Black"
)

在此处输入图像描述

import requests
import numpy as np
import plotly.express as px
import plotly.graph_objects as go

states_geojson = requests.get(
    "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_110m_admin_1_states_provinces_lines.geojson"
).json()


df = px.data.gapminder().query("year == 2007")
fig = px.scatter_geo(
    df,
    locations="iso_alpha",
    size="pop",  # size of markers, "pop" is one of the columns of gapminder
)


fig = fig.add_trace(
    go.Scattergeo(
        lat=[
            v
            for sub in [
                np.array(f["geometry"]["coordinates"])[:, 1].tolist() + [None]
                for f in states_geojson["features"]
            ]
            for v in sub
        ],
        lon=[
            v
            for sub in [
                np.array(f["geometry"]["coordinates"])[:, 0].tolist() + [None]
                for f in states_geojson["features"]
            ]
            for v in sub
        ],
        line_color="brown",
        line_width=1,
        mode="lines",
        showlegend=False,
    )
)

fig.update_geos(
    visible=True, resolution=50, scope="world", showcountries=True, countrycolor="Black"
)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文