Python 使轴在matplotlib pyplot中的长度相同

Python 使轴在matplotlib pyplot中的长度相同,python,matplotlib,Python,Matplotlib,我想让正方形独立于轴单位。例如,我知道我可以将图形尺寸设置为与图形相等(figsize=(10,10)),并且我可以将轴比例设置为与set_aspect('equal')相等,但是我如何强制实际轴长度相等,例如,使X轴和Y轴每10英寸长 编辑示例代码 import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from matplotlib.ticker import MaxNLocator import nump

我想让正方形独立于轴单位。例如,我知道我可以将图形尺寸设置为与
图形相等(figsize=(10,10))
,并且我可以将轴比例设置为与
set_aspect('equal')
相等,但是我如何强制实际轴长度相等,例如,使X轴和Y轴每10英寸长

编辑示例代码

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator
import numpy as np

x1 = 1
y1 = [10., 1000.]
err1 = 0.00865
x2 = 2
y2 = [9., 900.]
err2 = 0.00658

len_xaxis,len_yaxis = 5.,5. #fix here your numbers
xspace, yspace = .9, .9 # change the size of the void border here.
x_fig,y_fig = len_xaxis / xspace, len_yaxis / yspace

for i in range(2):
    plt.clf()
    #    fig = plt.figure(figsize=(6, 6))
    fig = plt.figure(figsize=(x_fig,y_fig))
    plt.subplots_adjust(left=1-xspace, right = xspace, top=yspace, bottom = 1-yspace)
    gs = gridspec.GridSpec(3, 1)
    gs.update(hspace=0., wspace=0.)
    ax1 = plt.subplot(gs[0:2, 0])

    ax1.errorbar(x1, y1[i], yerr=err1)
    ax1.errorbar(x2, y2[i], yerr=err2)

    ax1.invert_yaxis()

    plt.setp(ax1.get_xticklabels(), visible=False)  # Remove x-labels between the plots
    plt.xlim(0, 3)

    ax2 = plt.subplot(gs[2, 0], sharex=ax1)

    nbins = len(ax1.get_yticklabels())
    ax1.yaxis.set_major_locator(MaxNLocator(nbins=8, prune='both'))
    nbins = len(ax2.get_yticklabels())
    ax2.yaxis.set_major_locator(MaxNLocator(nbins=6, prune='both'))

    plt.tight_layout()
    plt.savefig('prune_%d.png' % i)
    plt.close()

plt.subplot\u adjust()
功能。5英寸而非10英寸的示例:

len_xaxis,len_yaxis = 5.,5. #fix here your numbers

xspace, yspace = .9, .9 # change the size of the void border here.

x_fig,y_fig = len_xaxis / xspace, len_yaxis / yspace
figure(figsize=(x_fig,y_fig))
plt.subplots_adjust(left=1-xspace, right = xspace, top=yspace, bottom = 1-yspace)
plot([1,2,3],[-1,3,5])

那不行。根据不同绘图中的数据(因此opn轴标签的长度),我在所有绘图中获得相同的高度,但不是相同的宽度。x轴和y轴没有固定的英寸长度比。我用示例I postet尝试了您的代码。根据我使用的y值(即y记号标签有多少位数),轴长比也会随着代码的添加而改变。这不是一个示例。我看了一下代码,解决方案成功了,两个轴(顶部和底部)总共占据了5x5英寸。请用一个失败的例子来编辑这个问题。谢谢,我已经包括了一个例子和你的建议。你是对的,你的代码是有效的。但是,我使用plt.tight_layout(),它似乎覆盖了调整,我在两个图中得到了不同的轴长比。有没有办法绕过这一点?我并不是被奖励的。基本上,该命令更改plt的相同参数。subplot_adjust()覆盖它