简体   繁体   English

如何将文本框添加到 Matplotlib 图的外部?

[英]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).我有一个带有一些重要指标(RMSE、MAE 和 MAPE)的RandomForestRegressor()图。 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 .使用plt.text() ,如文档中所示 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')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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