如何在动画绘图散点图中拥有多个数据帧?

How do I have multiple dataframes in an animated plotly scatter graph?

我正在尝试在动画绘图散点图上显示 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"] ))
  • 这可以在单个数据框中非常简单地完成
    1. xy 是浮点值
    2. time 动画帧
    3. 时间的字符串表示
    4. trace x/y 对对应于 x1/y1[=42 的字符串表示=]、x2/y2
  • 使用 Plotly Express 就非常简单
  • 已包含示例数据以显示此结构有多么简单
  • comments 要求对每条轨迹使用不同的符号。 https://plotly.com/python/marker-style/#custom-marker-symbols
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

原始数据

  • 根据评论 - 数据框的构造有许多不是解决方案核心的技术
  • 这仅显示两倍样本数据并加载到数据框中
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")