Matplotlib not using latex font while text.usetex==True(Matplotlib 在 text.usetex==True 时不使用乳胶字体)
问题描述
我想使用 Latex 计算机现代字体为我的绘图创建标签.然而,说服 matplotlib 使用 Latex 字体的唯一方法是插入如下内容:
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:
title(r'$mathrm{test}$')
这当然很荒谬,我告诉latex 启动数学模式,然后暂时退出数学模式以写入实际字符串.如何确保所有标签都以乳胶呈现,而不仅仅是公式?以及如何确保这将是默认行为?
This is of course ridiculous, I tell latex to start math mode, and then exit math mode temporary to write the actual string. 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()
这给出了以下结果:
这里的 x 轴是我希望标签出现的方式.如何确保所有标签都显示为这样,而无需进入数学模式并再次返回?
Here the x axis is how I want the labels to appear. How do I make sure that all labels appear like this without having to go to math mode and back again?
推荐答案
默认的Latex字体被称为Computer Modern:
The default Latex font is known as 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()
这篇关于Matplotlib 在 text.usetex==True 时不使用乳胶字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Matplotlib 在 text.usetex==True 时不使用乳胶字体
基础教程推荐
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
