Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib极坐标打印:以度为单位显示极坐标记号,十进制格式_Python_Matplotlib_Plot_Axis Labels_Polar Coordinates - Fatal编程技术网

Python 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

我试图在Matplotlib中的极扇形图上,按照指定的格式(即小数点后两位的浮点)以度标记记号,但这两种标记都不受明确支持

我可以将记号标记为度数或带有指定小数点的,但不能同时标记两者。请注意,Matplotlib默认为以度为单位的记号:

但是,在我使用
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)) # <---