Home > Software design >  Drawing tick labels above the plot in a polar graph
Drawing tick labels above the plot in a polar graph

Time:10-02

I've made a polar graph and I cannot figure out how to draw the tick labels (theta labels I guess?) above the gridlines of the graph. As you can see in the polar plot below, long labels are drawn below the gridlines in the graph. I was able to get them to be drawn above the spines with ax.set_axisbelow(False), but that did not affect the grid.

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')

labels are above the spines but below the grid

Some close ups

enter image description here

enter image description here

CodePudding user response:

The labels are linked to the axis and therefore unfortunately can't be 'shifted' on top with e.g. enter image description here

Code:

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()

Notes:

  • at the 1st axis the xticks labels have to be removed
  • activate the 1st plt.show() to see the first axis: enter image description here
  • at the 2nd axis the ytick labels = rlabels have to be removed
  • at the 2nd axis the grid has to be removed (otherwise the issue occurs again)

Add-on:

  • move the plot the the 2nd axis to have the areas bold green edge also on top of the grey gridlines
    • still below the overlapping labels
    • but these would now overlapping the rlabels, so those have to be moved in that case as well
#ax2.set_yticklabels([])  # remove the ytick labels = rlabels
    
ax2.set_thetagrids(np.rad2deg(angles))
ax2.set_rlabel_position(-90)

enter image description here

  • Related