Python matplotlib:将轴标签/图例从粗体更改为常规重量 [英] Python matplotlib: Change axis labels/legend from bold to regular weight

查看:664
本文介绍了Python matplotlib:将轴标签/图例从粗体更改为常规重量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 heavier than the axis tick marks. Is there anyway to force the axis labels/legend entries to be the same weight as the tick marks?

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

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

I can use math-mode, but the problem (annoyance) is when I have a sentence for a label, i.e.,

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

将所有内容压缩在一起.我可以通过使用

which squishes it all together. I can fix it by using

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

但这需要很多标点符号.我希望可以进行一些更改,从而绕过\mathrm{}格式,并使用标准TeX格式.

but that needs a lot of punctuation. I was hoping there was something that I could change that would allow me to bypass the \mathrm{} format, and use standard TeX format.

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

The other option I tried, was using \text instead of \mathrm, but it seems that Python's interpreter doesn't recognize this without loading the package amsmath. I have also tried:

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

这也不会返回期望的结果.

This doesn't return the desired result either.

推荐答案

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

The other answer provides a work-around to the problem... However, the problem is very specific to matplotlib and the implementation of the LateX backend.

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

First of all, the rc parameter controlling the font weight of the axis labels is 'axes.labelweight', which by default is set to u'normal'. This means that the labels should already be in regular weight.

字体显示为粗体的原因可以在

The reason why the font appears to be bold can be found in matplotlib/texmanager.py:

  1. 'font.family'选择字体系列,对于OP,则为serif.

  1. The font family is chosen by 'font.family', in the case of the OP, this is serif.

然后,对数组'font.<font.family>'(在这里为font.serif)进行求值,并将所有字体的声明添加到LateX序言中.在这些声明中,一行

Then, the array 'font.<font.family>' (here: font.serif) is evaluated, and the declaration of all fonts is added to the LateX preamble. Among those declarations is the line

\renewcommand{\rmdefault}{pnc}

将默认设置为New Century School Book 是《 Computer Modern Roman》的加粗版本.

which sets the default to New Century School Book which appears to be a bold version of Computer Modern Roman.

总而言之,解决该问题的最短方法是将font.serif设置为仅包含Computer Modern Roman:

In conclusion, the shortest way to solve the problem is to set font.serif to contain only Computer Modern Roman:

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

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

This has the additional benefit that it works for all elements, even for labels and other captions - without dirty tricks using the math mode:

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

Here is the complete code to generate the plot:

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:将轴标签/图例从粗体更改为常规重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆