问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/46680194/matplotlib-plt-plot-with-enumerate-not-working