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,枚举(list.T):
ax .scatter(i [0],i [1],c ='red')#这是绘制的
ax.plot(i [2],i [3],'g--')#这是不被压缩!
fig.show()
plot(array)
现在,我需要使用不同的数组
列表来调用 plot
几次。 code> for 循环无法删除。除了调用 plt.plot
以外,是否还有其他方法可以绘制虚线?
这是我得到的情节:
$ b
正如您所看到的,我没有得到 plt.plot(i [2],i [3],'g- - ')
。为什么会这样?
但是当您使用相同的循环打印值时:
<$我在枚举(array.T)中:
...:print i [2],i [3]
。 ..:
3 100
4 200
5 300
6 400
7 500
8 600
这些值是完美打印的。但是他们没有被绘制。
删除for循环:
ax.scatter(array [0],array [1],c ='red')
ax.plot(array [0],array [1],'g--')
你的代码的问题是你遍历行,这是很好的绘制单点( ax.scatter
),但不能用于连接单点( ax.plot
和' - '
选项):在每一行中,您只绘制了该点与其自身之间的界线,显然不会显示在图表中。
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. So my for
loop cannot be removed. Is there any other way to plot a dotted line apart from calling plt.plot
instead ?
This is the plot I get:
As you can see, I am not getting the plt.plot(i[2],i[3],'g--')
. Why is this so ?
But when you print the values using the same for loop:
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:
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.
这篇关于Matplotlib plt.plot与枚举不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!