4

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

我知道这个问题,其中刻度标签被替换为风玫瑰名称,在这个例子中,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()
4

2 回答 2

4

这应该这样做:

>>> 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()

在此处输入图像描述

于 2014-01-16T22:28:14.500 回答
0

使用 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()

xticks 带有弧度的极坐标图

于 2020-08-31T09:03:36.300 回答