Python Matplotlib极坐标打印:以度为单位显示极坐标记号,十进制格式
我试图在Matplotlib中的极扇形图上,按照指定的格式(即小数点后两位的浮点)以度标记记号,但这两种标记都不受明确支持 我可以将记号标记为度数或带有指定小数点的,但不能同时标记两者。请注意,Matplotlib默认为以度为单位的记号: 但是,在我使用Python Matplotlib极坐标打印:以度为单位显示极坐标记号,十进制格式,python,matplotlib,plot,axis-labels,polar-coordinates,Python,Matplotlib,Plot,Axis Labels,Polar Coordinates,我试图在Matplotlib中的极扇形图上,按照指定的格式(即小数点后两位的浮点)以度标记记号,但这两种标记都不受明确支持 我可以将记号标记为度数或带有指定小数点的,但不能同时标记两者。请注意,Matplotlib默认为以度为单位的记号: 但是,在我使用ax.xaxis.set\u major\u formatter()对记号应用了一种格式后,弧度反而显示出来: 如何在指定十进制格式的同时强制执行学位格式 注意:将记号转换为度(例如,numpy.rad2deg)不起作用,因为ax.set_xti
ax.xaxis.set\u major\u 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))
极坐标图的内部单位为弧度。因此,记号的位置以弧度表示,这些是需要格式化的数字。您可以使用
函数格式化程序
执行此操作
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()
或者,您可以利用。这里
xmax
是对应于100%的值。根据您对百分比的转换,100%将对应于np.pi*100/180
的弧度值
我通过注释#
突出显示添加的三行代码
将numpy导入为np
将matplotlib.pyplot作为plt导入
从matplotlib.ticker import PercentFormatter#这正是我想要做的。感谢您的解释和示例!既然你擅长MPL——你知道为什么四个想要的记号中只有三个显示在样本楔块上吗?缺少记号是一个舍入问题。您可以使用minTheta=0.42001
或minTheta=0.41999
。
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)) # <---