Matplotlib的set_aspect似乎不起作用

时间:2017-09-21 08:33:53

标签: python matplotlib plot

我正在尝试使用PyPlot可视化一组坐标,但我似乎没有像我在MATLAB中习惯的那样得到“方形”或“相等”的轴。以下脚本

import matplotlib.pyplot as plt

coords = [
          (20833.3333, 17100.0000),
          (20900.0000, 17066.6667),
          (21300.0000, 13016.6667),
          (21600.0000, 14150.0000),
          (21600.0000, 14966.6667),
          (21600.0000, 16500.0000),
          (22183.3333, 13133.3333),
          (22583.3333, 14300.0000),
          (22683.3333, 12716.6667),
          (23616.6667, 15866.6667),
          (23700.0000, 15933.3333),
          (23883.3333, 14533.3333),
          (24166.6667, 13250.0000),
          (25149.1667, 12365.8333),
          (26133.3333, 14500.0000),
          (26150.0000, 10550.0000),
          (26283.3333, 12766.6667),
          (26433.3333, 13433.3333),
          (26550.0000, 13850.0000),
          (26733.3333, 11683.3333),
          (27026.1111, 13051.9444),
          (27096.1111, 13415.8333),
          (27153.6111, 13203.3333),
          (27166.6667, 9833.3333),
          (27233.3333, 10450.0000)
          ]

x, y = zip(*coords)


plt.plot(x, y, '.')
plt.show()
plt.axes().set_aspect('equal')

导致一个看起来像这样的情节:

enter image description here

对我来说,间隔1000之间的间距在y轴上看起来更宽。为什么set_aspect没有像我期望的那样工作?

更新

确实必须在调用axes()之前设置show() 。为了完整起见,这里的情节应该是这样的:

enter image description here

1 个答案:

答案 0 :(得分:3)

您在查看绘图后正在更改方面。这对我有用:

import matplotlib.pyplot as plt

coords = [
          (20833.3333, 17100.0000),
          (20900.0000, 17066.6667),
          (21300.0000, 13016.6667),
          (21600.0000, 14150.0000),
          (21600.0000, 14966.6667),
          (21600.0000, 16500.0000),
          (22183.3333, 13133.3333),
          (22583.3333, 14300.0000),
          (22683.3333, 12716.6667),
          (23616.6667, 15866.6667),
          (23700.0000, 15933.3333),
          (23883.3333, 14533.3333),
          (24166.6667, 13250.0000),
          (25149.1667, 12365.8333),
          (26133.3333, 14500.0000),
          (26150.0000, 10550.0000),
          (26283.3333, 12766.6667),
          (26433.3333, 13433.3333),
          (26550.0000, 13850.0000),
          (26733.3333, 11683.3333),
          (27026.1111, 13051.9444),
          (27096.1111, 13415.8333),
          (27153.6111, 13203.3333),
          (27166.6667, 9833.3333),
          (27233.3333, 10450.0000)
          ]

x, y = zip(*coords)


plt.plot(x, y, '.')
plt.axes().set_aspect('equal')
plt.show()

如果需要指定框的大小或宽高比,可以使用命令set_size_inches。我会将plt.axes().set_aspect('equal')替换为plt.gcf().set_size_inches(size),其中size是以英寸为单位的轴长度元组(例如size=(6,6)