With matplotlib, it is possible to use latex to label axes and plots. i.e.
import matplotlib.pyplot as plt
data = pandas.DataFrame({'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14,15]})
data['A/B'] = data['A']/data['B']
plt.plot(data['C'], data['A/B'])
new_name1 = 'new_name'
new_name2 = 'new_name'
plt.title(r'$\frac{A}{B}$')
plt.show()
I would like to combine latex with python's string .format method. (While not necessary in this abstracted example, it is in my own work).
import matplotlib.pyplot as plt
data = pandas.DataFrame({'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14,15]})
data['A/B'] = data['A']/data['B']
plt.plot(data['C'], data['A/B'])
new_name1 = 'new_name1'
new_name2 = 'new_name2'
plt.title(r'$\frac{\{\}}{\{\}}$'.format(new_name1, new_name2))
plt.show()
gives me:
Traceback (most recent call last):
File "/home/b3053674/Documents/LargeStudy/large_study/plot_locally.py", line 912, in <module>
plt.title(r'$\frac{\{\}}{\{\}}$'.format(new_name1, new_name2))
KeyError: '\\{\\}'
and
import matplotlib.pyplot as plt
data = pandas.DataFrame({'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14,15]})
data['A/B'] = data['A']/data['B']
plt.plot(data['C'], data['A/B'])
new_name1 = 'new_name'
new_name2 = 'new_name'
plt.title(r'$\frac{{}}{{}}$'.format(new_name1, new_name2))
plt.show()
gives me:
ValueError:
\frac{}{}
^
Expected \frac{num}{den} (at char 5), (line:1, col:6)
Is it possible to use string formatting with latex in this way? If so, how do I do it?
