【发布时间】:2020-05-25 22:41:43
【问题描述】:
我有许多可变长度的二维序列,即列表列表,其中每个子列表都是一个序列。我想在 3d 可视化中投影这些序列/行/子列表,添加时间步长作为另一个维度。到目前为止,我未能使用 plotly.express 绘制所有 3d 线。
import plotly.express as px
t = [[ii+1 for ii in range(len(features[i]))] for i in range(len(labels))]
x0 = [[x[0] for x in features[i]] for i in range(len(labels))]
x1 = [[x[1] for x in features[i]] for i in range(len(labels))]
df = pd.DataFrame(dict(
X=[tii for ti in t for tii in ti],
Y=[xii for xi in x0 for xii in xi],
Z=[xii for xi in x1 for xii in xi],
color=[aa for a in labels for aa in a]
))
fig = px.line_3d(df, x="X", y="Y", z="Z", color="color")
fig.show
这就是我得到的,这并不是我真正想要的。它将所有带有公共标签的案例/子列表视为一个序列,因此我们在每一行的末尾看到它回到它开始的地方。我已经查看了如何在 for 循环中迭代地绘制它(就像 matplotlib 一样)(基本上在每次迭代时创建一个新的 pandas 数据框并绘制它),但是没有成功。请问有人有这方面的经验吗?非常感谢!
一个mcve如下:
import plotly.express as px
import numpy as np
import pandas as pd
features = [np.random.rand(4,2).tolist(),
np.random.rand(5,2).tolist(),
np.random.rand(6,2).tolist(),
np.random.rand(5,2).tolist(),
np.random.rand(9,2).tolist()]
labels = [[1, 1, 1, 1], [1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2],
[2, 2, 2, 2, 2], [0, 0, 0, 0, 0, 0, 0, 0, 0]]
t = [[ii+1 for ii in range(len(features[i]))] for i in range(len(labels))]
x0 = [[x[0] for x in features[i]] for i in range(len(labels))]
x1 = [[x[1] for x in features[i]] for i in range(len(labels))]
df2 = pd.DataFrame(dict(
X=[tii for ti in t for tii in ti],
Y=[xii for xi in x0 for xii in xi],
Z=[xii for xi in x1 for xii in xi],
color=[aa for a in labels for aa in a]
))
fig1 = px.line_3d(df2, x="X", y="Y", z="Z", color="color")
fig1.show()
你看到的基本上是 3 行而不是 5 行。
【问题讨论】:
-
在我看来,
features没有定义。你介意生成一个mcve吗? -
谢谢。我在问题中提供了一个示例。
-
你看到三行因为
px第一组标签[1,1,...]与第二组相同。
标签: python pandas matplotlib plot plotly