matplotlib中设置轴标题标签的方法

问题描述

在这里插入图片描述当我们在用matplotlib画图的时候,有的时候,我们会发现加上轴标签后,图会变小一点,而轴标题离图的边缘比较远,如上图所示(上图y左轴标签的留白也比较严重)。
上图的源码如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')

time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10

fig = plt.figure(figsize=(6.8,5))
ax = fig.add_subplot(111)
ax.plot(time, Swdown, '-', label = 'Swdown')
ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
ax2.plot(time, temp, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)"")
ax2.set_ylabel(r"Temperature ($^\circ$C)",r")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
ax2.legend(loc=0)
plt.savefig('0.png')
plt.show()

解决方法

查看了matplotlib的相关描述,发现轴标签(label)的文字使调用了text的方法,如下:
在这里插入图片描述我们知道text的方法是有对齐操作的,所以为了解决留白较大的问题,我们可以设置一下水平对齐ha,垂直对齐va。对齐的时候,是将标签视为水平放置时的情况来定义的,即在这里插入图片描述
我对左右的y标签分别进行了对齐,然后重新绘图,代码如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')

time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10

fig = plt.figure(figsize=(6.8,5))
ax = fig.add_subplot(111)
ax.plot(time, Swdown, '-', label = 'Swdown')
ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
ax2.plot(time, temp, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)",va="top",ha="center")
ax2.set_ylabel(r"Temperature ($^\circ$C)",va="center",ha="center")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
ax2.legend(loc=0)
plt.savefig('0.png')
plt.show()

得到的结果如下,可见,左右都比较贴近轴:
在这里插入图片描述
但是离图片边缘比较远,这个时候可以使用tight_layout,这是matplotlib给的紧密布局方法,有时却无效,这次我尝试了一下,是不成功了,但是我用调整子图的subplots_adjust方法,成功了:
在这里插入图片描述
代码如下:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')

time = np.arange(10)
temp = np.random.random(10)*30
Swdown = np.random.random(10)*100-10
Rn = np.random.random(10)*100-10

fig = plt.figure(figsize=(6.8,5))
ax = fig.add_subplot(111)
ax.plot(time, Swdown, '-', label = 'Swdown')
ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
ax2.plot(time, temp, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)",va="top",ha="center")
ax2.set_ylabel(r"Temperature ($^\circ$C)",va="top",ha="center")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
ax2.legend(loc=1)
fig.subplots_adjust(0.08, 0.1, 0.92, 0.95)
plt.savefig('0.png')
#plt.show()
#无效,啧啧
plt.tight_layout()

总结

matplotlib绘图,入门我觉得挺繁琐,普通的图效率比较低下,但是一些组成图还是比较好…

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月司

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值