Mark ticks in latex in matplotlib

后端 未结 2 496
星月不相逢
星月不相逢 2021-02-01 09:33

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. How can I do it?

2条回答
  •  旧时难觅i
    2021-02-01 09:52

    Another possibility is to update the pyplot rcParams, although this might be rather a hack than a legitimate way.

    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()
    

    enter image description here

提交回复
热议问题