如何为 go.layout.Shape(type="line"...) 分配标签?

How to assign a label to go.layout.Shape(type="line"...)?

我制作了下图。

该图应用了一些 add_trace,并以 go.Scatter 作为参数。

已创建具有固定 color 属性的 4 go.layout.Shapetype="line" 列表,并使用该列表更新图形布局:fig.update_layout(..., shapes=...)

这些迹线已分配有标签,我们可以在最右边看到它们。

有没有办法添加标签以分配给线条?

您希望您的线条出现在图例中 (https://plot.ly/python/legend/). However, only traces can appear in the legend, not shapes which are a kind of annotation. What you could do is to create the lines using go.Scatter(..., mode='lines'), and then they would appear in the legend. You just need to give the starting and end points in go.Scatter (see https://plot.ly/python/line-and-scatter/)。