gpt4 book ai didi

python - 第二个 y 轴标签被切断

转载 作者:IT老高 更新时间:2023-10-28 22:16:34 26 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 在条形图中绘制两组数据,因此我在 twinx() 方法中使用了两个轴。但是,第二个 y 轴标签被截断。我尝试了几种不同的方法,但均未成功(tight_layout()、在 rcParams 中设置 major_pad 等...)。我觉得解决方法很简单,但我还没有遇到过。

这是一个 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")

Sample graph with Data2 cut off

最佳答案

我刚刚想通了:诀窍是在 savefig 中使用 bbox_inches='tight'

例如plt.savefig("test.png",bbox_inches='tight')

fixed now

关于python - 第二个 y 轴标签被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21288062/

26 4 0