Search code examples
pythonmatplotlibgraphanacondaspyder

How to include the squared symbol in a graph label


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


Solution

  • 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')