Python matplotlib label index based on loop counter
I am using python and matplotlib to generate graphical output. I am creating multiple plots inside a loop and would like the loop counter to serve as an index on the y-axis label. How do I get the loop counter (variable) so that it appears as an index?
Here's what I have:
axis_ylabel = plt.ylabel(u"\u03b1 [\u00b0]", rotation='horizontal', position=(0.0,0.9))
as a result:
Ξ± [Β°]
(I am using unicode instead of Tex because dvipng is not available on this system.)
I would like something like this:
for i in range(1,3):
axis_ylabel = plt.ylabel(u"\u03b1" + str(i) + u" [\u00b0]", rotation='horizontal', position=(0.0,0.9))
Not surprisingly, this gives:
Ξ±1 [Β°]
Ξ±2 [Β°]
What I really want are numbers that are indices. How to concatenate conversion to string using command to create index? The unicode include "_" is not used to create the index. Also, I still need python / matplotlib to figure out that this command-index should affect the next variable. Any thoughts?
Edit
I've come this far:
axis_ylabel = plt.ylabel(u"\u03b1" + r"$_" + str(i) + r"$" + u" [\u00b0]", rotation='horizontal', position=(0.0,0.9))
- this results in an index character. However, this is NOT an integer conversion, but a different character.
Edit 2
I am using python 2.6.6 and matplotlib 0.99.1.1. Inserting any line into <>
in r"$<>$"
will not display this line, but will be a completely different character. I posted this issue as a new question .
source to share
Matplotlib ships its own math expression engine called mathtext
.
From the doc:
Note that you do not need to install TeX as matplotlib ships its own TeX expression tag, layout engine and fonts.
So maybe try using the following:
for i in range(1,3):
plt.ylabel(
r"$\alpha_{:d} [\degree]$".format(i),
rotation='horizontal',
position=(0.0,0.9))
You can also use Unicode in mathtext
:
If a particular character does not have a name (as is true of many more hidden characters in STIX fonts), Unicode characters can also:
ur'$\u23ce$'
source to share