Python matplotlib: Change axis labels/legend from bold to regular weight

前端 未结 2 1558
独厮守ぢ
独厮守ぢ 2021-02-06 07:47

I\'m trying to make some publication-quality plots, but I have encountered a small problem. It seems by default that matplotlib axis labels and legend entries are weighted heavi

2条回答
  •  萌比男神i
    2021-02-06 08:32

    How about:

    enter image description here

    import matplotlib.pyplot as plt
    import numpy as np
    
    plt.rc('text',usetex=True)
    font = {'family':'serif','size':16}
    plt.rc('font',**font)
    plt.rc('legend',**{'fontsize':14})
    
    x = np.linspace(0,2*np.pi,100)
    y = np.sin(x)
    
    fig = plt.figure(figsize=(5,5))
    p1, = plt.plot(x,y)
    p2, = plt.plot(x,x**2)
    plt.xlabel('$\mathrm{This is the }x\mathrm{-axis}$'.replace(' ','\: '))
    plt.ylabel('$y\mathrm{-axis}$')
    plt.legend([p1,p2],['$\sin(x)$','$x^2$'], loc='best')
    fig.subplots_adjust(left=0.2, bottom=0.15)
    plt.savefig('Test.eps',bbox_inches='tight',format='eps')
    plt.show()
    

    This uses

    plt.xlabel('$\mathrm{This is the }x\mathrm{-axis}$'.replace(' ','\: '))
    

    to replace spaces with '\: '. TeX gurus may object to this however. You may want to ask on TeX stackexchange if there is a better way.

提交回复
热议问题