不正确的 matplotlib 图
Incorrect matplotlib plot
我一直在尝试使用以下数据点集在 matplotlib 上绘制一个简单的绘图,但我得到的绘图不正确,这完全令人困惑。该图包括不在数据点集中的点。
我正在绘制的点集是:
[(0, 0), (3, 0), (0, 0), (2, 0), (0, 0), (3, 0), (1, 0), (7, 0), (2, 0), (0, 0), (5, 0), (2, 1), (10, 1), (1, 0), (1, 0), (8, 0), (3, 0), (1, 0), (2, 0), (2, 0), (1, 0), (6, 1), (3, 0), (3, 0), (12, 1), (3, 0), (0, 0), (2, 0), (0, 0), (2, 0), (3, 1), (0, 0), (4, 0), (4, 0), (2, 0), (2, 0)]
我只是在打电话:
plt.plot(pts, 'ro')
我很想知道我这里哪里出了问题。提前致谢。
"Set of points" 让我觉得你想要一个散点图。如果您期待这样的结果:
那么您可能需要 pyplot 的 scatter()
函数。
import matplotlib.pyplot as plt
data = [(0, 0), (3, 0), (0, 0), (2, 0), (0, 0), (3, 0), (1, 0), (7, 0), (2, 0), (0, 0), (5, 0), (2, 1), (10, 1), (1, 0), (1, 0), (8, 0), (3, 0), (1, 0), (2, 0), (2, 0), (1, 0), (6, 1), (3, 0), (3, 0), (12, 1), (3, 0), (0, 0), (2, 0), (0, 0), (2, 0), (3, 1), (0, 0), (4, 0), (4, 0), (2, 0), (2, 0)]
x,y = zip(*data)
#plt.plot(data, 'ro') # is the same as
#plt.plot(x, 'ro') # this
plt.scatter(x, y) # but i think you want scatter
plt.show()
对于plot()
注意:
If x and/or y is 2-dimensional, then the corresponding columns will be plotted.
目前,matplotlib 认为您正在尝试根据元组的索引绘制元组的每个条目。也就是说,您的情节具有点 (i, x_i) 和 (i, y_i),'i' 从 1 到 35。
正如@jedwards 指出的那样,您可以使用分散函数。
或者,您可以通过如下提取元组的每个元素来使 plot 函数明确地 plot (x_i, y_i):
import matplotlib.pyplot as plt
data = [(0, 0), (3, 0), (0, 0), (2, 0), (0, 0), (3, 0), (1, 0), (7, 0), (2, 0), (0, 0), (5, 0), (2, 1), (10, 1), (1, 0), (1, 0), (8, 0), (3, 0), (1, 0), (2, 0), (2, 0), (1, 0), (6, 1), (3, 0), (3, 0), (12, 1), (3, 0), (0, 0), (2, 0), (0, 0), (2, 0), (3, 1), (0, 0), (4, 0), (4, 0), (2, 0), (2, 0)]
plt.plot([int(i[0]) for i in data], [int(i[1]) for i in data], 'or')
plt.xlim(-1, 8) # Sets x-axis limits
plt.ylim(-1, 2) # Sets y-axis limits
plt.show() # Show the plot
我一直在尝试使用以下数据点集在 matplotlib 上绘制一个简单的绘图,但我得到的绘图不正确,这完全令人困惑。该图包括不在数据点集中的点。
我正在绘制的点集是:
[(0, 0), (3, 0), (0, 0), (2, 0), (0, 0), (3, 0), (1, 0), (7, 0), (2, 0), (0, 0), (5, 0), (2, 1), (10, 1), (1, 0), (1, 0), (8, 0), (3, 0), (1, 0), (2, 0), (2, 0), (1, 0), (6, 1), (3, 0), (3, 0), (12, 1), (3, 0), (0, 0), (2, 0), (0, 0), (2, 0), (3, 1), (0, 0), (4, 0), (4, 0), (2, 0), (2, 0)]
我只是在打电话:
plt.plot(pts, 'ro')
我很想知道我这里哪里出了问题。提前致谢。
"Set of points" 让我觉得你想要一个散点图。如果您期待这样的结果:
那么您可能需要 pyplot 的 scatter()
函数。
import matplotlib.pyplot as plt
data = [(0, 0), (3, 0), (0, 0), (2, 0), (0, 0), (3, 0), (1, 0), (7, 0), (2, 0), (0, 0), (5, 0), (2, 1), (10, 1), (1, 0), (1, 0), (8, 0), (3, 0), (1, 0), (2, 0), (2, 0), (1, 0), (6, 1), (3, 0), (3, 0), (12, 1), (3, 0), (0, 0), (2, 0), (0, 0), (2, 0), (3, 1), (0, 0), (4, 0), (4, 0), (2, 0), (2, 0)]
x,y = zip(*data)
#plt.plot(data, 'ro') # is the same as
#plt.plot(x, 'ro') # this
plt.scatter(x, y) # but i think you want scatter
plt.show()
对于plot()
注意:
If x and/or y is 2-dimensional, then the corresponding columns will be plotted.
目前,matplotlib 认为您正在尝试根据元组的索引绘制元组的每个条目。也就是说,您的情节具有点 (i, x_i) 和 (i, y_i),'i' 从 1 到 35。
正如@jedwards 指出的那样,您可以使用分散函数。 或者,您可以通过如下提取元组的每个元素来使 plot 函数明确地 plot (x_i, y_i):
import matplotlib.pyplot as plt
data = [(0, 0), (3, 0), (0, 0), (2, 0), (0, 0), (3, 0), (1, 0), (7, 0), (2, 0), (0, 0), (5, 0), (2, 1), (10, 1), (1, 0), (1, 0), (8, 0), (3, 0), (1, 0), (2, 0), (2, 0), (1, 0), (6, 1), (3, 0), (3, 0), (12, 1), (3, 0), (0, 0), (2, 0), (0, 0), (2, 0), (3, 1), (0, 0), (4, 0), (4, 0), (2, 0), (2, 0)]
plt.plot([int(i[0]) for i in data], [int(i[1]) for i in data], 'or')
plt.xlim(-1, 8) # Sets x-axis limits
plt.ylim(-1, 2) # Sets y-axis limits
plt.show() # Show the plot