[英]Drawing tick labels above the plot in a polar graph
我制作了一個極坐標圖,但我不知道如何在圖表的網格線上方繪制刻度標簽(我猜是 theta 標簽?)。 正如您在下面的極坐標 plot 中所見,圖中的網格線下方繪制了長標簽。 我能夠使用ax.set_axisbelow(False)
將它們繪制在脊椎上方,但這並不影響網格。
fig, ax = plt.subplots(dpi=300, subplot_kw=dict(polar=True))
sz = 10
angles = np.linspace(0, 2*np.pi, sz, endpoint=False)
angles=np.concatenate((angles,[angles[0]]))
thing = np.random.rand(sz)
vals = np.concatenate((thing,[thing[0]]))
labels = ['Lorem','ipsum','dolorrrrrrasdfasdrrr','sit','amet,','consectetuer','adipiscing asd asdff','elit.','Aenean','commod', 'Lorem']
ax.set_theta_offset(np.pi / 2.0)
ax.plot(angles, vals, color='green')
ax.fill(angles, vals, alpha=0.25, color='green')
ax.set_thetagrids(np.rad2deg(angles), labels, fontsize='medium')
ax.set_rticks(np.arange(0, 1.1, 0.1))
ax.tick_params(axis='y', which='major', labelsize=6)
ax.set_rlabel_position(-90)
ax.set_axisbelow(False) # places the ticks above the spines (green lines), but not above the grid
ax.spines[:].set_edgecolor('green')
一些特寫
標簽鏈接到軸,因此不幸的是不能用例如zorder
在頂部“移動”。
但是,第二個軸位於第一個軸之上,可用作解決此問題的解決方法:
代碼:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
fig, ax = plt.subplots(dpi=300, subplot_kw=dict(polar=True))
sz = 10
angles = np.linspace(0, 2*np.pi, sz, endpoint=False)
angles=np.concatenate((angles,[angles[0]]))
thing = np.random.rand(sz)
vals = np.concatenate((thing,[thing[0]]))
labels = ['Lorem','ipsum','dolorrrrrrasdfasdrrr','sit','amet,','consectetuer',
'adipiscing asd asdff','elit.','Aenean','commod', 'Lorem']
ax.set_theta_offset(np.pi / 2.0)
ax.set_thetagrids(np.rad2deg(angles))
ax.plot(angles, vals, color='green')
ax.fill(angles, vals, alpha=0.25, color='green')
ax.set_rticks(np.arange(0, 1.1, 0.1))
ax.tick_params(axis='y', which='major', labelsize=6)
ax.set_rlabel_position(-90)
ax.spines[:].set_edgecolor('green')
ax.set_xticklabels([]) # remove the xticks labels
# plt.show() # activate to see the stripped first axis
# 2nd axis just for the labels
ax2 = ax.figure.add_axes(ax.get_position(), polar=True, #projection='polar',
label=labels, frameon=False)
ax2.set_theta_offset(np.pi / 2.0)
ax2.set_yticklabels([]) # remove the yticks labels = rlabels
ax2.set_thetagrids(np.rad2deg(angles), labels, fontsize='medium')
ax2.set_rticks(np.arange(0, 1.1, 0.1))
ax2.grid(False) # remove the grid of the 2nd axis
ax2.tick_params(axis='y', which='major', labelsize=6)
# optional plot area green edge also on top
# ax2.plot(angles, vals, color='green')
plt.show()
筆記:
plt.show()
以查看第一個軸: 添加在:
#ax2.set_yticklabels([]) # remove the ytick labels = rlabels
ax2.set_thetagrids(np.rad2deg(angles))
ax2.set_rlabel_position(-90)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.