Plotly - 如何将宽度设置为特定行?

Plotly - How to set width to specific line?

我正在尝试使用 plotly 库和 我想设置每行的特定宽度。

这是我的代码。


x_link = [125, 257, None, 125, 787, None]
y_link = [383, 588, None, 383, 212, None]
z_link = [65, 85, None, 65, 526, None]

link_size = [3,6]

trace1= go.Scatter3d(
                x = x_link, 
                y = y_link,
                z = z_link,
                line=dict(
                    color='#0061ff',
                    width=link_size
                )
)

但是它引发了这个错误。

Invalid value of type 'builtins.list' received for the 'width' property of scatter3d.line Received value: [3, 6]

The 'width' property is a number and may be specified as: - An int or float in the interval [0, inf]

那么,有没有办法设置每行的具体宽度呢?

谢谢。

这是您要执行的操作的文档https://plot.ly/python/reference/#scatter-line

Line 只接受单个宽度值,而不是一组值,这与您给它的不同。

尝试只传入 6,应该不会出错。

您需要制作多条走线并指定每条走线的宽度:

x_link = [125, 257, None, 125, 787, None]
y_link = [383, 588, None, 383, 212, None]
z_link = [65, 85, None, 65, 526, None]

link_size = [3,6]

trace1= go.Scatter3d(
                x = x_link[:3], 
                y = y_link[:3],
                z = z_link[:3],
                line=dict(
                    color='green',
                    width=link_size[0]
                )
)

trace2= go.Scatter3d(
                x = x_link[3:], 
                y = y_link[3:],
                z = z_link[3:],
                line=dict(
                    color='red',
                    width=link_size[1]
                )
)

fig = go.Figure(data=[trace1, trace2])

您将不得不为您的数据集添加 单独的轨迹 才能执行此操作,因为 line=dict(width) 只需要一个单个参数而不是某种数组。对于更大的数据集,这可能是 大量工作。但是,如果您将数据集从 lists 更改为 a list of lists,你可以考虑更多的数据点。因此,通过一些数据操作,您可以将其变为:

x_link = [125, 257, None, 125, 787, None]
y_link = [383, 588, None, 383, 212, None]
z_link = [65, 85, None, 65, 526, None]

进入这个:

x_link = [[125, 257, None], [125, 787, None]]
y_link = [[383, 588, None], [383, 212, None]]
z_link = [[65, 85, None], [65, 526, None]]

和运行此代码:

import plotly.graph_objects as go
from plotly.offline import init_notebook_mode, iplot

x_link = [[125, 257, None], [125, 787, None]]
y_link = [[383, 588, None], [383, 212, None]]
z_link = [[65, 85, None], [65, 526, None]]

# figure formatting
colors=['red', 'green']
link_size = [2,12]

# make multiple traces
traces={}
for i in range(0, len(x_link)):
    traces['trace_' + str(i)]=go.Scatter3d(x = x_link[i], 
                                           y = y_link[i],
                                           z = z_link[i],
                                           line=dict(
                                                color=colors[i],
                                                width=link_size[i]))
data=list(traces.values())

# build and plot figure
fig=go.Figure(data)
fig.show()

获取此图: