Matplotlib的set_方面似乎不起作用

2024-06-17 15:08:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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

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

更新

实际上,必须在调用show()之前设置axes()。为了完整起见,以下是该图的外观:

enter image description here


Tags: import脚本间隔可视化showpltcoords习惯
1条回答
网友
1楼 · 发布于 2024-06-17 15:08:34

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

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)

相关问题 更多 >