极地图的内部单位是辐射。因此,在辐射中给出了刻度的位置,这些是你需要格式化的数字。你可以用一个 FuncFormatter 。
FuncFormatter
rad2fmt = lambda x,pos : f"{np.rad2deg(x):.2f}锟斤拷" ax.xaxis.set_major_formatter(FuncFormatter(rad2fmt))
完整的示例如下所示:
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()
或者,您可以使用 PercentFormatter 。这里 xmax 是对应于100%的值。根据您转换为百分比,100%将对应于弧度值 np.pi*100/180 。
PercentFormatter
xmax
np.pi*100/180
我通过评论突出显示了三行代码 #
#
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)) # <---