【问题标题】:How to plot deviation above and below the line x=y line in a figure?如何在图中的 x=y 线上方和下方绘制偏差?
【发布时间】:2021-12-16 22:34:17
【问题描述】:

我已经使用 python 绘制了数据。以下是代码的摘录:

#matplotlib 

ax = df.plot.scatter(x = 'exp_CO', y = 'sim_CO',s=30, color='g' )


#plot the line of correlation:x=y
lims = [
    np.min([ax.get_xlim(), ax.get_ylim()]),  # min of both axes
    np.max([ax.get_xlim(), ax.get_ylim()]),  # max of both axes
]


# now plot both limits against eachother
ax.plot(lims, lims, 'k-', alpha=0.75, zorder=0)
ax.set_aspect('equal')
ax.set_xlim(lims)
ax.set_ylim(lims)

plt.title('experimental $^{13}{CO}$ chemical shift (ppm) vs simulated $^{13}{CO}$ chemical shift (ppm)')
plt.xlabel('exp_CO',weight='semibold', size = 20)
plt.ylabel('sim_CO',weight='semibold', size = 20)
plt.show()

现在我想在 x=y 线的下方和上方绘制一条线,这基本上表示该线上方和下方的误差为 2.45。 这就是绘图应该看起来像这样,上面和下面的粉色线到中间线的距离应该覆盖 2.45:

我如何绘制这个?任何帮助表示赞赏。提前致谢。

【问题讨论】:

    标签: python plot


    【解决方案1】:

    我认为你应该添加这个:

    plt.plot(lims, lims - 2.45, color="r")
    plt.plot(lims, lims + 2.45, color="r")
    

    【讨论】:

    • 很遗憾,这不起作用。它显示了这种类型的错误:typeError: unsupported operand type(s) for -: 'list' and 'float'
    【解决方案2】:

    基本上,您只需要在 x=y 行旁边再增加两行,减去您的“间隔宽度”。我没有你的数据,所以我自己生成了一个完整的例子:

    import numpy as np
    import matplotlib.pyplot as plt
    
    lin_x = np.arange(0, 10, 0.5)
    noise = np.random.uniform(-1, 1, lin_x.shape[0])
    noise_std = np.std(noise) # In your case this may be =2.45
    
    plt.close()
    plt.plot(lin_x)
    plt.scatter(range(lin_x.shape[0]), lin_x+noise)
    plt.plot(lin_x + 2.*noise_std, c='pink')
    plt.plot(lin_x - 2.*noise_std, c='pink')
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 2020-01-23
      • 1970-01-01
      • 2015-09-13
      • 2015-12-09
      • 2020-09-16
      • 2015-10-16
      相关资源
      最近更新 更多