如何创建n个绘图形状(循环)-Python

时间:2018-09-11 10:52:05

标签: python plot plotly shapes

因此,我尝试使用Plotly在图形中绘制n个圆形。这个数字n可以变化,这就是为什么我不能手动创建它们,但是我需要一个循环的原因。反正有这样做吗?要根据我的列表索引复制这段代码n次?:

mongoose.Types.ObjectId('id');

2 个答案:

答案 0 :(得分:0)

因此,基本上,我发现在循环中创建形状的唯一方法是创建自己代表“布局”的json对象。这样,我可以根据需要在词典(布局)中包含尽可能多的条目。

答案 1 :(得分:0)

在更新图形布局之前,我以循环方式创建了形状列表,例如:

shapes = []
    for ind_begin, ind_end in zip(ind_begins, ind_ends):
        shapes.append(go.layout.Shape(
                    type="rect",
                    xref="x",
                    yref="paper",
                    x0=df.iloc[:, 0][ind_begin],
                    y0=0,
                    x1=df.iloc[:, 0][ind_end],
                    y1=1,
                    fillcolor="LightSalmon",
                    opacity=0.5,
                    layer="below",
                    line_width=0,
                ))
    fig.update_layout(
            shapes=shapes
        )