11

我想将轴的弧度单位写成与以下成比例\pi:类似于

$\frac{\pi}{4}$, $\frac{\pi}{2}$, ... 

代替

0.785, 1.5707 ... 

有什么标准方法吗?例如,我应该在以下代码中添加什么?

from pylab import *

x=arange(-10.0,10.0,0.1)
y= arctan(x)

plot(x,y,'b.')
show()

我找到了这个例子http://matplotlib.sourceforge.net/examples/units/radian_demo.html但它不起作用,因为我没有 basic_units 模块。

谢谢!

4

4 回答 4

13

将它们硬编码为分数或接受浮点数

import matplotlib.pyplot as plt
import numpy as np

x=np.arange(-10.0,10.0,0.1)
y=np.arctan(x)

fig = plt.figure()
ax  = fig.add_subplot(111)

ax.plot(x,y,'b.')

y_pi   = y/np.pi
unit   = 0.25
y_tick = np.arange(-0.5, 0.5+unit, unit)

y_label = [r"$-\frac{\pi}{2}$", r"$-\frac{\pi}{4}$", r"$0$", r"$+\frac{\pi}{4}$",   r"$+\frac{\pi}{2}$"]
ax.set_yticks(y_tick*np.pi)
ax.set_yticklabels(y_label, fontsize=20)

y_label2 = [r"$" + format(r, ".2g")+ r"\pi$" for r in y_tick]
ax2 = ax.twinx()
ax2.set_yticks(y_tick*np.pi)
ax2.set_yticklabels(y_label2, fontsize=20)

plt.show()

结果是

在此处输入图像描述

于 2012-05-24T05:29:13.247 回答
3

i wrote a little function, that gives you back a list of labels:

import numpy as np
from fractions import Fraction

def create_pi_labels(a, b, step):

    max_denominator = int(1/step)
    # i added this line and the .limit_denominator to solve an 
    # issue with floating point precision
    # because of floataing point precision Fraction(1/3) would be 
    # Fraction(6004799503160661, 18014398509481984)

    values = np.arange(a, b+step/10, step)
    fracs = [Fraction(x).limit_denominator(max_denominator) for x in values]
    ticks = values*np.pi

    labels = []

    for frac in fracs:
        if frac.numerator==0:
            labels.append(r"$0$")
        elif frac.numerator<0:
            if frac.denominator==1 and abs(frac.numerator)==1:
                labels.append(r"$-\pi$")
            elif frac.denominator==1:
                labels.append(r"$-{}\pi$".format(abs(frac.numerator)))
            else:
                labels.append(r"$-\frac{{{}}}{{{}}} \pi$".format(abs(frac.numerator), frac.denominator))
        else:
            if frac.denominator==1 and frac.numerator==1:
                labels.append(r"$\pi$")
            elif frac.denominator==1:
                labels.append(r"${}\pi$".format(frac.numerator))
            else:
                labels.append(r"$\frac{{{}}}{{{}}} \pi$".format(frac.numerator, frac.denominator))

    return ticks, labels

https://github.com/MaxNoe/python-plotting/blob/master/source/create_pi_labels.py

于 2014-07-14T22:07:12.963 回答
3

您可以在此处下载 basic_units.py 文件:

之后它应该像这样工作:

from pylab import *
from basic_units import radians

x = arange(-10.0,10.0,0.1)
y = map(lambda y: y*radians,arctan(x))
x = map(lambda x: x*radians,x)

plot(x,y,'b.',xunits=radians,yunits=radians)
show()

或者,您可以按照他们在 basic_units.py 中实现 cos 函数的方式来实现 arctan 函数

于 2017-05-16T12:58:16.803 回答
0

我创建了一个PyPi 包,它可以在 pi 的倍数处格式化和放置刻度。

"""Show an example of using MultiplePi on the y-axis."""
import matplotlib.pyplot as plt
import numpy as np

from matplot_fmt_pi import MultiplePi

x = np.arange(-10.0, 10.0, 0.1)
y = np.arctan(x)

fig = plt.figure()
ax = fig.add_subplot(111)

ax.set_title("MultiplePi formatting")
ax.plot(x, y, 'b.')

y_pi = y / np.pi
unit = 0.25
y_tick = np.arange(-0.5, 0.5 + unit, unit)

# New way
manager = MultiplePi(4)
ax.yaxis.set_major_locator(manager.locator())
ax.yaxis.set_major_formatter(manager.formatter())

# Other way
y_label2 = [r"$" + format(r, ".2g") + r"\pi$" for r in y_tick]
ax2 = ax.twinx()
ax2.set_yticks(y_tick * np.pi)
ax2.set_yticklabels(y_label2)

plt.savefig("./pi_y_axis.png", dpi=120)
于 2020-06-01T21:31:54.740 回答