Matplotlib在所有情节上打上Sans-Serif [英] Matplotlib ticks sans-serif for all plots

查看:145
本文介绍了Matplotlib在所有情节上打上Sans-Serif的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pgf在matplotlib中实现sans-serif轴标签和刻度.我想通过rcParams进行控制,因此我可以将其用于所有后续绘图.目前,我正在使用

I'm trying to achieve sans-serif axes labels and ticks in matplotlib with pgf. I want to control that by rcParams so I have it working for all subsequent plots. Currently, I'm using

"pgf.rcfonts":False,
"pgf.texsystem": "pdflatex",   
"text.usetex": True,                
"font.family": "sans-serif",

我从mpl文档改编的

.它确实将我在图中的所有文字都设置为sans-serif.但是,数字以衬线字体保留.我希望他们是无衬线的.一旦我强制使用格式化程序,即无衬线.有什么办法可以使其自动执行此操作吗? MinEx紧随其后...

which I adapted from the mpl docs. It does set all my text in the figure to sans-serif. However, the numbers are kept with a serif font. I want them to be sans-serif. As soon as i force the formatter, its sans-serif. Any way to get it to do that automatically? MinEx follows...

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter

#define style
style = {
        "pgf.rcfonts":False,
        "pgf.texsystem": "pdflatex",   
        "text.usetex": True,                
        "font.family": "sans-serif"
        }
#set
mpl.rcParams.update(style)

#get data
data = np.random.random(100)
#set up plot
f,ax = plt.subplots()
ax.plot(data)
#label something
ax.set_xlabel('Runner')

标签现在没有了.滴答声不是!但是打电话时

The label is sans now. The ticks are not! But when calling

ax.xaxis.set_major_formatter(FormatStrFormatter('%d'))

是的.

推荐答案

根据 pgf texsystem例如,则需要使用"pfg"后端(mpl.use("pgf"))并选择要使用的字体:

According to the pgf texsystem example, you need to use the "pfg" backend (mpl.use("pgf")) and choose the font you want to use:

style = {
        "pgf.texsystem": "pdflatex",   
        "text.usetex": True,                
        "pgf.preamble": [
         r"\usepackage[utf8x]{inputenc}",
         r"\usepackage[T1]{fontenc}",
         r"\usepackage{cmbright}",
         ]
        }

或者,您可以使用格式化程序,该格式化程序不会将刻度线标签格式化为乳胶数学(即不会将它们放入美元符号中).

Alternatively you may use a formatter, which does not format the ticklabels as latex math (i.e. does not put them into dollar signs).

可以通过设置默认值ScalarFormatter不使用乳胶数学

One may adapt the default ScalarFormatter not to use latex math by setting

ax.xaxis.get_major_formatter()._usetex = False
ax.yaxis.get_major_formatter()._usetex = False

这篇关于Matplotlib在所有情节上打上Sans-Serif的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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