第二个 y 轴标签被切断 [英] Second y-axis label getting cut off

查看:20
本文介绍了第二个 y 轴标签被切断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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_pads in rcParams, etc...). I feel like the solution is simple, but I haven't come across it yet.

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

side plt.savefig("test.png")

推荐答案

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

I just figured it out: the trick is to use bbox_inches='tight' in savefig.

E.G.plt.savefig("test.png",bbox_inches='tight')

这篇关于第二个 y 轴标签被切断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆