Matplotlib:更改数学字体大小,然后回到默认值 [英] Matplotlib: Change math font size and then go back to default

查看:790
本文介绍了Matplotlib:更改数学字体大小,然后回到默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个问题中学到了 Matplotlib:改变数学字体大小如何在 matplotlib 中更改数学文本的默认大小。我做的是:

pre $ 从matplotlib导入rcParams
rcParams ['mathtext.default'] ='regular'

有效地使LaTeX字体与常规字体大小相同。



我不知道如何将它重置为默认行为,即:LaTeX字体看起来比常规字体小一些。 b
$ b

我需要这个,因为我希望LaTeX字体只在一个图上看起来和常规字体大小相同,而不是在我的图中所有使用LaTex数学格式的图上。



以下是我如何创建图形的 MWE

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

#生成随机数据。
x = np.random.randn(60)
y = np.random.randn(60)

fig = plt.figure(figsize =(5,10))#create顶级容器
gs = gridspec.GridSpec(6,4)#创建一个GridSpec对象

ax0 = plt.subplot(gs [0:2,0:4])
plt.scatter(x,y,s = 20,label ='aaa $ _ {subaaa} $')
句柄,labels = ax0.get_legend_handles_labels()
ax0.legend(句柄, ('A $ _y $',fontsize = 16)
plt.xlabel('A $ _x $' ,fontsize = 16)

ax1 = plt.subplot(gs [2:4,0:4])
#我只希望在这个图上有相同大小的LaTeX字体。
from matplotlib import rcParams
rcParams ['mathtext.default'] ='regular'

plt.scatter(x,y,s = 20,label ='bbb $ _ {subbbb} $')
句柄,标签= ax1.get_legend_handles_labels()
ax1.legend(句柄,标签,loc ='右上角',numpoints = 1,fontsize = 14)
plt.ylabel('B $ _y $',fontsize = 16)
plt.xlabel('B $ _x $',fontsize = 16)

#在整个绘图中将LaTeX字体设置为常规
#。
#rcParams ['mathtext.default'] ='it'
#plt.rcdefaults()

ax2 = plt.subplot(gs [4:6,0:4 ])
plt.scatter(x,y,s = 20,label ='ccc $ _ {subccc} $')
句柄,labels = ax2.get_legend_handles_labels()
ax2.legend (句柄,标签,loc ='右上',numpoints = 1,fontsize = 14)
plt.ylabel('C $ _y $',fontsize = 16)
plt.xlabel('C $ _x $',fontsize = 16)

fig.tight_layout()

out_png ='test_fig.png'
plt.savefig(out_png,dpi = 150)
plt.close()


解决方案

这是因为在绘制Axes对象时,不是在创建时才使用 mathtext.default 设置。为了解决这个问题,我们需要在Axes对象被绘制之前改变设置,这里是一个演示:

 #你的在这里绘制代码

def wrap_rcparams(f,params):
def _f(* args,** kw):
backup = {key:plt.rcParams [key] for键参数}
plt.rcParams.update(params)
f(* args,** kw)
plt.rcParams.update(备份)
返回_f

plt.rcParams ['mathtext.default'] ='it'
ax1.draw = wrap_rcparams(ax1.draw,{mathtext.default:'regular'})

#在这里保存数字

这里是输出:


I've learned from this question Matplotlib: Change math font size how to change the default size of math text in matplotlib. What I do is:

from matplotlib import rcParams
rcParams['mathtext.default'] = 'regular'

which effectively makes the LaTeX font the same size as the regular font.

What I don't know is how to reset this back to the default behaviour, ie: the LaTeX font looking a bit smaller than the regular font.

I need this because I want the LaTeX font to look the same size as the regular font only on one plot, not on all plots in my figure that use LaTex math formatting.

Here's a MWE of how I create my figure:

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

# Generate random data.
x = np.random.randn(60)
y = np.random.randn(60)

fig = plt.figure(figsize=(5, 10))  # create the top-level container
gs = gridspec.GridSpec(6, 4)  # create a GridSpec object

ax0 = plt.subplot(gs[0:2, 0:4])
plt.scatter(x, y, s=20, label='aaa$_{subaaa}$')
handles, labels = ax0.get_legend_handles_labels()
ax0.legend(handles, labels, loc='upper right', numpoints=1, fontsize=14)
plt.ylabel('A$_y$', fontsize=16)
plt.xlabel('A$_x$', fontsize=16)

ax1 = plt.subplot(gs[2:4, 0:4])
# I want equal sized LaTeX fonts only on this plot.
from matplotlib import rcParams
rcParams['mathtext.default'] = 'regular'

plt.scatter(x, y, s=20, label='bbb$_{subbbb}$')
handles, labels = ax1.get_legend_handles_labels()
ax1.legend(handles, labels, loc='upper right', numpoints=1, fontsize=14)
plt.ylabel('B$_y$', fontsize=16)
plt.xlabel('B$_x$', fontsize=16)

# If I set either option below the line that sets the LaTeX font as 'regular'
# is overruled in the entire plot.
#rcParams['mathtext.default'] = 'it'
#plt.rcdefaults()

ax2 = plt.subplot(gs[4:6, 0:4])
plt.scatter(x, y, s=20, label='ccc$_{subccc}$')
handles, labels = ax2.get_legend_handles_labels()
ax2.legend(handles, labels, loc='upper right', numpoints=1, fontsize=14)
plt.ylabel('C$_y$', fontsize=16)
plt.xlabel('C$_x$', fontsize=16)

fig.tight_layout()

out_png = 'test_fig.png'
plt.savefig(out_png, dpi=150)
plt.close()

解决方案

I think this is because the mathtext.default setting is used when the Axes object is drawn, not when it's created. To walk around the problem we need the change the setting just before the Axes object is drawn, here is a demo:

# your plot code here

def wrap_rcparams(f, params):
    def _f(*args, **kw):
        backup = {key:plt.rcParams[key] for key in params}
        plt.rcParams.update(params)
        f(*args, **kw)
        plt.rcParams.update(backup)
    return _f

plt.rcParams['mathtext.default'] = 'it'
ax1.draw = wrap_rcparams(ax1.draw, {"mathtext.default":'regular'})

# save the figure here

Here is the output:

这篇关于Matplotlib:更改数学字体大小,然后回到默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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