[英]Matplotlib: Equal aspect ratio for one subplot only
我想繪制一個圖像及其沿兩個軸的軌跡。 我想要共享的軸(一條軌跡為x,另一條軌跡為y),各圖之間沒有空間,但圖像的縱橫比相等。 我在Python 3.6中工作。
使用GridSpec(我也嘗試過使用子圖),可以完成第一部分:
但是,如果我在圖像上施加相等的長寬比,則會得到
看來我不知道如何制作一個方形圖像,周圍沒有空格...
這是我的代碼的相關部分(我也有使用子圖的版本):
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')
有什么幫助嗎?
也許有某種巧妙的方法可以完全避免此問題,但是作為一種快速的解決方案,您可以使用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)
盡管您希望稍微改變刻度線,但這給了我:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.