枚举不工作的 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)
现在,我需要使用不同的 array
列表多次调用 plot
。 所以我的 for
循环无法删除。除了调用 plt.plot
之外,还有其他方法可以绘制虚线吗?
这是我得到的情节:
如您所见,我没有收到 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
数值打印完美。然而,它们并没有被绘制出来。
移除 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)
现在,我需要使用不同的 array
列表多次调用 plot
。 所以我的 for
循环无法删除。除了调用 plt.plot
之外,还有其他方法可以绘制虚线吗?
这是我得到的情节:
如您所见,我没有收到 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
数值打印完美。然而,它们并没有被绘制出来。
移除 for 循环:
ax.scatter(array[0],array[1],c='red')
ax.plot(array[0],array[1],'g--')
你的代码的问题是你迭代了行,这对于绘制单点(ax.scatter
)是很好的,但对于连接单点(ax.plot
和 '--'
选项):在每一行你只绘制那个点和它本身之间的线,这显然没有出现在图表中。