草庐IT

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

coder 2023-08-23 原文

我正在尝试制作一些具有出版质量的图,但我遇到了一个小问题。默认情况下,matplotlib 轴标签和图例条目的权重似乎比轴刻度线重。无论如何强制轴标签/图例条目与刻度线具有相同的权重?

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('x-Axis')
plt.ylabel('y-Axis')
plt.legend([p1,p2],['Sin(x)','x$^2$'])
plt.gcf().subplots_adjust(left=0.2)
plt.gcf().subplots_adjust(bottom=0.15)
plt.savefig('Test.eps',bbox_inches='tight',format='eps')
plt.show()

我可以使用数学模式,但问题(烦恼)是当我有一个标签的句子时,即

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

它把它压在一起。我可以通过使用修复它

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

但这需要很多标点符号。我希望有一些我可以改变的东西,让我绕过 \mathrm{} 格式,并使用标准的 TeX 格式。

我尝试过的另一个选项是使用 \text 而不是 \mathrm,但似乎 Python 的解释器在不加载包 amsmath 的情况下无法识别这一点。我也试过:

import matplotlib
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})
matplotlib.rcParams['text.latex.preamble']=[r'\usepackage{amsmath}']

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(r'$\text{this is the x-Axis}$')
plt.ylabel('$y-Axis$')
plt.legend([p1,p2],['Sin(x)','x$^2$'])
plt.gcf().subplots_adjust(left=0.2)
plt.gcf().subplots_adjust(bottom=0.15)
plt.savefig('Test.eps',bbox_inches='tight',format='eps')
plt.show()

这也不会返回所需的结果。

最佳答案

另一个答案提供了解决该问题的方法...但是,该问题非常特定于 matplotlib 和 LateX 后端的实现。

首先,rc控制轴标签字体粗细的参数是 'axes.labelweight' ,默认设置为 u'normal' .这意味着标签应该已经处于正常重量。

字体显示为粗体的原因可以在 matplotlib/texmanager.py 中找到。 :

  1. 字体系列由 'font.family' 选择,对于 OP,这是 serif .

  2. 然后,数组'font.<font.family>' (此处:font.serif)被评估,所有字体的声明被添加到 LateX 序言中。在这些声明中有一行

    \renewcommand{\rmdefault}{pnc}
    

    默认设置为新世纪教科书 which appears to be a bold version of Computer Modern Roman .

综上所述,解决问题最短的方法是设置font.serif仅包含 Computer Modern Roman:

font = {'family':'serif','size':16, 'serif': ['computer modern roman']}

这还有一个额外的好处,它适用于所有 元素,甚至适用于标签和其他标题 - 无需使用数学模式的肮脏技巧:


这里是生成绘图的完整代码:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

plt.rc('text',usetex=True)
#font = {'family':'serif','size':16}
font = {'family':'serif','size':16, 'serif': ['computer modern roman']}
plt.rc('font',**font)
plt.rc('legend',**{'fontsize':14})
matplotlib.rcParams['text.latex.preamble']=[r'\usepackage{amsmath}']

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(r'$\text{this is the x-Axis}$')
plt.ylabel('$y-Axis$')
plt.legend([p1,p2],['Sin(x)','x$^2$'])
plt.gcf().subplots_adjust(left=0.2)
plt.gcf().subplots_adjust(bottom=0.15)
plt.savefig('Test.eps',bbox_inches='tight',format='eps')
plt.show()

关于Python matplotlib : Change axis labels/legend from bold to regular weight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16287921/

有关Python matplotlib : Change axis labels/legend from bold to regular weight的更多相关文章

随机推荐