Matplotlib,使用乳胶的一致字体 [英] Matplotlib, Consistent font using latex

查看:94
本文介绍了Matplotlib,使用乳胶的一致字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想在某些情节中使用乳胶标题,而在其他情节中不使用乳胶.现在,matplotlib为Latex标题和非Latex标题提供了两种不同的默认字体,我希望两者保持一致.是否有我必须更改的RC设置才能自动启用?

My problem is I'd like to use Latex titles in some plots, and no latex in others. Right now, matplotlib has two different default fonts for Latex titles and non-Latex titles and I'd like the two to be consistent. Is there an RC setting I have to change that will allow this automatically?

我使用以下代码生成图:

I generate a plot with the following code:

import numpy as np
from matplotlib import pyplot as plt

tmpData = np.random.random( 300 )

##Create a plot with a tex title
ax = plt.subplot(211)
plt.plot(np.arange(300), tmpData)
plt.title(r'$W_y(\tau, j=3)$')
plt.setp(ax.get_xticklabels(), visible = False)

##Create another plot without a tex title
plt.subplot(212)
plt.plot(np.arange(300), tmpData )
plt.title(r'Some random numbers')
plt.show()

这是我正在谈论的不一致之处.轴刻度标签相对于标题而言较薄.

Here is the inconsistency I am talking about. The axis tick labels are thin looking relative to the titles.:

推荐答案

要使tex样式/文本文本看起来像常规文本,您需要将mathtext字体设置为Bitstream Vera Sans,

To make the tex-style/mathtext text look like the regular text, you need to set the mathtext font to Bitstream Vera Sans,

import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'custom'
matplotlib.rcParams['mathtext.rm'] = 'Bitstream Vera Sans'
matplotlib.rcParams['mathtext.it'] = 'Bitstream Vera Sans:italic'
matplotlib.rcParams['mathtext.bf'] = 'Bitstream Vera Sans:bold'
matplotlib.pyplot.title(r'ABC123 vs $\mathrm{ABC123}^{123}$')

如果希望常规文本看起来像数学文本一样,则可以将所有内容更改为Stix.这会影响标签,标题,刻度等.

If you want the regular text to look like the mathtext text, you can change everything to Stix. This will affect labels, titles, ticks, etc.

import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'stix'
matplotlib.rcParams['font.family'] = 'STIXGeneral'
matplotlib.pyplot.title(r'ABC123 vs $\mathrm{ABC123}^{123}$')

基本思想是您需要将常规字体和Mathtext字体设置为相同,并且这样做的方法有点晦涩.您可以看到自定义字体的列表,

Basic idea is that you need to set both the regular and mathtext fonts to be the same, and the method of doing so is a bit obscure. You can see a list of the custom fonts,

sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])

正如其他人所提到的,您还可以通过在rcParams中设置text.usetex,使Latex用一种字体为您呈现所有内容,但这很慢,并非完全必要.

As others mentioned, you can also have Latex render everything for you with one font by setting text.usetex in the rcParams, but that's slow and not entirely necessary.

这篇关于Matplotlib,使用乳胶的一致字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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