Matplotlib:设置上标字体大小

ztl*_*ztl 5 python matplotlib

在Matplotlib中,如何设置上标的字体大小(除了控制基础的字体大小)?例如,使用带有科学记数法的轴的Matplotlib创建图形:可以很容易地设置刻度标签的字体大小,但是如何指定其指数的字体大小?我想差异地对基数和指数进行控制(即,在刻度标签的字体大小上播放以获得所需大小的指数不是一个好选择 - 我们可以修改字体大小的比例吗?基数和指数?).谢谢.

DrV*_*DrV 8

如果你有指数,你获得文本基本上有两种可能性:

  1. 通过使用外部TeX安装(在这种情况下你有rcParams['text.usetex'] == True).
  2. 通过使用mathtext内置的Tex克隆matplotlib

如果您正在使用外部TeX安装,那么它取决于TeX(我的猜测是这样的\DeclareMathSizes{10}{18}{12}{8},但我没有尝试过).

如果您使用"标准"方法,则字体大小比率将被硬编码matplotlib.所以,没有办法改变它们; 根据Donald Knuth最初的TeX规范,上标是基本字体的70%.


在说"没办法"之后,我会表明一种方式.但这不是一条美丽的道路......

由于matplotlib主要用Python编写,你可能会改变很多东西.您想要的参数在文件中.../matplotlib/mathtext.py.这...取决于您的Python发行版和操作系统.(例如,我的是/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/mathtext.py)

在该文件中应该有1200行左右:

# How much text shrinks when going to the next-smallest level.  GROW_FACTOR
# must be the inverse of SHRINK_FACTOR.
SHRINK_FACTOR   = 0.7
GROW_FACTOR     = 1.0 / SHRINK_FACTOR
# The number of different sizes of chars to use, beyond which they will not
# get any smaller
NUM_SIZE_LEVELS = 6
# Percentage of x-height of additional horiz. space after sub/superscripts
SCRIPT_SPACE    = 0.2
# Percentage of x-height that sub/superscripts drop below the baseline
SUBDROP         = 0.3
# Percentage of x-height that superscripts drop below the baseline
SUP1            = 0.5
# Percentage of x-height that subscripts drop below the baseline
SUB1            = 0.0
# Percentage of x-height that superscripts are offset relative to the subscript
DELTA           = 0.18
Run Code Online (Sandbox Code Playgroud)

您可以更改这些以使文本间距不同.例如,让我们制作一个简单的测试图:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,5, 1000)
y = np.sin(x**2)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
ax.set_xlabel(r'$x_1$')
ax.set_ylabel(r'$sin(x_1^2)$')
ax.text(.5, -.5, r'$\rm{this\ is}_\mathrm{subscript}$', fontsize=24)
ax.text(.5, -.7, r'$\rm{this\ is}^\mathrm{superscript}$', fontsize=24)
ax.text(.5, -.9, r'$\frac{2}{1+\frac{1}{3}}$', fontsize=24)
Run Code Online (Sandbox Code Playgroud)

这给出了:

在此输入图像描述

然后我们做了一些魔术:

import matplotlib

matplotlib.mathtext.SHRINK_FACTOR = 0.5
matplotlib.mathtext.GROW_FACTOR = 1 / 0.5
Run Code Online (Sandbox Code Playgroud)

并再次运行相同的绘图代码:

在此输入图像描述

如您所见,super /下标大小已更改.但不幸的是,它有副作用,如果你看一下这个分数.