gpt4 book ai didi

python - 在 python 中绘制图表 - LineCollection

转载 作者:行者123 更新时间:2023-12-01 00:16:19 24 4
gpt4 key购买 nike

我可以简化lines不知何故?

从此:

fig, ax = plt.subplots()

lines = np.array([[[x[i-1], y[i-1]], [x[i], y[i]]]])
line_coll = LineCollection(lines,colors='red')
ax.add_collection(line_coll)
lines = np.array([[[0, R], [x[i], y[i]]]])
line_coll = LineCollection(lines,colors='red')
ax.add_collection(line_coll)


plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])

类似或更短的内容:

lines = np.array([[[x[i-1], y[i-1]], [x[i], y[i]]]])
lines = np.array([[[0, R], [x[i], y[i]]]])
line_coll = LineCollection(lines,colors='red')
ax.add_collection(line_coll)

我不想添加仍然

line_coll = LineCollection(lines,colors='red')
ax.add_collection(line_coll)

对于每个 lines

<小时/>

编辑:

这是我的循环:

while i <= n:
if i == 0:
#code
lines= np.array([[[0, R], [x[i], 0]]])
#line_coll= LineCollection(lines,colors= 'red')
#ax.add_collection(line_coll)

elif i == n:
#code
lines = np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
[[[0, R], [x[i], y[i]]]]])
#for line in lines:
# line_coll = LineCollection(line, colors='red')
# ax.add_collection(line_coll)

else:
#code
lines = np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
[[[0, R], [x[i], y[i]]]]])
#for line in lines:
# line_coll = LineCollection(line, colors='red')
# ax.add_collection(line_coll)
i= i+1

#how can i add one loop for all "lines" above? This below doesnt work good
for line in lines:
line_coll = LineCollection(line, colors='red')
ax.add_collection(line_coll)
<小时/>

编辑 2:

while i <= n:
if i == 0:
#code
lines.append(np.array([[[0, R], [x[i], 0]]]))
elif i == n:
#code
lines.append(np.array([[[x[i-1], y[i-1]], [x[i], y[i]]], [[0, R], [x[i], y[i]]]]))

else:
#code
lines.append(np.array([[[x[i-1], y[i-1]], [x[i], y[i]]], [[0, R], [x[i], y[i]]]]))

i= i+1

for line in lines:
line_coll = LineCollection(line, colors='red')
ax.add_collection(line_coll)

while上面的循环有效(我必须删除一些 [] )如果我设置就好了:

plt.xlim([0, 60])
plt.ylim([0, 60])

如果我这样设置就不会:

plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])

有错误list indices must be integers or slices, not tuple对于 plt.xlim([0, lines[:,:,0].max()])

最佳答案

您可以将线条合并到一个数组中,然后循环该数组并一次添加一位线条艺术家。代码看起来像这样:

fig, ax = plt.subplots()

lines = np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
[[[0, R], [x[i], y[i]]]]])
for line in lines:
line_coll = LineCollection(line, colors='red')
ax.add_collection(line_coll)

plt.xlim([0, lines[:,:,0].max()])
plt.ylim([0, lines[:,:,1].max()])

编辑尝试以下技巧

lines = []

while i <= n:
if i == 0:
#Code here
lines.append(np.array([[[0, R], [x[i], 0]]]))
elif i == n:
#Code here
lines.append(np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
[[[0, R], [x[i], y[i]]]]]))
else:
#Code here
lines.append(np.array([[[[x[i-1], y[i-1]], [x[i], y[i]]]],
[[[0, R], [x[i], y[i]]]]]))
i= i+1

for line in lines:
line_coll = LineCollection(line, colors='red')
ax.add_collection(line_coll)

关于python - 在 python 中绘制图表 - LineCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59314361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com