使用 matplotlib,如何将轴标签的背景变白? [英] Using matplotlib, how do I whiten the background of the axis label?

查看:109
本文介绍了使用 matplotlib,如何将轴标签的背景变白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将轴标签的背景变白,这样当它穿过轴线本身时,后者就不会穿过它?

Is there a way to whiten out the background of the axis label so that when it crosses the axis line itself, the latter does not run through it?

例如,这个脚本(迄今为止我管理的最好的)

For example, this script (the best I managed so far)

#!/usr/bin/python 
import matplotlib.pyplot as plt

xx=[1,2,3]
yy=[2,3,4]
dy=[0.1,0.2,0.05]

fig=plt.figure()
ax=fig.add_subplot(111)

ax.errorbar(xx,yy,dy,fmt='ro-',ms=6,elinewidth=4)

ax.set_xlim([0.,3.4])
ax.set_ylim([0.,4.4])

ax.set_xlabel(r'$T/t$',fontsize=16)
ax.set_ylabel(r'$S(\mathbf{Q})L^{1+\eta}$',fontsize=16)

# position the axis labels
ax.xaxis.set_label_coords(1,0)
ax.yaxis.set_label_coords(0.1,0.93)
ax.yaxis.get_label().set_rotation('horizontal')
ax.yaxis.get_label().set_backgroundcolor('w')
#ax.yaxis.get_label().set_zorder(222)  #doesn't do the trick

plt.show()

几乎产生了我要查找的内容,但y轴仍然在标签上:.

produces almost what I'm looking for, but still the y-axis runs over the label: .

推荐答案

默认情况下,左书脊的zorder为2.5.出于某种原因,这似乎会引起问题;也许代码中有一些东西只有在它们是完整的时才有效?无论如何,如果您添加

By default, the left spine has a zorder of 2.5. For some reason this seems to cause problems; maybe there's something in the code which only works if they're integral? Anyway, if you add

ax.spines['left'].set_zorder(2)

或更笼统

ax.spines['left'].set_zorder(ax.yaxis.get_label().get_zorder()-1)

在演出前,它应该工作.另外,set_ylabel返回ylab对象本身,因此,如果使用"ylab = ax.set_ylabel(stuff)",则可以避免以后再调用所有ax.yaxis.get_label().

before the show, it should work. Also, set_ylabel returns the ylab object itself, so if you use "ylab = ax.set_ylabel(stuff)" you can avoid all the ax.yaxis.get_label() calls later.

这篇关于使用 matplotlib,如何将轴标签的背景变白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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