简体   繁体   English

Matplotlib plt.plot与枚举不起作用

[英]Matplotlib plt.plot with enumerate not working

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)

Now, I need to call plot several times using different array lists. 现在,我需要使用不同的array列表多次调用plot So my for loop cannot be removed. 因此,我的for循环无法删除。 Is there any other way to plot a dotted line apart from calling plt.plot instead ? 除了调用plt.plot之外,还有其他方法可以绘制虚线吗?

This is the plot I get: 这是我得到的情节:

在此处输入图片说明

As you can see, I am not getting the plt.plot(i[2],i[3],'g--') . 如您所见,我没有得到plt.plot(i[2],i[3],'g--') Why is this so ? 为什么会这样呢?

But when you print the values using the same for loop: 但是,当您使用相同的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

The values are perfectly printed. 值已完美打印。 They however are not plotted. 但是,未绘制它们。

Remove the for loop: 删除for循环:

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

The problem with your code is that you iterate over rows, which is fine for plotting single dots ( ax.scatter ), but not for connecting single dots ( ax.plot with '--' option): at each row you only plot the line between that point and itself, which obviously doesn't show up in the graph. 与您的代码的问题是,你遍历行,这是罚款单绘制点( ax.scatter ),但不能用于连接单独点( ax.plot'--'选项):在每行只绘制该点与其自身之间的直线,显然不会显示在图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM