Matplotlib plt.plot枚举不起作用

时间:2017-10-11 05:03:10

标签: python for-loop matplotlib enumerate

import numpy as np 
import matplotlib.pyplot as plt 

array = np.array([[1,2,3,4,5,6],[10,20,30,40,50,60],[3,4,5,6,7,8],[100,200,300,400,500,600]])

def plot(list):
    fig = plt.figure()
    ax = fig.add_subplot(111)

    for a,i in enumerate(list.T):
        ax.scatter(i[0],i[1],c='red') # This is plotted
        ax.plot(i[2],i[3],'g--') # THIS IS NOT BEING PLOTTED !!!! 
    fig.show()

plot(array)

现在,我需要使用不同的plot列表多次致电array 因此我的for循环无法移除。除了调用plt.plot之外,还有其他方法可以绘制虚线吗?

这是我得到的情节:

enter image description here

正如您所看到的,我没有得到plt.plot(i[2],i[3],'g--')。为什么会这样?

但是当您使用相同的for循环打印值时:

In [21]: for a,i in enumerate(array.T):
    ...:     print i[2],i[3]
    ...:     
3 100
4 200
5 300
6 400
7 500
8 600

值完美打印。然而,他们没有绘制。

1 个答案:

答案 0 :(得分:1)

删除for循环:

ax.scatter(array[0],array[1],c='red')
ax.plot(array[0],array[1],'g--')

您的代码的问题在于您遍历行,这适用于绘制单个点(ax.scatter),但不适用于连接单个点(ax.plot'--'选项) :在每一行中,您只绘制该点与其自身之间的线,这显然不会出现在图中。