otm*_*ger 4 python plot matplotlib
我正在使用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,len(D)+1,1), D.keys(), size='small')
fig.savefig('D_bar.png')
Run Code Online (Sandbox Code Playgroud)
这是输出:

我怎样才能解决这个问题?谢谢
实际上,现在有一种自动方式:tight_layout.
在你的情况下:
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')
Run Code Online (Sandbox Code Playgroud)
根据matplotlib 邮件列表,没有自动方法可以做到这一点。但是,您可以通过使用figure.subplots_adjust方法手动调整jdust子图填充。在代码中放置fig.subplots_adjust(left = 0.4)后会产生以下结果:ax = fig.add_subplot(111)

| 归档时间: |
|
| 查看次数: |
2049 次 |
| 最近记录: |