简体   繁体   中英

How do I add a text box to the outside of a Matplotlib plot?

I have a RandomForestRegressor() plot with some important metrics (RMSE, MAE, and MAPE). I have the plot already but I need these metrics in a text box placed just outside of the plot. Here is what it looks like:

sn.set_style('dark')
# plot the model
plot = sn.scatterplot(plant_target, predicted_values_plant_1)
plot.set(xlabel='Given', ylabel='Prediction',title='Usine Random Forest Cross Validation (rain + debit + sierra)')
# generate and graph y = x line
x_plot = np.linspace(0,600,600)
y_plot = x_plot
plt.plot(x_plot, y_plot, color='r')
print('RMSE: '+ str(rmse_plant_1))
print('MAE: '+ str(mae_plant_1))
print('MAPE: '+ str(smape_score_plant_1))

Output:

在此处输入图片说明

How do I create a text box to add these metrics just to the right of the plot?

Use plt.text() , as found in the docs . For example:

import numpy as np
import seaborn as sn

plant_target, predicted_values_plant_1 = [100,200,300,400,500],[100,300,400,400,500]

rmse_plant_1 = 0.7
mae_plant_1 = 0.8
smape_score_plant_1 = 0.9

sn.set_style('dark')

# plot the model
plot = sn.scatterplot(x=plant_target, y=predicted_values_plant_1)
plot.set(xlabel='Given', ylabel='Prediction',title='Usine Random Forest Cross Validation (rain + debit + sierra)')

# generate and graph y = x line
x_plot = np.linspace(0,600,600)
y_plot = x_plot

# *** ADDING TEXT HERE ***
metrics = 'RMSE: '+ str(rmse_plant_1) + '\n' + 'MAE: '+ str(mae_plant_1) + '\n' + 'MAPE: '+ str(smape_score_plant_1)
plt.text(400, 100, metrics)

plt.plot(x_plot, y_plot, color='r')

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM