在matplotlib的情节中,我特别想在x轴上将点标记为pi / 2,pi,3pi / 2等乳胶。我该怎么办?
答案 0 :(得分:20)
plt.xticks
命令可用于放置LaTeX刻度线。有关详细信息,请参阅此doc page。
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
cos = np.cos
pi = np.pi
# This is not necessary if `text.usetex : True` is already set in `matplotlibrc`.
mpl.rc('text', usetex = True)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
t = np.linspace(0.0, 2*pi, 100)
s = cos(t)
plt.plot(t, s)
plt.xticks([0, pi/2, pi, 3*pi/2, 2*pi],
['$0$', r'$\frac{\pi}{2}$', r'$\pi$', r'$\frac{3\pi}{2}$', r'$2\pi$'])
plt.show()
答案 1 :(得分:0)
另一种可能性是更新pyplot rcParams
,尽管这可能是hack而非合法方法。
import matplotlib.pyplot as plt
import numpy as np
cos = np.cos
pi = np.pi
params = {'mathtext.default': 'regular' } # Allows tex-style title & labels
plt.rcParams.update(params)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
t = np.linspace(0.0, 2*pi, 100)
s = cos(t)
plt.plot(t, s)
ax.set_xticks([0, pi/2, pi, 3*pi/2, 2*pi])
ax.set_xticklabels(['$0$', r'$\frac{\pi}{2}$', r'$\pi$', r'$\frac{3\pi}{2}$', r'$2\pi$'])
plt.show()