加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python matplotlib:barh plot显示不完整的YTick标签 – 如何动

发布时间:2020-12-20 11:26:44 所属栏目:Python 来源:网络整理
导读:我正在使用matplotlib将barh绘图绘制到文件中.不幸的是,YTickLaels有点太长了,情节区域不会自动向右移动.有没有办法自动将绘图区域向右移动,所以我不会遇到不完整的YTickLabels问题? 我使用的代码如下: import matplotlib as mplmpl.use('Agg')import matp
我正在使用matplotlib将barh绘图绘制到文件中.不幸的是,YTickLaels有点太长了,情节区域不会自动向右移动.有没有办法自动将绘图区域向右移动,所以我不会遇到不完整的YTickLabels问题?

我使用的代码如下:

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,D.keys(),size='small')
fig.savefig('D_bar.png')

这是输出:

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

解决方法

实际上,现在有一种自动方式: tight_layout.

在你的情况下:

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
D = {u'Label1':26,size='small')
fig.tight_layout()
fig.savefig('D_bar.png')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读