Python线性方程误差限额

时间:2017-12-12 16:44:59

标签: python matplotlib

我在预测值和实际值之间绘制了一个图表,如下所示。现在我需要在图表上做出线性线,误差允许值为20%。即该区域内的点有20%的误差。 以下是我的代码

error_percents = list(((plane_df['Predicted Cost']-plane_df['Actual Cost'])/plane_df['Actual Cost'])*100)
x = list(plane_df['Actual Cost']/100)
y = list(plane_df['Predicted Cost']/100)
max_xy = max(max(x), max(y))
lin_x = np.arange(max_xy)
lin_y = np.arange(max_xy)

fig2 = plt.figure(figsize=(15,10))
axes = fig2.add_axes([0,0,1,1])
axes.scatter(x,y,s=100, marker='+')
axes.plot(lin_x, lin_y, c='green', label="zero error")
axes.legend()

Graph

我有零误差线(x = y),其中误差为0,我如何形成误差为20%的线并绘制它?请指导我如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

要在图中标记正/负20%区域,您可以执行以下操作:

import numpy as np
import matplotlib.pyplot as plt

xs = np.arange(0, 100, 1)
ys = np.arange(0, 100, 1)
errs = ys * 0.2

fig = plt.figure()
plt.xlabel('Actual cost')
plt.ylabel('Predicted cost')
plt.fill_between(xs, ys-errs, ys+errs, label='+-20 percent region', alpha=0.3, color='red')
plt.plot(xs, ys, color='green', label='zero error')
plt.legend(loc='upper left')
plt.savefig('test.png')

这导致以下情节: enter image description here

相关问题