我想将轴的弧度单位写成与\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模块。
谢谢!
答案 0 :(得分:12)
在分数中硬编码或接受浮点数
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()
结果是
答案 1 :(得分:2)
我写了一个小函数,它给你一个标签列表:
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
答案 2 :(得分:2)
您可以下载basic_units.py文件here:
应该像这样工作:
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函数
答案 3 :(得分:0)
我创建了一个PyPi Package,可以设置刻度并将其放置在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)