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
  • 14,043
  • 5
  • 29
  • 55
Gabriele Valvo
  • 196
  • 2
  • 12

3 Answers3

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
  • 14,043
  • 5
  • 29
  • 55
2

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
  • 622
  • 1
  • 5
  • 17
0

you could also try:

plt.text(5, 1, f"$R^2$ score EO: {r2_train_EO:0.2f}", style='italic')

Pietro
  • 3
  • 1