【问题标题】:Matplotlib polar plotting: displaying polar tickmarks in degrees, with decimal formatMatplotlib 极坐标绘图:以度数显示极坐标刻度,以十进制格式显示
【发布时间】: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


    【解决方案1】:

    极坐标图的内部单位是弧度。因此,刻度的位置以弧度给出,这些是您需要格式化的数字。您可以使用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()
    

    【讨论】:

    • 这正是我想要做的。谢谢你的解释和例子!既然你精通 MPL——你知道为什么四个所需的刻度中只有三个显示在示例楔形上吗?
    • 缺失的勾号是一个舍入问题。您可以改用minTheta = 0.42001minTheta = 0.41999
    【解决方案2】:

    或者,您可以使用PercentFormatter。这里xmax 是对应于 100% 的值。根据您转换为百分比的方式,100% 对应的弧度值为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)) # <---
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-25
      • 2017-09-29
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 2013-06-14
      相关资源
      最近更新 更多