python+;matplotlib:barh plot显示不完整的YTick标签-如何动态向右移动打印区域以适合给定的YTick标签?

python+;matplotlib:barh plot显示不完整的YTick标签-如何动态向右移动打印区域以适合给定的YTick标签?,python,matplotlib,plot,Python,Matplotlib,Plot,我正在使用matplotlib将barh打印到文件。不幸的是,YTickLaels有点太长,绘图区域不会自动向右移动。有没有办法自动将绘图区域向右移动,这样我就不会遇到标签不完整的问题 我使用的代码如下所示: import matplotlib as mpl mpl.use('Agg') import matplotlib.pyplot as plt D = {u'Label1':26, u'Label2 is longer than others': 17, u'Label3 is not s

我正在使用
matplotlib
barh
打印到文件。不幸的是,YTickLaels有点太长,绘图区域不会自动向右移动。有没有办法自动将绘图区域向右移动,这样我就不会遇到标签不完整的问题

我使用的代码如下所示:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
D = {u'Label1':26, u'Label2 is longer than others': 17, u'Label3 is not so short either':30}
fig = plt.figure(figsize=(5.5,3),dpi=300)
ax = fig.add_subplot(111)
ax.grid(True,which='both')
bar = ax.barh(range(1,len(D)+1,1),D.values(),0.4,align='center')
plt.yticks(range(1,len(D)+1,1), D.keys(), size='small')
fig.savefig('D_bar.png')
以下是输出:

我怎样才能解决这个问题?谢谢

根据,没有自动执行此操作的方法。但是,您可以使用方法手动调整子批次填充。将
fig.subplot\u adjust(left=0.4)
放在
ax=fig.add\u subplot(111)
之后的代码中会产生以下结果:


实际上,现在有一种自动的方法可以做到这一点:

就你而言:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
D = {u'Label1':26, u'Label2 is longer than others': 17, 
     u'Label3 is not so short either':30}
fig = plt.figure(figsize=(5.5,3),dpi=300)
ax = fig.add_subplot(111)
ax.grid(True,which='both')
bar = ax.barh(range(1,len(D)+1,1),D.values(),0.4,align='center')
plt.yticks(range(1,len(D)+1,1), D.keys(), size='small')
fig.tight_layout() # <---- ADD THIS
fig.savefig('D_bar.png')
将matplotlib导入为mpl
mpl.use('Agg')
将matplotlib.pyplot作为plt导入
D={u'Label1':26,u'Label2比其他的长:17,
u'Label3也不那么短:30}
图=plt.图(图尺寸=(5.5,3),dpi=300)
ax=图添加_子批次(111)
ax.grid(True,其中class='tware')
bar=ax.barh(范围(1,len(D)+1,1),D.values(),0.4,align='center')
plt.yticks(范围(1,len(D)+1,1),D.keys(),size='small')

fig.tight_layout()#没有帮助?如果您将kwarg
tight_layout
传递给
plt。figure
它也会自动神奇地工作(但这可能是一个1.3.x功能)调用
tight_layout
,以上述任何一种方式调用都会阻止剪掉标签,但会使绘图更窄。知道我会有一个非常宽的png文件,我如何保持标签和打印大小?