代码之家  ›  专栏  ›  技术社区  ›  ascendants

Matplotlib极坐标打印:以度为单位显示极坐标记号,采用十进制格式

  •  1
  • ascendants  · 技术社区  · 7 年前

    (例如,有两个小数位的浮点数),但这两种方法都不被明确支持。

    但在我使用 ax.xaxis.set_major_formatter() enter image description here

    如何在指定十进制格式的同时强制执行度格式?

    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))
    
    2 回复  |  直到 7 年前
        1
  •  4
  •   jtlz2    5 年前

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

    enter image description here

        2
  •  1
  •   Sheldore    7 年前

    或者,你可以利用 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)) # <---
    

    enter image description here

    推荐文章