在matplotlib中用乳胶标记记号

2024-04-16 04:56:04 发布

您现在位置:Python中文网/ 问答频道 /正文

在matplotlib中的绘图中,我特别想在x轴上标记点为pi/2、pi、3pi/2等。我该怎么做?


Tags: 标记绘图matplotlibpi
1条回答
网友
1楼 · 发布于 2024-04-16 04:56:04

可以使用plt.xticks命令放置乳胶记号。有关详细信息,请参见本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()

enter image description here

相关问题 更多 >