Python 2.7 设置轴高度-将轴高度拉伸到图例高度

Python 2.7 设置轴高度-将轴高度拉伸到图例高度,python-2.7,matplotlib,legend,axes,Python 2.7,Matplotlib,Legend,Axes,我有一个图例,其高度大于轴高度(如下面代码的结果)。现在,我想拉伸轴的高度,使其在图例的末端结束 import matplotlib.pyplot as plt import numpy as np t = np.arange(0., 10.5, 0.5) fig, ax = plt.subplots() for c in range(0, 20): ax.plot(t, t*c/2, label='func {}'.format(c)) ax.legend(bbox_to_anch

我有一个图例,其高度大于轴高度(如下面代码的结果)。现在,我想拉伸轴的高度,使其在图例的末端结束

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0., 10.5, 0.5)

fig, ax = plt.subplots()
for c in range(0, 20):
    ax.plot(t, t*c/2, label='func {}'.format(c))
ax.legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0.)

我想要的是这样的东西(如果网格底部或刻度底部以图例结尾,这并不重要)

我试图通过附加以下代码来实现我的目标,但没有任何效果(legend_h始终=1.0):

legend\u h=ax.get\u legend().get\u window\u extent().height
ax\u height=ax.get\u window\u extent()高度
如果最大高度<图例高度:
fig.set_figheight(图例h/ax_高度*fig.get_figheight())
进一步说,如果我能只改变轴本身的属性而不是整个图形的属性,那就太好了

编辑:
我的主要目的是从脚本运行图形生成,但我也在Ipython笔记本中尝试过。一种尝试是在获取高度和设置新的图形高度之前临时存储图形。但是这也没有产生正确的结果。

我认为你可以通过简单地将
plt.draw()
添加到你已经拥有的内容中来实现你想要的,例如

fig, ax = plt.subplots()
for c in range(0, 20):
    ax.plot(t, t*c/2, label='func {}'.format(c))
ax.legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0.)

plt.draw()

legend_h = ax.get_legend().get_window_extent().height
ax_height = ax.get_window_extent().height

if ax_height < legend_h:
    fig.set_figheight(legend_h/ax_height * fig.get_figheight())

我认为你可以通过简单地将
plt.draw()
添加到你已经拥有的内容中来实现你想要的,例如

fig, ax = plt.subplots()
for c in range(0, 20):
    ax.plot(t, t*c/2, label='func {}'.format(c))
ax.legend(bbox_to_anchor=(1.01, 1), loc=2, borderaxespad=0.)

plt.draw()

legend_h = ax.get_legend().get_window_extent().height
ax_height = ax.get_window_extent().height

if ax_height < legend_h:
    fig.set_figheight(legend_h/ax_height * fig.get_figheight())

原则上,@mattpitkin的回答表明了正确的方法。但是,与其使用
set\u figheight
,不如使用
set\u size\u inches
。计算还需要包括图形边距,该边距可从
fig.subplotpars
获得

除了高度之外,我们还可以设置图形的宽度,以便包含图例

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(0,10); c=20

fig, ax = plt.subplots()
for c in range(0, 20):
    ax.plot(t, t*c/2, label='func {}'.format(c))
bbox = (1.01,1)    
ax.legend(bbox_to_anchor=bbox, loc=2, borderaxespad=0.)

fig.canvas.draw()

legend_h = ax.get_legend().get_window_extent().height
ax_height = ax.get_window_extent().height
if ax_height < legend_h:
    w,h = fig.get_size_inches()
    h =legend_h/fig.dpi/(fig.subplotpars.top-fig.subplotpars.bottom)
    fig.set_size_inches(w,h)

# set width as well
w,h = fig.get_size_inches()
r = ax.get_legend().get_window_extent().width/fig.dpi/w
fig.subplots_adjust(right=1-1.1*r)
plt.show()
导入matplotlib.pyplot作为plt
将numpy作为np导入
t=np.linspace(0,10);c=20
图,ax=plt.子批次()
对于范围(0,20)内的c:
绘图(t,t*c/2,label='func{}'。格式(c))
bbox=(1.01,1)
ax.图例(bbox\u至锚=bbox,loc=2,borderaxespad=0)
图canvas.draw()
legend_h=ax.get_legend().get_window_extent().height
ax\u height=ax.get\u window\u extent()高度
如果最大高度<图例高度:
w、 h=图获取尺寸英寸()
h=图例h/图dpi/(图子地块顶部-图子地块底部)
图设置尺寸英寸(宽、高)
#同时设置宽度
w、 h=图获取尺寸英寸()
r=ax.get_legend().get_window_extent().width/fig.dpi/w
图子批次调整(右=1-1.1*r)
plt.show()
下面的图片是作为脚本运行时显示的


在Ipython或jupyter中,图形将自动裁剪或展开,因为显示的png将使用
bbox\u inches='tight'
选项自动保存。因此,jupyter笔记本电脑不需要调整宽度。

原则上,@Matt Pitkin的回答显示了正确的方法。但是,与其使用
set\u figheight
,不如使用
set\u size\u inches
。计算还需要包括图形边距,该边距可从
fig.subplotpars
获得

除了高度之外,我们还可以设置图形的宽度,以便包含图例

import matplotlib.pyplot as plt
import numpy as np

t = np.linspace(0,10); c=20

fig, ax = plt.subplots()
for c in range(0, 20):
    ax.plot(t, t*c/2, label='func {}'.format(c))
bbox = (1.01,1)    
ax.legend(bbox_to_anchor=bbox, loc=2, borderaxespad=0.)

fig.canvas.draw()

legend_h = ax.get_legend().get_window_extent().height
ax_height = ax.get_window_extent().height
if ax_height < legend_h:
    w,h = fig.get_size_inches()
    h =legend_h/fig.dpi/(fig.subplotpars.top-fig.subplotpars.bottom)
    fig.set_size_inches(w,h)

# set width as well
w,h = fig.get_size_inches()
r = ax.get_legend().get_window_extent().width/fig.dpi/w
fig.subplots_adjust(right=1-1.1*r)
plt.show()
导入matplotlib.pyplot作为plt
将numpy作为np导入
t=np.linspace(0,10);c=20
图,ax=plt.子批次()
对于范围(0,20)内的c:
绘图(t,t*c/2,label='func{}'。格式(c))
bbox=(1.01,1)
ax.图例(bbox\u至锚=bbox,loc=2,borderaxespad=0)
图canvas.draw()
legend_h=ax.get_legend().get_window_extent().height
ax\u height=ax.get\u window\u extent()高度
如果最大高度<图例高度:
w、 h=图获取尺寸英寸()
h=图例h/图dpi/(图子地块顶部-图子地块底部)
图设置尺寸英寸(宽、高)
#同时设置宽度
w、 h=图获取尺寸英寸()
r=ax.get_legend().get_window_extent().width/fig.dpi/w
图子批次调整(右=1-1.1*r)
plt.show()
下面的图片是作为脚本运行时显示的


在Ipython或jupyter中,图形将自动裁剪或展开,因为显示的png将使用
bbox\u inches='tight'
选项自动保存。因此,jupyter笔记本不需要进行宽度调整。

是的,这在Ipython笔记本中起作用,但在脚本中不起作用。请尝试在末尾添加
fig.tight_layout()
(尽管这可能会裁剪图例)@AnnetteC-我正在编辑答案,以包含来自的内容。试一试。是的,这在Ipython笔记本中正在运行,但不是在脚本中。尝试在末尾添加
fig.tight_layout()
(尽管这可能会稍微裁剪图例)@AnnetteC-我正在编辑答案,以包括来自的内容。试试看。