有没有办法控制哪个顶点在plotly.express.line_geo地图中连接?

发布于 2025-01-24 19:22:35 字数 1497 浏览 3 评论 0 原文

我正在尝试制作一个可以选择使用Animation_Frame显示不同月/年的连接图。 plotly.express具有此选项,但是plotly.express.line_geo地图似乎只是随机连接网络的顶点。我从

import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.line_geo(df, locations="iso_alpha",
                  color="continent", # "continent" is one of the columns of gapminder
                  projection="orthographic")
fig.show()

plotly.graph_objects允许您映射顶点之间的实际连接,但似乎没有动画选项。

import plotly.graph_objects as go
import pandas as pd

df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df_airports.head()

df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()

fig = go.Figure()

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()),
        )
    )

fig.show()

有谁知道我可以制作像飞行路径图这样的地图的方式,但是允许动画选项查看飞行图不同的几个月/年?

I'm trying to make a connection map that has the option to use an animation_frame to show different months/years. Plotly.express has this option, but the plotly.express.line_geo maps seem to just attach the vertices of the network at random. I was looking at these examples from https://plotly.com/python/lines-on-maps/.

import plotly.express as px
df = px.data.gapminder().query("year == 2007")
fig = px.line_geo(df, locations="iso_alpha",
                  color="continent", # "continent" is one of the columns of gapminder
                  projection="orthographic")
fig.show()

Plotly.graph_objects allows you to map actual connections between vertices, but doesn't seem to have an animation option.

import plotly.graph_objects as go
import pandas as pd

df_airports = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df_airports.head()

df_flight_paths = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
df_flight_paths.head()

fig = go.Figure()

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()),
        )
    )

fig.show()

Does anyone know of a way that i could make a map like the flight path map, but allow an animation option to look at the flight maps for different months/years?

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

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

发布评论

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

评论(1

素染倾城色 2025-01-31 19:22:35
  • 您可以使用框架
  • 将其分组为基于第一开始机场的首字母,
  • 而无需每次飞行创建跟踪,而是创建对,而是创建对阵列中的开始端位置的 none
  • 相距的
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd

df_flight_paths = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv"
)

frames = []

# lets split data based on first letter of start airport
# create a frame for each grouping
bins = 6
for color, df in df_flight_paths.groupby(
    pd.qcut(
        df_flight_paths["airport1"].str[0].apply(ord),
        q=bins,
        labels=px.colors.qualitative.Plotly[:bins],
    )
):
    name = f'{df["airport1"].str[0].min()}-{df["airport1"].str[0].max()}'
    frames.append(
        go.Frame(
            name=name,
            data=go.Scattergeo(
                lon=df.assign(nan=None)[["start_lon", "end_lon", "nan"]].values.ravel(),
                lat=df.assign(nan=None)[["start_lat", "end_lat", "nan"]].values.ravel(),
                mode="lines",
                line=dict(width=1, color=color),
            ),
        )
    )

# now create figure and add play button and slider
go.Figure(
    data=frames[0].data,
    frames=frames,
    layout={
        "updatemenus": [
            {
                "type": "buttons",
                "buttons": [{"label": "Play", "method": "animate", "args": [None]}],
            }
        ],
        "sliders": [
            {
                "active": 0,
                "steps": [
                    {
                        "label": f.name,
                        "method": "animate",
                        "args": [[f.name]],
                    }
                    for f in frames
                ],
            }
        ],
    },
).update_geos(
    scope="north america",
)

与 a href =“ https://i.sstatic.net/4lvk5.png” rel =“ nofollow noreferrer”>

  • you can animate any trace type using frames
  • taking sample flight path data used in question, have split it into groups based on first letter of start airport
  • there is no need to create a trace per flight, instead create pairs of start end locations in arrays separated by None
  • with this it is simple to create a frame with a trace for each group
  • then just create the figure from the frames, plus a default trace
  • add play button and slider
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd

df_flight_paths = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv"
)

frames = []

# lets split data based on first letter of start airport
# create a frame for each grouping
bins = 6
for color, df in df_flight_paths.groupby(
    pd.qcut(
        df_flight_paths["airport1"].str[0].apply(ord),
        q=bins,
        labels=px.colors.qualitative.Plotly[:bins],
    )
):
    name = f'{df["airport1"].str[0].min()}-{df["airport1"].str[0].max()}'
    frames.append(
        go.Frame(
            name=name,
            data=go.Scattergeo(
                lon=df.assign(nan=None)[["start_lon", "end_lon", "nan"]].values.ravel(),
                lat=df.assign(nan=None)[["start_lat", "end_lat", "nan"]].values.ravel(),
                mode="lines",
                line=dict(width=1, color=color),
            ),
        )
    )

# now create figure and add play button and slider
go.Figure(
    data=frames[0].data,
    frames=frames,
    layout={
        "updatemenus": [
            {
                "type": "buttons",
                "buttons": [{"label": "Play", "method": "animate", "args": [None]}],
            }
        ],
        "sliders": [
            {
                "active": 0,
                "steps": [
                    {
                        "label": f.name,
                        "method": "animate",
                        "args": [[f.name]],
                    }
                    for f in frames
                ],
            }
        ],
    },
).update_geos(
    scope="north america",
)

enter image description here

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