Tick label padding and tick label position on polar / radial chart - matplotlib

Viewed 755

I am trying to rotate the radial tick labels on the attached plot.

Why does matplotlib not rotate them when I have the 'rotation' command specified?

I would then like to shift the labels in the radial direction. Is there an equivalent of the 'pad' command with the polar charts?

import numpy as np
import matplotlib.pyplot as plt
import math

Graph_title = "Radar Plot"

def radarplot():
    ax = plt.subplot(111, polar=True)

    # INPUT DATA
    n_directions = 12
    angles = [n / float(n_directions) * 2 * math.pi for n in range(n_directions)]
    data = [3.0, 3.0, 3.0, 3.0, 2.0, 2.5, 2.5, 2.5, 2.75, 2.75, 3.0, 3.0]

    # Add the last element of the list to the list. This is necessary or the line from 330 deg to 0 degree does not join up on the plot.
    angles = np.append(angles, angles[:1])
    data = np.append(data, data[:1])

    ax.plot(angles, data, linewidth=2, linestyle='solid', color = 'red')

    # Radial tick parameters
    radial_ticks = [0.00, 0.50, 1.00, 1.50, 2.00, 2.50, 3.00]
    ax.set_rlabel_position(45)
    ax.set_rorigin(0)
    plt.yticks(radial_ticks, color='black', size=8)
    ax.set_yticklabels(radial_ticks, rotation = 45, zorder = 500)

    # X Tick parameters
    plt.xticks(angles, color='black', size=10, zorder = 5)
    ax.tick_params(axis='x', which='major', pad=3)
    ax.set_theta_zero_location("N")         # Sets the labels initial position to 0 degrees
    ax.set_theta_direction("clockwise")     # Set the labels to rotate clockwise


    plt.savefig(Graph_title +".png", figsize = [6.4, 5], dpi=1000)
    plt.show()
    plt.close()

radarplot()

enter image description here

2 Answers

Recently I wanted to achieve the same thing as you and here is the solution that I came up with.

  1. Suppress the automatic r tick labels using the command ax.set_yticklabels([])
  2. For each radial tick define a tick list, a position list, and an alignment list.
  3. Using the text command write the values in the tick list at the radial locations specified by the position list with alignment specified by the alignment list.

Essentially the r ticks can be moved along the radial direction by changing the values in the position list.

Make sure that the text command is specified with transform=ax.transData option.

import numpy as np
import matplotlib.pyplot as plt
import math

Graph_title = "Radar Plot"

def radarplot():
    ax = plt.subplot(111, polar=True)

    # INPUT DATA
    n_directions = 12
    angles = [n / float(n_directions) * 2 * math.pi for n in range(n_directions)]
    data = [3.0, 3.0, 3.0, 3.0, 2.0, 2.5, 2.5, 2.5, 2.75, 2.75, 3.0, 3.0]

    # Add the last element of the list to the list. This is necessary or the line from 330 deg to 0 degree does not join up on the plot.
    angles = np.append(angles, angles[:1])
    data = np.append(data, data[:1])

    ax.plot(angles, data, linewidth=2, linestyle='solid', color = 'red')
    
    r_ticks = [0.00, 0.50, 1.00, 1.50, 2.00, 2.50, 3.00] #tick list
    r_ticks_pos = [0.20, 0.65, 1.15, 1.65, 2.15, 2.65, 3.25] #radial position list (CHANGE THESE VALUES TO MOVE EACH TICK RADIALLY INDIVIDUALLY)
    r_ticks_h_align = ['center','center','center','center','center','center','center'] #horizontal alignment list
    r_ticks_v_align = ['center','center','center','center','center','center','center'] #vertical alignment list
    r_label_angle = 45 #theta angle

    # Radial tick parameters
    ax.set_rlabel_position(r_label_angle)
    ax.set_rorigin(0)
        
    ax.set_yticklabels([])    
    

    ax.set_rticks(r_ticks)
    ax.set_rlabel_position(r_label_angle)

    #write the ticks using the text command
    for rtick, rtick_pos, rtick_ha, rtick_va in zip(r_ticks, r_ticks_pos, r_ticks_h_align, r_ticks_v_align):
        plt.text(np.radians(r_label_angle), rtick_pos, r'$'+str(rtick)+'$', ha=rtick_ha, va=rtick_va, transform=ax.transData, rotation=-45, fontsize=8)

    # X Tick parameters
    plt.xticks(angles, color='black', size=10, zorder = 5)
    ax.tick_params(axis='x', which='major', pad=3)
    ax.set_theta_zero_location("N")         # Sets the labels initial position to 0 degrees
    ax.set_theta_direction("clockwise")     # Set the labels to rotate clockwise


    plt.savefig(Graph_title +".png", figsize = [6.4, 5], dpi=1000)
    plt.show()
    plt.close()

radarplot()

And the result:

enter image description here

I meet the same problem, and I only solved the rotation problem. axis='x' failed to control the rotation, only axis='both' can work.

ax.tick_params(
        axis='both',
        labelrotation=-45.,
        )

I am trying to solve the pad problem.

Related