在 matplotlib 中绘制预测和 ground_truth 点之间的线

mat*_*hew 2 python matplotlib prediction

我有两个数据框,ground_truth 和预测(都是熊猫系列)。最后,我想像我已经做的那样绘制所有预测点和所有ground_truth 点。我想要做的是每个预测和 ground_truth 点之间绘制一条线。所以这条线是预测点x1,y1和ground_truth点x2,y2之间的连接。为了更好的理解,我附上了一张图片。黑线(通过油漆创建)是我想要做的。例子

这是我已经拥有的:

fig, ax = plt.subplots()

ax.plot(pred,'ro', label='Prediction', color = 'g')
ax.plot(GT,'^', label='Ground Truth', color = 'r' )

plt.xlabel('a')
plt.ylabel('b')
plt.title('test')

plt.xticks(np.arange(-1, 100, 5))
plt.style.use('ggplot')
plt.legend()                
plt.show()
Run Code Online (Sandbox Code Playgroud)

Imp*_*est 7

我想最简单和最容易理解的解决方案是在循环之间predGT循环中绘制相应的线。

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['legend.numpoints'] = 1

#generate some random data
pred = np.random.rand(10)*70
GT =  pred+(np.random.randint(8,40,size= len(pred))*2.*(np.random.randint(2,size=len(pred))-.5 ))

fig, ax = plt.subplots(figsize=(6,4))

# plot a black line between the 
# ith prediction and the ith ground truth 
for i in range(len(pred)):
    ax.plot([i,i],[pred[i], GT[i]], c="k", linewidth=0.5)
ax.plot(pred,'o', label='Prediction', color = 'g')
ax.plot(GT,'^', label='Ground Truth', color = 'r' )

ax.set_xlim((-1,10))
plt.xlabel('a')
plt.ylabel('b')
plt.title('test')

plt.legend()             
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明