JPuydt
JPuydt

Reputation: 403

python - matplotlib - polar plots with angular labels in radians

The problem I have looks pretty simple : whenever I do a polar plot, the angle ticks are taken care of by ThetaFormatter, which labels them in degrees.

I am aware of this question, where the ticks labels are replaced with wind rose names, and of this example where it looks like matplotlib does what I want in a cartesian setting, but I haven't found how to do the same in a polar plot... where it would be the most natural!

Here is some simple example of a polar plot:

from pylab import *
fig = figure()
axe = fig.gca(polar=True)
thetas = linspace(0,2*pi,200)
rhos = 3+cos(5*thetas)
axe.plot(thetas, rhos)
fig.show()

Upvotes: 4

Views: 12176

Answers (2)

T.Body
T.Body

Reputation: 51

With python 3.8.3 and matplotlib 3.2.2, this can be achieved neatly via the get_xticks (which returns tick values in decimal-radians) and set_xticklabels function.

If you'd prefer a different label style (i.e. decimal radians or different precision), the function format_radians_label can be replaced by any function which takes a float (in radians) and returns a corresponding string.

N.b. to avoid the use of the TeX formatter I've directly used the UTF8 character π rather than \pi

import matplotlib.pyplot as plt
import numpy as np

def format_radians_label(float_in):
    # Converts a float value in radians into a
    # string representation of that float
    string_out = str(float_in / (np.pi))+"π"
    
    return string_out

def convert_polar_xticks_to_radians(ax):
    # Converts x-tick labels from degrees to radians
    
    # Get the x-tick positions (returns in radians)
    label_positions = ax.get_xticks()
    
    # Convert to a list since we want to change the type of the elements
    labels = list(label_positions)
    
    # Format each label (edit this function however you'd like)
    labels = [format_radians_label(label) for label in labels]
    
    ax.set_xticklabels(labels)

fig = plt.figure()
axe = fig.gca(polar=True)
thetas = np.linspace(0,2*np.pi,200)
rhos = 3+np.cos(5*thetas)
axe.plot(thetas, rhos)

convert_polar_xticks_to_radians(axe)

fig.show()

Polar plot with radians for xticks

Upvotes: 1

CT Zhu
CT Zhu

Reputation: 54330

This should do it:

>>> fig=plt.figure()
>>> axe=fig.gca(polar=True)
>>> thetas=linspace(0,2*pi,200)
>>> rhos=3+cos(5*thetas)
>>> axe.plot(thetas, rhos)
[<matplotlib.lines.Line2D object at 0x109a2b550>]
>>> xT=plt.xticks()[0]
>>> xL=['0',r'$\frac{\pi}{4}$',r'$\frac{\pi}{2}$',r'$\frac{3\pi}{4}$',\
    r'$\pi$',r'$\frac{5\pi}{4}$',r'$\frac{3\pi}{2}$',r'$\frac{7\pi}{4}$']
>>> plt.xticks(xT, xL)
([<matplotlib.axis.XTick object at 0x107bac490>, <matplotlib.axis.XTick object at 0x109a31310>, <matplotlib.axis.XTick object at 0x109a313d0>, <matplotlib.axis.XTick object at 0x109a31050>, <matplotlib.axis.XTick object at 0x1097a8690>, <matplotlib.axis.XTick object at 0x1097a8cd0>, <matplotlib.axis.XTick object at 0x1097a8150>, <matplotlib.axis.XTick object at 0x107bb8fd0>], <a list of 8 Text xticklabel objects>)
>>> plt.show()

enter image description here

Upvotes: 4

Related Questions