【问题标题】:How do I have multiple dataframes in an animated plotly scatter graph?如何在动画散点图中有多个数据框?
【发布时间】:2021-12-06 06:51:06
【问题描述】:

我试图在动画散点图上显示 3 组 X/Y 坐标,其中动画键是时间。目前我的解决方法是将所有坐标集添加到同一个数据框中,但是我相信这会给我带来问题,因为我需要更改标记属性以轻松区分每个点。

这就是我的解决方法:

这就是我生成图表的方式:

x1_trim += x2_trim
x1_trim += x3_trim
y1_trim += y2_trim
y1_trim += y3_trim

d = {
    "x1": x1_trim,
    "y1": y1_trim,
    "time": time_trim
}
df = pd.DataFrame(d)

#Default x and y axis
x_range = [-1,1]
y_range = [-1,1]

fig = px.scatter(df, x="x1", y="y1", animation_frame="time", range_x=x_range, range_y=y_range)
fig.add_shape(type="rect",x0=-0.5, y0=-0.5, x1=0.5, y1=0.5, line=dict(color="Green",width=2))

如您所见,我将 x2/y2 和 x3/y3 数据添加到 x1/y1 列表的末尾,如何在保留动画情节的所有信息的同时将它们分开?我试图在同一个图上显示多个散点图,但从未设法让它工作。

我的解决方案尝试:

#Building the dataframe and drawing graph
d1 = {
    "x": x1_trim,
    "y": y1_trim,
    "time": time_trim
}

d2 = {
    "x": x2_trim,
    "y": y2_trim,
    "time":time_trim
}

d3 = {
    "x": x3_trim,
    "y": y3_trim,
    "time": time_trim
}

dfs = {"d1": d1, "d2": d2, "d3": d3}

fig = go.Figure()

for i in dfs:
   fig = fig.add_trace(go.Scatter(x = dfs[i]["x"], y = dfs[i]["y"], name = i, animation_frame=dfs[0]["time"] ))

【问题讨论】:

    标签: python python-3.x plotly plotly-python


    【解决方案1】:
    import numpy as np
    import itertools
    import pandas as pd
    import plotly.express as px
    import plotly.graph_objects as go
    
    # construct a dataframe that has four columns, x, y, trace: 0,1,2 as string for trace, time for animation frame
    df = pd.DataFrame(np.random.uniform(1, 5, [288, 2]), columns=["x", "y"]).join(
        pd.DataFrame(
            itertools.product(
                pd.Series(
                    pd.date_range("1-jan-2021", freq="1H", periods=24 * 4)
                ).dt.time.astype(str),
                range(3),
            ),
            columns=["time", "trace"],
        ).astype(str)
    )
    
    fig = px.scatter(
        df.assign(size=8),
        x="x",
        y="y",
        color="trace",
        animation_frame="time",
        symbol="trace",
        size="size",
        size_max=10,
    )
    fig
    
    

    数据帧结构

    x        float64
    y        float64
    time      object
    trace     object
    dtype: object
    

    原始数据

    • 每 cmets - 数据框的构建有许多不是解决方案核心的技术
    • 这仅显示了两次样本数据并加载到数据框中
    import io
    df = pd.read_csv(io.StringIO("""x,y,time,trace
    4.47189433943762,2.2663279945423125,00:00:00,0
    3.263751615344729,2.7707896475420433,00:00:00,1
    3.5073083888118197,3.937926244743114,00:00:00,2
    3.254552306893224,1.7740014652497695,01:00:00,0
    1.6111813732639115,1.5324478432794377,01:00:00,1
    3.411314175447148,4.495634466903654,01:00:00,2
    1.7036170024927264,4.284719804413246,00:00:00,0
    1.9797441059531726,3.9012400136550798,00:00:00,1
    1.5178030860172549,3.7904674709011084,00:00:00,2
    2.03612601506845,2.5378053661978592,01:00:00,0
    2.230688800088902,2.946463794148376,01:00:00,1
    1.3626620551885207,2.442489690168825,01:00:00,2
    4.733618949813925,3.9103378744051014,00:00:00,0
    4.4816142771548435,1.1245335267028125,00:00:00,1
    4.9550805577829315,3.2454665809417227,00:00:00,2
    2.9007566994079816,1.1620429771047482,01:00:00,0
    2.11807366926913,3.9811777626521083,01:00:00,1
    2.0753910017252144,4.416934286540347,01:00:00,2
    2.3867481776916804,1.6378885254464284,00:00:00,0
    1.4021710772900526,2.1565431787254536,00:00:00,1
    3.5150580308648562,2.2722969079838387,00:00:00,2
    4.987010605760303,1.943335174662026,01:00:00,0
    3.0504403251471484,4.398673922531113,01:00:00,1
    4.021398175417694,4.422199058284852,01:00:00,2"""))
    
    df["trace"] = df["trace"].astype(str)
    px.scatter(df, x="x", y="y", color="trace", animation_frame="time")
    

    【讨论】:

    • 非常感谢您回答我的问题,我对您如何创建数据框感到非常困惑,因为我以前从未使用过 pandas。您能否向我展示如何使用硬编码数据创建数据框作为更简单的示例?道歉。
    • 我知道我已经使用了很多编码糖来生成 DF...因此我在答案中包含了 20 个样本行。这真的是硬编码数据
    • 已将样本数据更新为 CSV 以构建数据框。无需理解我用于生成测试数据的技术
    • stackoverflow.com/help/someone-answers 已更新 - 您需要非常熟悉 Python 编码和绘图对象模型才能理解这一点。
    • 进一步更新 - 发现更简单的符号方法
    猜你喜欢
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 2021-10-28
    相关资源
    最近更新 更多