【问题标题】:Plot a line between prediction and ground_truth point in matplotlib在 matplotlib 中的预测和 ground_truth 点之间绘制一条线
【发布时间】:2016-11-22 12:45:27
【问题描述】:

我有两个数据框,ground_truth 和 prediction(都是熊猫系列)。最后,我想绘制所有 预测点 和所有 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()

【问题讨论】:

    标签: python matplotlib prediction


    【解决方案1】:

    我想最简单和最容易理解的解决方案是在循环中绘制 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()
    

    【讨论】:

      【解决方案2】:

      您可以将每条线绘制为单独的图。您可以创建一个循环并为连接两点的每条线调用plot。但是,您也可以给 plot(x, y, ...) 两个二维数组作为参数。 x 中的每一列将对应于y 中的同一列,并在图中用一条线表示。所以你需要生成这两个。它可能看起来像这样:

      L = len(pred)
      t = np.c_[range(L), range(L)].T
      ax.plot(t, np.c_[pred, GT].T, '-k')
      

      【讨论】:

      • 我喜欢这个解决方案!非常紧凑! (我猜对于没有经验的人来说很难理解)。
      • 谢谢 :) 是的,它不那么可读。但是c_是一个numpy函数,它从参数生成一个列数组,T是转置函数。
      【解决方案3】:

      您可以使用 matplotlib 误差条 (http://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.html) 来实现这一点,其想法是在您正在绘制的两条线的平均值周围绘制误差条:

      这是一个展示我想法的最小示例:

      import numpy as np
      import pandas as pd
      import matplotlib.pyplot as plt
      
      # example data
      x = np.arange(0.1,10, 0.5)
      y1 = pd.Series(np.exp(-x), index = x)
      y2 = pd.Series(np.exp(-x)+ np.sin(x), index = x)
      avg_line = (y1 + y2)*0.5
      
      err = (avg_line - y1).abs()
      
      fig, ax = plt.subplots(1)
      y1.plot(marker = 'o', label='Prediction', color = 'g', linestyle  = '', ax = ax)
      y2.plot(marker = '^', label='Ground Truth', color = 'r', linestyle  = '', ax = ax)
      ax.errorbar(x, avg_line.values, yerr=err.values, fmt= 'none', ecolor = 'k', barsabove = False, capthick=0)
      plt.style.use('ggplot')
      ax.legend()
      

      希望这能解决您的问题。

      【讨论】:

        猜你喜欢
        • 2018-12-19
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        • 2019-08-15
        • 1970-01-01
        • 2021-04-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多