Matplotlib:仅一个子图具有相等的长宽比

时间:2019-03-13 15:29:28

标签: python python-3.x matplotlib plot

我想绘制一个图像以及沿着其两个轴的迹线。我想要共享的轴(一条轨迹为x,另一条轨迹为y),各图之间没有空间,但图像的纵横比相等。我使用Python 3.6。

使用GridSpec(我也尝试过使用子图),可以完成第一部分:

no_space

但是,如果我在图像上施加相等的宽高比,我会得到

enter image description here

似乎我不知道如何制作一个方形图像,周围没有空格...

这是我代码的相关部分(我也有使用子图的版本):

Get-ADUser -Filter <string>
[-ResultPageSize <int>]
[-ResultSetSize <System.Nullable[System.Int32]>]
[-SearchBase <string>]
[-SearchScope {Base | OneLevel | Subtree}]
[-AuthType {Negotiate | Basic}]
[-Credential <PSCredential>]
[-Partition <string>]
[-Properties <string[]>]
[-Server <string>]
[<CommonParameters>]

有帮助吗?

1 个答案:

答案 0 :(得分:0)

也许有某种巧妙的方法可以完全避免此问题,但是作为一种快速的解决方案,您可以使用subplots_adjust

import matplotlib.pyplot as plt

h, w = plt.figaspect(1)
fig = plt.figure(figsize = (h, w))
grid = fig.add_gridspec(nrows = 2, ncols = 2, 
          hspace = 0, wspace = 0, width_ratios = [2, 1], 
          height_ratios = [1, 2])
ax = fig.add_subplot(grid[1,0])
ay = fig.add_subplot(grid[0,0], sharex = ax)
az = fig.add_subplot(grid[1,1], sharey = ax)
plt.setp(ay.get_xticklabels(), visible = False)
plt.setp(az.get_yticklabels(), visible = False)

# Add this for square image
ax.set_aspect('equal')

# Adjust subplots
plt.subplots_adjust(top=0.9)

尽管您希望稍微改变刻度线,但这给了我

enter image description here

相关问题