简体   繁体   中英

Matplotlib polar plotting: displaying polar tickmarks in degrees, with decimal format

I am attempting to label the tickmarks on a polar sector plot in Matplotlib in degrees following a specified format (ie, a float with two decimal places), but doing both of these is not clearly supported.

I am able to label the tickmarks as degrees or with specified decimal places, but not both. Note that Matplotlib defaults to tickmarks in degrees:

But after I apply a format to the tickmarks using ax.xaxis.set_major_formatter() , radians are displayed instead: 在此处输入图片说明

How can I enforce the degree format while also specifying the decimal format?

Note: converting the tickmarks to degrees (eg, numpy.rad2deg ) does not work, since ax.set_xticks() interprets the argument as radians only (and yet, Matplotlib displays them as degrees by default...)

Sample code:

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.ticker import FormatStrFormatter

minTheta = 0.42; maxTheta = 0.55

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')

#create four tick labels (in radians) dynamically based on the theta range
ticks = np.linspace(minTheta, maxTheta, 4)

ax.set_xticks(ticks)

#disable or enable the following line to change the tick display format*************
ax.xaxis.set_major_formatter(FormatStrFormatter('%.2f'))

#Adjust the sector window: you must call these AFTER setting the ticks, since setting the ticks
#actually adjusts the theta range window. This must be in degrees.
ax.set_thetamin(np.rad2deg(minTheta))
ax.set_thetamax(np.rad2deg(maxTheta))

The internal units of the polar plot are radians. The position of the ticks is hence given in radians and those are the numbers you need to format. You can do so using a FuncFormatter .

rad2fmt = lambda x,pos : f"{np.rad2deg(x):.2f}°"
ax.xaxis.set_major_formatter(FuncFormatter(rad2fmt))

Complete example would look like:

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.ticker import FuncFormatter

minTheta = 0.42; maxTheta = 0.55

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')

#create four tick labels (in radians) dynamically based on the theta range
ticks = np.linspace(minTheta, maxTheta, 4)
ax.set_xticks(ticks)

#disable or enable the following line to change the tick display format*
rad2fmt = lambda x,pos : f"{np.rad2deg(x):.2f}°"
ax.xaxis.set_major_formatter(FuncFormatter(rad2fmt))

#Adjust the sector window: you must call these AFTER setting the ticks, since setting the ticks
#actually adjusts the theta range window. And it must be in degrees.
ax.set_thetamin(np.rad2deg(minTheta))
ax.set_thetamax(np.rad2deg(maxTheta))

plt.show()

在此处输入图片说明

Alternatively, you can make use of PercentFormatter . Here xmax is the value which corresponds to 100%. As per your conversion to percentages, 100% would correspond to a radian value of np.pi*100/180 .

I am highlighting the three added lines of code by a comment #

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import PercentFormatter # <---


minTheta = 0.42; maxTheta = 0.55

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')

ticks = np.linspace(minTheta, maxTheta, 4)
ax.set_xticks(ticks)

ax.set_thetamin(np.rad2deg(minTheta))
ax.set_thetamax(np.rad2deg(maxTheta))

xmax = np.pi*100/180 # <---
ax.xaxis.set_major_formatter(PercentFormatter(xmax, decimals=2)) # <---

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM