1

I want to plot the value of R2 in my graph, but I don't know how to write the "2" as an exponent. How can I do that? Below you can find the code and the plot.

plt.figure()
plt.plot((y_train1),(y_train_pred),'.', color='darkviolet', alpha=1, marker='o', markersize = 2, markeredgecolor = 'black', markeredgewidth = 0.1)
plt.plot((np.array((-0.1,7))),(np.array((-0.1,7))),'-', color='magenta')
plt.xlabel('True')
plt.ylabel('Predicted')
plt.title('Train')
plt.text(5, 1, "R2 score EO: {:0.2f}".format(r2_train_EO), style='italic')

enter image description here

CDJB
  • 13,204
  • 5
  • 27
  • 47

2 Answers2

3

While the answer referencing latex would work - having to install latex for such a small change is a lot of hassle. This could be achieved by just using the unicode squared symbol in the label string:

plt.text(5, 1, u"R\u00b2 score EO: {:0.2f}".format(r2_train_EO), style='italic')
CDJB
  • 13,204
  • 5
  • 27
  • 47
1

You should configure pyplot to use Latex:

from matplotlib import rc
rc('text', usetex=True) 

Then write R^2 in your label.

Edit: the above is recommended if you also want to control fonts. If that is not needed, just write '$R^2$' in your text - this is also Latex.

Stefan
  • 577
  • 1
  • 4
  • 15