【发布时间】:2019-07-07 22:12:29
【问题描述】:
我正在尝试按照 指定格式(即,带两位小数的浮点数)在 Matplotlib 中以 度 标记极坐标扇区图上的刻度,但是这样做这两个都没有明确支持。
我可以将刻度标记为具有指定小数位的度数或,但不能同时使用两者。请注意,Matplotlib 默认以度为单位标记:
但在我使用ax.xaxis.set_major_formatter() 将格式应用于刻度标记后,会显示弧度:
如何在指定十进制格式的同时强制执行度数格式?
注意:将刻度转换为度数(例如,numpy.rad2deg)不起作用,因为 ax.set_xticks() 仅将参数解释为弧度(然而,Matplotlib 默认将它们显示为度数...)
示例代码:
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))
【问题讨论】:
标签: python matplotlib plot axis-labels polar-coordinates