Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using matplotlib, how do I whiten the background of the axis label?

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()

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

like image 945
ev-br Avatar asked Jan 28 '26 22:01

ev-br


1 Answers

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)

or more generally

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

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.

example of box dominating spine

like image 189
DSM Avatar answered Jan 30 '26 11:01

DSM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!