如何创建 n 个绘图形状(在循环中)- python
How to create n plotly shapes (in loop) - python
所以我想用 Plotly 在图表中绘制 n 个圆形。这个数字 n 可以变化,这就是为什么我不能手动创建它们但我需要一个循环。有没有办法做到这一点?根据我的列表索引复制这段代码n次?:
'shapes': [
# Circle to define radius_of_persistence
{
'type': 'circle',
'xref': 'x',
'yref': 'y',
'x0': zois_list[0]-radius_of_persistence,
'y0': zois_list[1]-radius_of_persistence,
'x1': zois_list[0]+radius_of_persistence,
'y1': zois_list[1]+radius_of_persistence,
'line': {
'color': 'rgba(255, 171, 96, 1)',
},
},
所以,基本上,我发现在循环中创建形状的唯一方法是创建代表 "layout" 我自己的 json 对象。通过这种方式,我可以根据需要在字典(布局)中包含尽可能多的条目。
我在更新图形布局之前循环创建了形状列表,例如:
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
)
所以我想用 Plotly 在图表中绘制 n 个圆形。这个数字 n 可以变化,这就是为什么我不能手动创建它们但我需要一个循环。有没有办法做到这一点?根据我的列表索引复制这段代码n次?:
'shapes': [
# Circle to define radius_of_persistence
{
'type': 'circle',
'xref': 'x',
'yref': 'y',
'x0': zois_list[0]-radius_of_persistence,
'y0': zois_list[1]-radius_of_persistence,
'x1': zois_list[0]+radius_of_persistence,
'y1': zois_list[1]+radius_of_persistence,
'line': {
'color': 'rgba(255, 171, 96, 1)',
},
},
所以,基本上,我发现在循环中创建形状的唯一方法是创建代表 "layout" 我自己的 json 对象。通过这种方式,我可以根据需要在字典(布局)中包含尽可能多的条目。
我在更新图形布局之前循环创建了形状列表,例如:
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
)