Matplotlib: set font size of superscript

In Matplotlib, how can I set the font size of a superscript (besides controlling the font size of the database)? For example, creating a graph with Matplotlib with axes in scientific notation: it is easy to set the font size of label labels, but how can I specify the font size of their exhibitors? I want to have different control over the base AND on the exponent (i.e. playing on the font size of label labels to get an indicator of the desired size is not a good option - can we change the ratio of the font sizes of the base and the exponent?). Thank.

+4
source share
1 answer

If you have indicators, there are two possibilities that you have received:

  • TeX ( rcParams['text.usetex'] == True).
  • mathtext Tex, matplotlib

TeX, TeX ( \DeclareMathSizes{10}{18}{12}{8}, ).

"" , matplotlib. , ; 70% TeX .


, "", . ...

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)

:

enter image description here

:

import matplotlib

matplotlib.mathtext.SHRINK_FACTOR = 0.5
matplotlib.mathtext.GROW_FACTOR = 1 / 0.5

:

enter image description here

, / . , , , .

+7

Source: https://habr.com/ru/post/1547974/


All Articles