Radial text annotation polar plot with clockwise direction

96 views Asked by At

I want to place some text on a radius of a polar plot. When using the default theta zero location and direction it works as expected

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, polar=True)
ax.set_yticklabels("")
ax.annotate('test',
            xy=(np.deg2rad(90), 0.5),
            fontsize=15,
            rotation=90)

enter image description here

However this fails when changing the direction

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, polar=True)
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
ax.set_yticklabels("")
ax.annotate('test',
            xy=(np.deg2rad(90), 0.5),
            fontsize=15,
            rotation=90)

enter image description here

It seems that the x and y are correct but the rotation angle is not. In theory, the transformation between clockwise and anti-clockwise should bring theta into -theta but that obviously does not work here. I have tried any possible transformation but it seems that something weird is happening..

What am I missing?

1

There are 1 answers

3
Tranbi On BEST ANSWER

The rotation angle will follow the opposite of the position angle, starting from 90 (at position 0 you have an angle of 90°). Furthermore you can take angle % 180 to avoid having text upside down:

import matplotlib.pyplot as plt
import numpy as np

angle = 135

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, polar=True)
ax.set_theta_zero_location("N") # apply +90° rotation
ax.set_theta_direction(-1)      # set - before angle
ax.set_yticklabels("")
ax.annotate('test',
            xy=(np.deg2rad(angle), 0.5),
            fontsize=15,
            rotation=90-(angle % 180))

plt.show()

Output for angle=135:

enter image description here