How to display R-squared value on my graph in Python

15,986

Solution 1

If I understand correctly, you want to show R2 in the graph. You can add it to the graph title:

ax.set_title('R2: ' + str(r2_score(y_test, y_predicted)))

before plt.show()

Solution 2

If you want to place the r-squared value on the graph itself, like a graph in Microsoft Excel, you can call the annotate method:

ax.annotate("r-squared = {:.3f}".format(r2_score(y_test, y_predicted)), (0, 1))

or

plt.annotate("r-squared = {:.3f}".format(r2_score(y_test, y_predicted)), (0, 1))

Placing r-squared annotation in upper left of graph

The first argument is the text you wish to place on the graph, and the second argument is the position of the bottom left corner of that text. If you wanted to add another line, like MSE, you could append "\n" and whatever text you wish to the first argument. Keep in mind that the (x, y) tuple is scaled to the data, so if your data range is something other than [0, 1], and you wanted to place the text in the upper left, you may want to change the second argument from (0, 1).

For more information, see https://matplotlib.org/3.3.2/api/_as_gen/matplotlib.axes.Axes.annotate.html or https://matplotlib.org/3.3.2/api/_as_gen/matplotlib.pyplot.annotate.html

Share:
15,986
paula.n
Author by

paula.n

Updated on June 04, 2022

Comments

  • paula.n
    paula.n almost 2 years

    I am a Python beginner so this may be more obvious than what I'm thinking. I'm using Matplotlib to graphically present my predicted data vs actual data via a neural network. I am able to calculate r-squared, and plot my data, but now I want to combine the value on the graph itself, which changes with every new run.

    My NN uses at least 4 different inputs, and gives one output. This is my end code for that:

    y_predicted = model.predict(X_test)
    

    This is how i calculate R2:

    # Using sklearn
    from sklearn.metrics import r2_score
    print r2_score(y_test, y_predicted)
    

    and this is my graph:

    fig, ax = plt.subplots()
    ax.scatter(y_test, y_predicted)
    ax.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=4)
    ax.set_xlabel('Actual')
    ax.set_ylabel('Predicted')
    #regression line
    y_test, y_predicted = y_test.reshape(-1,1), y_predicted.reshape(-1,1)
    ax.plot(y_test, LinearRegression().fit(y_test, y_predicted).predict(y_test))
    
    plt.show()
    

    It gives something like the graph attached, and the R2 varies everytime I change the epochs, or number of layers, or type of data etc. The red is my line of regression, which I will label later. Since R2 is a function I can't simply use the legend or text code.

    I would also like to display MSE.

    Can anyone help me out?

    Graph

  • Daniel Lima
    Daniel Lima over 4 years
    Could also use an annotation: matplotlib.org/3.1.1/api/_as_gen/…
  • paula.n
    paula.n over 4 years
    Thank you so much Ioanna, literally what I was looking for! :)
  • Maxim Lott
    Maxim Lott over 2 years
    Any way to place it on the line itself? And of course the line might move, so it would be nice if there were a way to tell it be on the line, specifically.