简体   繁体   中英

Subscripts in matplotlib axes

I am trying to write some variable alpha with a subscript "mod" while putting text on one of my plots in matplotlib. I know how to do one-letter or one-number subscripts via doing some along the lines of:

r'$\alpha_i$'

which prints alpha with a subscript of "i", but is there anyway to code this so it can write multiple letters as subscripts instead of just one. When I do

  r'$\alpha_mod$'

it print alpha with a subscript of "m" with od in normal letters

Here is a quick example:

import matplotlib.pyplot as plt
ax = plt.gca()
ax.text(2, 2, r'$\alpha_mod$', fontsize=10)
y = [2,3,4,5]
x = [1,2,3,4]
plt.plot(x,y)
plt.show()

Short answer: Use curly brackets $\\alpha_{mod}$

Longer version: Matplotlib uses LaTeX format for strings. In LaTeX, if you want a subscript to include more than one letter you have to use curly brackets.

import matplotlib.pyplot as plt
ax = plt.gca()
ax.text(2, 2, r'$\alpha_{mod}$', fontsize=10)
y = [2,3,4,5]
x = [1,2,3,4]
plt.plot(x,y)
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM