python - 在 python 中绘制图表 - LineCollection

标签 python matplotlib plot graph

我可以简化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/

相关文章:

python-2.7 - 如何在 Bokeh 中的 HoverTool 工具提示中显示与系列关联的图例名称?

python - Pandas 合并和求和数据帧

python - 如何合并字符串包含的 Pandas ?

python - 在 Python 中将图例标签设置为日期

python - 使用 OpenCV 或 Matplotlib/Pyplot 可视化 MNIST 数据集

python - 在 Canopy 的函数内使用 matplotlib.animation

python - 无法使用 GeoPandas 打开形状文件

python - 在 Bokeh 中使用 HoverTool 将本地镜像嵌入相对路径

matlab - 是否可以在 MATLAB 中打印 unicode 文本或字符?

matlab - 如何设置绘图的不透明度?