简体   繁体   English

Matplotlib在text.usetex == True时不使用乳胶字体

[英]Matplotlib not using latex font while text.usetex==True

I want to create labels to my plots with the latex computer modern font. 我想用乳胶电脑现代字体为我的情节创建标签。 However, the only way to persuade matplotlib to use the latex font is by inserting something like: 但是,说服matplotlib使用乳胶字体的唯一方法是插入如下内容:

title(r'$\mathrm{test}$')

This is of course ridiculous, I tell latex to start math mode, and then exit math mode temporary to write the actual string. 这当然是荒谬的,我告诉latex要启动数学模式,然后暂时退出数学模式以写入实际的字符串。 How do I make sure that all labels are rendered in latex, instead of just the formulas? 如何确保所有标签都以乳胶呈现,而不仅仅是公式? And how do I make sure that this will be the default behaviour? 我如何确保这将是默认行为?

A minimal working example is as follows: 最小的工作示例如下:

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

# use latex for font rendering
mpl.rcParams['text.usetex'] = True


x = np.linspace(-50,50,100)
y = np.sin(x)**2/x
plt.plot(x,y)

plt.xlabel(r'$\mathrm{xlabel\;with\;\LaTeX\;font}$')
plt.ylabel(r'Not a latex font')
plt.show()

This gives the following result: 这给出了以下结果:

绘图显示乳胶字体类型的错误呈现

Here the x axis is how I want the labels to appear. 这里x轴是我想要标签出现的方式。 How do I make sure that all labels appear like this without having to go to math mode and back again? 如何确保所有标签都显示为这样,而无需进入数学模式并再次返回?

The default Latex font is known as Computer Modern : 默认的Latex字体称为Computer Modern

from matplotlib import rc
import matplotlib.pylab as plt

rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
rc('text', usetex=True)

x = plt.linspace(0,5)
plt.plot(x,plt.sin(x))
plt.ylabel(r"This is $\sin(x)$", size=20)
plt.show()

在此输入图像描述

The marked answer can be enabled by default by changing a few lines in the matplotlibrc file: 默认情况下,可以通过更改matplotlibrc文件中的几行来启用标记的答案:

text.usetex = True
font.family = serif 
font.serif = cm

I am using matplotlib 1.3.1 on Mac OSX, add the following lines in matplotlibrc works for me 我在Mac OSX上使用matplotlib 1.3.1,在matplotlibrc添加以下行为我工作

text.usetex : True
font.family : serif 
font.serif  : cm

Using = leads to a UserWarning: Illegal line 使用=导致UserWarning: Illegal line

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 text.usetex 时,无法在 Windows 中将 matplotlib 图保存到 .eps :True - Can´t save matplotlib figure to .eps in Windows while using text.usetex : True 设置“matplotlib.rcParams['text.usetex'] = True”以在标签中使用LaTeX并使用德语语言环境使用逗号时的Python图形问题 - Python figure problem when setting "matplotlib.rcParams['text.usetex'] = True" to use LaTeX in labels AND using the German locale to use the comma text.usetex的好处是什么:在matplotlib中为True - What's the benefit of text.usetex : True in matplotlib 带有text.usetex'true'的Axes字体不使用set font - Axes fonts with text.usetex 'true' does not use set font 使用'text.usetex'= true时,pdf中嵌入的字体错误 - wrong fonts embedded in pdf when using 'text.usetex' = true matplotlib rc('text',usetex = True)产生错误 - matplotlib rc('text', usetex=True) producing errors 使用MatPlotlib和LaTex文本渲染(usetex)创建.eps输出时出错 - Error creating .eps output with matplotlib with LaTex text rendering (usetex) matplotlib无法使用usetex = True输出EPS数字 - matplotlib fails to output EPS figure with usetex = True 使用matplotlib导出pdf时找不到字体使用usetex = True的Agg后端 - Can not find fonts when export pdf using matplotlib Agg backend with usetex=True libpng 错误:在设置 usetex=True 后使用 matplotlib 时读取错误 - libpng error: Read Error when using matplotlib after setting usetex=True
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM