在Matplotlib中,如何设置上标的字体大小(除了控制基本字体的大小)?例如,使用Matplotlib创建带有坐标轴的科学图形图形:设置刻度标签的字体大小很容易,但是如何指定其指数的字体大小呢?我想在指数和基数上有一个不同的控件(即,打勾标记的字体大小以获得所需大小的指数不是一个好选择-我们可以修改字体大小的比率吗?基本和指数?)。谢谢。
如果您有指数,则获得文本基本上有两种可能性:
rcParams['text.usetex'] == True
)。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
您可以更改这些设置以使文本间距不同。例如,让我们制作一个简单的测试图:
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)
这给出:
然后我们做一些魔术:
import matplotlib
matplotlib.mathtext.SHRINK_FACTOR = 0.5
matplotlib.mathtext.GROW_FACTOR = 1 / 0.5
并再次运行相同的绘图代码:
如您所见,上标/下标的大小已更改。但不幸的是,如果您查看分数,它会产生副作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句