zje*_*zje 27 python graph matplotlib
我试图用matplotlib在条形图中绘制两组数据,所以我在方法中使用了两个轴twinx().但是,第二个y轴标签被切断.我尝试了几种不同的方法但没有成功(tight_layout()设置major_pads rcParams等等).我觉得解决方案很简单,但我还没有遇到过.
这是一个MWE:
#!/usr/bin/env python
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
matplotlib.rcParams.update({'font.size': 21})
ax = plt.gca()
plt.ylabel('Data1') #Left side
ax2 = ax.twinx()
for i in range(10):
if(i%2==0):
ax.bar(i,np.random.randint(10))
else:
ax2.bar(i,np.random.randint(1000),color='k')
plt.ylabel('Data2') #Right
Run Code Online (Sandbox Code Playgroud)
side plt.savefig("test.png")

zje*_*zje 78
我想通了:关键是要使用bbox_inches='tight'在savefig.
例如 plt.savefig("test.png",bbox_inches='tight')

小智 7
我遇到了同样的问题,但plt.tight_layout()没有自动解决。相反,我在/
中使用了 labelpad 参数:ylabelset_ylabel
ax.set_ylabel('label here', rotation=270, color='k', labelpad=15)
我猜当你问这个问题时,这还没有实现,但由于它是谷歌上的最高结果,希望它可以帮助当前 matplotlib 版本的用户。
| 归档时间: |
|
| 查看次数: |
25299 次 |
| 最近记录: |