在matplotlib中,是否有一种方法可以将网格线设置在小节/线/面之下,同时保留上面的刻度标签? [英] In matplotlib, Is there a way to set gridlines below bars/lines/patches while retaining ticklabels above?

查看:114
本文介绍了在matplotlib中,是否有一种方法可以将网格线设置在小节/线/面之下,同时保留上面的刻度标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

> Matplotlib:在其他图形元素后面绘制网格线

,但对我没有任何帮助.

Related to Matplotlib: draw grid lines behind other graph elements, but nothing there worked for me.

我有以下绘图,我想在红线下隐藏网格线,同时将标签保留在红线的顶部:

I have the following plot where I want to hide the gridlines under the red line while retaining the labels on top of the red line:

import numpy as np
import matplotlib.pyplot as plt

#plot
r = np.arange(0, 3.0, 0.01)
theta = 2 * np.pi * r
ax = plt.subplot(111, polar=True)
ax.plot(theta, r, color='r', linewidth=20)
ax.set_rmax(2.0)
ax.grid(True, lw=2)
#set labels
label_pos = np.linspace(0.0, 2 * np.pi, 6, endpoint=False)
ax.set_xticks(label_pos)
label_cols = ['Label ' + str(num) for num in np.arange(6)]
ax.set_xticklabels(label_cols, size=24)

我可以用ax.set_axisbelow(True)在顶部显示红线.

I can get the red line on top with ax.set_axisbelow(True).

但是我找不到一种方法可以将红线保持在网格线的顶部,而将标签保持在红线的顶部.将zorder=-1添加到plot命令中,即使我添加ax.set_axisbelow(True)也将红线放在底部. ax.set_zorder(-1))到目前为止也没有用.

But I can't find a way to keep the red line on top of the gridlines while retaining the labels on top of the red line. Adding zorder=-1 to the plot command, puts the red line in the bottom even if I add ax.set_axisbelow(True). ax.set_zorder(-1)) has not worked so far either.

如何使网格线位于底部(最低zorder)中,然后是红线,然后是红线顶部的标签?

How can I get the grid lines in the bottom (lowest zorder) followed by the red line and then the labels on top of the red line?

推荐答案

您始终可以手动绘制网格:

You can always plot the grid manually:

import numpy as np
import matplotlib.pyplot as plt

#plot
r = np.arange(0, 3.0, 0.01)
theta = 2 * np.pi * r
rmax = 2.0
n_th = 6
th_pos = np.linspace(0.0, 2 * np.pi, n_th, endpoint=False)
n_r = 5
r_pos = np.linspace(0, rmax, n_r)


ax = plt.subplot(111, polar=True)

## Plot the grid    
for pos in th_pos:
    ax.plot([th_pos]*2, [0, rmax], 'k:', lw=2)
for pos in r_pos[1:-1]:
    x = np.linspace(0, 2*np.pi, 50)
    y = np.zeros(50)+pos
    ax.plot(x, y, 'k:', lw=2)

## Plot your data
ax.plot(theta, r, color='r', linewidth=20)
ax.set_rmax(rmax)
ax.grid(False)

#set ticks and labels
ax.set_xticks(th_pos)
label_cols = ['Label ' + str(num) for num in np.arange(n_th)]
ax.set_xticklabels(label_cols, size=24)
ax.set_yticks(r_pos[1:])


plt.show()

这篇关于在matplotlib中,是否有一种方法可以将网格线设置在小节/线/面之下,同时保留上面的刻度标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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