在 matplotlib 中,有没有办法在 bars/lines/patches 以下设置网格线,同时保留上面的刻度标签?
In matplotlib, Is there a way to set gridlines below bars/lines/patches while retaining ticklabels above?
与 Matplotlib: draw grid lines behind other graph elements 相关,但对我没有任何帮助。
我有以下情节,我想在红线下方隐藏网格线,同时将标签保留在红线上方:
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)
得到上面的红线。
但是我找不到一种方法可以将红线保持在网格线的顶部,同时将标签保留在红线的顶部。将 zorder=-1
添加到 plot 命令,即使我添加 ax.set_axisbelow(True)
也会将红线放在底部。 ax.set_zorder(-1))
到目前为止也没有工作。
如何获取底部(最低 zorder)的网格线,然后是红线,然后是红线顶部的标签?
您始终可以手动绘制网格:
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: draw grid lines behind other graph elements 相关,但对我没有任何帮助。
我有以下情节,我想在红线下方隐藏网格线,同时将标签保留在红线上方:
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)
得到上面的红线。
但是我找不到一种方法可以将红线保持在网格线的顶部,同时将标签保留在红线的顶部。将 zorder=-1
添加到 plot 命令,即使我添加 ax.set_axisbelow(True)
也会将红线放在底部。 ax.set_zorder(-1))
到目前为止也没有工作。
如何获取底部(最低 zorder)的网格线,然后是红线,然后是红线顶部的标签?
您始终可以手动绘制网格:
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()