【问题标题】:python - matplotlib - polar plots with angular labels in radianspython - matplotlib - 带有弧度角标签的极坐标图
【发布时间】:2014-01-16 20:31:12
【问题描述】:

我遇到的问题看起来很简单:每当我绘制极坐标图时,角度刻度都由 ThetaFormatter 处理,它以度为单位标记它们。

我知道这个question,其中刻度标签被替换为风玫瑰名称,以及这个example,它看起来像matplotlib 在笛卡尔设置中做了我想要的,但我还没有找到如何在极地情节中做同样的事情......这将是最自然的!

这是一个极坐标图的简单示例:

from pylab import *
fig = figure()
axe = fig.gca(polar=True)
thetas = linspace(0,2*pi,200)
rhos = 3+cos(5*thetas)
axe.plot(thetas, rhos)
fig.show()

【问题讨论】:

    标签: python numpy matplotlib polar-coordinates


    【解决方案1】:

    应该这样做:

    >>> fig=plt.figure()
    >>> axe=fig.gca(polar=True)
    >>> thetas=linspace(0,2*pi,200)
    >>> rhos=3+cos(5*thetas)
    >>> axe.plot(thetas, rhos)
    [<matplotlib.lines.Line2D object at 0x109a2b550>]
    >>> xT=plt.xticks()[0]
    >>> xL=['0',r'$\frac{\pi}{4}$',r'$\frac{\pi}{2}$',r'$\frac{3\pi}{4}$',\
        r'$\pi$',r'$\frac{5\pi}{4}$',r'$\frac{3\pi}{2}$',r'$\frac{7\pi}{4}$']
    >>> plt.xticks(xT, xL)
    ([<matplotlib.axis.XTick object at 0x107bac490>, <matplotlib.axis.XTick object at 0x109a31310>, <matplotlib.axis.XTick object at 0x109a313d0>, <matplotlib.axis.XTick object at 0x109a31050>, <matplotlib.axis.XTick object at 0x1097a8690>, <matplotlib.axis.XTick object at 0x1097a8cd0>, <matplotlib.axis.XTick object at 0x1097a8150>, <matplotlib.axis.XTick object at 0x107bb8fd0>], <a list of 8 Text xticklabel objects>)
    >>> plt.show()
    

    【讨论】:

    • 谢谢,但在我的问题文本中,我指出了一个示例,其中 matplotlib 被指示移动到弧度 - 但在笛卡尔设置中。我还指出了另一个问题,即刻度被风玫瑰方向所取代——但一切都是手工完成的,这很丑陋。我正在寻找一种混合两者的方法:移动到弧度,但不自己给出标签(我可以以风玫瑰为例轻松完成)。
    【解决方案2】:

    使用 python 3.8.3 和 matplotlib 3.2.2,这可以通过 get_xticks(返回十进制弧度值)和 set_xticklabels 函数巧妙地实现。

    如果您更喜欢不同的标签样式(即十进制弧度或不同的精度),函数 format_radians_label 可以替换为任何接受浮点数(以弧度为单位)并返回相应字符串的函数。

    注意为了避免使用TeX 格式化程序,我直接使用了UTF8 字符π 而不是\pi

    import matplotlib.pyplot as plt
    import numpy as np
    
    def format_radians_label(float_in):
        # Converts a float value in radians into a
        # string representation of that float
        string_out = str(float_in / (np.pi))+"π"
        
        return string_out
    
    def convert_polar_xticks_to_radians(ax):
        # Converts x-tick labels from degrees to radians
        
        # Get the x-tick positions (returns in radians)
        label_positions = ax.get_xticks()
        
        # Convert to a list since we want to change the type of the elements
        labels = list(label_positions)
        
        # Format each label (edit this function however you'd like)
        labels = [format_radians_label(label) for label in labels]
        
        ax.set_xticklabels(labels)
    
    fig = plt.figure()
    axe = fig.gca(polar=True)
    thetas = np.linspace(0,2*np.pi,200)
    rhos = 3+np.cos(5*thetas)
    axe.plot(thetas, rhos)
    
    convert_polar_xticks_to_radians(axe)
    
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 2019-08-25
      • 2014-03-13
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      相关资源
      最近更新 更多