[英]Mark ticks in latex in matplotlib
In a plot in matplotlib I specially want to mark points on the x-axis as pi/2, pi, 3pi/2 and so on in latex.在 matplotlib 的绘图中,我特别想将 x 轴上的点标记为乳胶中的 pi/2、pi、3pi/2 等。 How can I do it?
我该怎么做?
The plt.xticks
command can be used to place LaTeX tick marks. plt.xticks
命令可用于放置 LaTeX 刻度线。 See this doc page for more details.有关更多详细信息,请参阅此文档页面。
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()
Another possibility is to update the pyplot rcParams
, although this might be rather a hack than a legitimate way.另一种可能性是更新
pyplot rcParams
,尽管这可能是一种破解而不是合法的方式。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.