问题描述
我试图用matplotlib在条形图中绘制两组数据,所以我使用两个轴与 twinx()
方法。但是,第二个y轴标签被切断。我尝试了一些不成功的方法( tight_layout()
),在<$ c中设置 major_pad
$ c> rcParams 等等)。我觉得解决方案很简单,但我还没有遇到它。
I'm trying to plot two sets of data in a bar graph with matplotlib, so I'm using two axes with the twinx()
method. However, the second y-axis label gets cut off. I've tried a few different methods with no success (tight_layout()
, setting the major_pad
s in rcParams
, etc...). I feel like the solution is simple, but I haven't come across it yet.
这是一个MWE:
Here's a 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
边
plt .savefig(test.png)
sideplt.savefig("test.png")
推荐答案
我只知道它的诀窍是使用 bbox_inches = '紧'
在 savefig
。
I just figured it out: the trick is to use bbox_inches='tight'
in savefig
.
EG plt.savefig(test.png,bbox_inches ='tight')
这篇关于第二个y轴标签被切断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!