python - 使用 plotly 在一个图中绘制多条 3d 线

标签 python pandas matplotlib plot plotly

我有许多长度可变的 2d 序列,即列表列表,其中每个子列表都是一个序列。我想在 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

这就是我得到的,这并不是我真正想要的。它将所有具有公共(public)标签的案例/子列表视为一个序列,因此我们在每一行的末尾看到它返回到它开始的位置。我研究了如何在 for 循环中迭代地绘制它(就像 matplotlib)(基本上是在每次迭代时创建一个新的 pandas 数据框并绘制它),但是没有成功。请问有人有这方面的经验吗?非常感谢!

screenshot

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 行。

最佳答案

您的问题是您对不同的轨迹使用了相同的标签。这里有一个循环的解决方法

import numpy as np
import plotly.graph_objs as go

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]]

fig = go.Figure()
for i, feat in enumerate(features):
    feat = np.array(feat)
    fig.add_trace(
        go.Scatter3d(
            x=np.arange(len(feat)),
            y=feat[:,0],
            z=feat[:,1],
            mode='lines',
            hovertext=labels[i]
        )
    )
fig.show()

您可能需要使用跟踪名称。

更新

虽然它并不太复杂,但它应该尽可能通用


import numpy as np
import plotly.graph_objs as go
from itertools import cycle

def plotly_color_map(names):
    # From https://stackoverflow.com/a/44727682
    plotly_colors = cycle(['#1f77b4',  # muted blue
                           '#ff7f0e',  # safety orange
                           '#2ca02c',  # cooked asparagus green
                           '#d62728',  # brick red
                           '#9467bd',  # muted purple
                           '#8c564b',  # chestnut brown
                           '#e377c2',  # raspberry yogurt pink
                           '#7f7f7f',  # middle gray
                           '#bcbd22',  # curry yellow-green
                           '#17becf'  # blue-teal
                           ])

    return dict(zip(names, plotly_colors))


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]]

legend_groups = [l[0] for l in labels]

traces = [False if (len(legend_groups[:i])>0 and l in legend_groups[:i]) 
          else True for i, l in enumerate(legend_groups)]

cm = plotly_color_map(set(legend_groups))

fig = go.Figure()
for i, feat in enumerate(features):
    feat = np.array(feat)
    fig.add_trace(
        go.Scatter3d(
            x=np.arange(len(feat)),
            y=feat[:,0],
            z=feat[:,1],
            mode='lines',
            line={"color":cm[legend_groups[i]]},
            legendgroup=legend_groups[i],
            hovertext=labels[i],
            showlegend=traces[i],
            name="label_{}".format(legend_groups[i])
        )
    )
fig.show()

关于python - 使用 plotly 在一个图中绘制多条 3d 线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60153352/

相关文章:

python - 在 PyTorch 中定义 Adam 优化器的最佳方式是什么?

python - 对 Pandas 数据框的列进行排序

python - 如何计算每小时平均次数?

python - 将顶部主要 x 刻度的位置更改为底部轴的函数

python - 如何在 conda environment.yml 文件中 pip 安装包?

python - 无法在 python 3.5 中动态更新 pyplot

python - 通过 Python 但通过 Linux 命令终端调用 MATLAB

python - 向每个组 pandas python 添加序列计数

python - matplotlib 中的矩形位置不正确

python - 使用 timedelta 系列绘制直方图