I am trying to automatically label the x-axis of a Matplotlib plot in Python2.7 with a chemical formula that has numbers as subscripts, without italics.
Here is a code to show this:
import numpy as np
import pylab as plt
x = np.array([1,2,3,4])
y = np.array([1,2,3,4])
chem_list = ['H2O','CO2','X7Z4']
for j, elem in enumerate(chem_list):
plt.plot(x,y)
plt.xlabel(chem_list[j])
plt.legend(loc=1)
plt.show()
The x-axis labels come from a list. Each item in the list is a label and I need to use it as the text in the label.
This method works, however, the text is italicized and I need to avoid italic text. Another post that produces italic text: here.
Example of my question:
For the first iteration of the loop, I would like to use H2O ax the x-axis label, but I do not want it to be italicized.
Is there a way to do this in Python?
Based on the documentation (http://matplotlib.org/users/mathtext.html), you can use \mathregular{...}
to use regular text within a mathtext expression. For you case, for example, you would write:
chem_list = ['$\mathregular{H_2O}$','$\mathregular{CO_2}$','$\mathregular{X_7Z_4}$']