簡體   English   中英

在極圖中 plot 上方繪制刻度標簽

[英]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()

筆記:

  • 在第一個軸上,必須刪除 xticks 標簽
  • 激活第一個plt.show()以查看第一個軸: 在此處輸入圖像描述
  • 在第二個軸上,必須刪除 ytick labels = rlabels
  • 在第二個軸上,網格必須被移除(否則問題再次發生)

添加在:

  • 將 plot 移動到第二個軸,使粗體綠色邊緣區域也在灰色網格線之上
    • 仍然低於重疊標簽
    • 但是這些現在會與 rlabels 重疊,所以在那種情況下也必須移動它們
#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM