是否可以设置更高的轴标签的z索引? [英] Is it possible to set higher z-index of the axis labels?

查看:56
本文介绍了是否可以设置更高的轴标签的z索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,轴的代码标签隐藏在图形线的后面:

将 numpy 导入为 np导入matplotlib.pyplot作为pltX = np.linspace(-np.pi,np.pi,256,端点=真)C,S = np.cos(X), np.sin(X)plt.plot(X,C)plt.plot(X,S)plt.xticks([-np.pi,-np.pi/2,0,np.pi/2,np.pi],[r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])plt.yticks([-1, 0, +1],[r'$-1$', r'$0$', r'$+1$'])ax = plt.gca()ax.spines ['right'].set_color('none')ax.spines['top'].set_color('none')ax.xaxis.set_ticks_position('底部')ax.spines['bottom'].set_position(('data',0))ax.yaxis.set_ticks_position('left')ax.spines['left'].set_position(('data',0))用于ax.get_xticklabels()+ ax.get_yticklabels()中的标签:label.set_fontsize(14)label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.75))plt.show()

您会发现很难看到例如 -pi .有没有办法把轴标签放在前面?

解决方案

有一个

In the following code labels of the axis get hidden behind the graph lines:

import numpy as np
import matplotlib.pyplot as plt

X = np.linspace(-np.pi, np.pi, 256, endpoint=True)
C,S = np.cos(X), np.sin(X)

plt.plot(X,C)
plt.plot(X,S)

plt.xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
           [r'$-\pi$', r'$-\pi/2$', r'$0$', r'$+\pi/2$', r'$+\pi$'])

plt.yticks([-1, 0, +1],
           [r'$-1$', r'$0$', r'$+1$'])

ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

for label in ax.get_xticklabels() + ax.get_yticklabels():
    label.set_fontsize(14)
    label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.75 ))

plt.show()

You can see that for instance -pi is hard to see. Is there a way to put axis labels up front?

解决方案

There is an rcParameter axes.axisbelow which steers exactly this behaviour:

axes.axisbelow: draw axis gridlines and ticks

  • below patches (True);
  • above patches but below lines ('line');
  • or above all (False)

If you set

plt.rcParams["axes.axisbelow"] = False

at the top of the script, gridlines and tick(label)s are drawn on top of everything else. The result would then look like

这篇关于是否可以设置更高的轴标签的z索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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