例如,当我尝试绘制点时
points = [(0,0),(0,1),(1,1,),(1,0)]
for point in points:
plt.plot(point[0], point[1], 'o')
plt.show()
会显示
这使得点很难看清。我怎样才能使 x, y 轴更宽,以便该点移动到绘图的中心?有点像
也就是说,我怎样才能将点的偏移值设置为plot的边界,这样它们就不会在拐角处偏移?
请您参考如下方法:
正如@Rob 所说,您可以使用
plt.xlim([xmin, xmax])
plt.ylim([ymin, ymax])
还值得注意的是,绘制所有 x 值与所有 y 值的关系效率更高。
points = [(0,0),(0,1),(1,1),(1,0)]
xs, ys = zip(*points)
plt.plot(xs, ys, 'o')
plt.xlim([-1, 2])
plt.ylim([-1, 2])
plt.show()
如果你想为每个点单独的颜色,你可以使用scatter:
points = [(0,0),(0,1),(1,1,),(1,0)]
xs, ys = zip(*points)
colors = range(len(xs))
plt.scatter(xs, ys, c=colors)
plt.xlim([-1, 2])
plt.ylim([-1, 2])
plt.show()
编辑:此外,事实证明,既然你想绘制点,你应该使用 scatter 而不是 plt 因为它会自动调整你的极限。寓意:使用适合工作的工具。见下文(忽略我奇怪的 matplotlibrc 设置):
points = [(0,0),(0,1),(1,1),(1,0)]
for point in points:
plt.plot(point[0], point[1], 'o')
plt.show()
points = [(0,0),(0,1),(1,1),(1,0)]
plt.scatter(*zip(*points))
plt.show()
