[英]Mark ticks in latex in matplotlib
在 matplotlib 的繪圖中,我特別想將 x 軸上的點標記為乳膠中的 pi/2、pi、3pi/2 等。 我該怎么做?
plt.xticks
命令可用於放置 LaTeX 刻度線。 有關更多詳細信息,請參閱此文檔頁面。
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()
另一種可能性是更新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.