【问题标题】:Visualize distance to expected value in a scatter plot in Seaborn在 Seaborn 的散点图中可视化与预期值的距离
【发布时间】:2019-05-16 14:05:11
【问题描述】:

我想创建一个散点图(对于离散的 x 并且每个 x 只有一个点),对于每个点,我想用一条线可视化到其预期值的距离,最好在 Seaborn 中。

Basically, I want something like this(取自this post),但我希望误差线只进入一个方向,而不是向上向下。错误栏的行应在我的预期值处结束。

编辑:一个例子。

一些代码:

import matplotlib.pyplot as plt

some_y=[1,2,3,7,9,10]
expected_y=[2, 2.5, 2, 5, 8.5, 9.5]

plt.plot(some_y, ".", color="blue")
plt.plot(expected_y, ".", color="red")
plt.show()

Looks like this

What I would like to do

此外,它不必看起来像这样完全。只是朝着这个方向发展。

【问题讨论】:

  • plt.plot 生成一行。用几个plt.plotLineCollection 生成了几行。使用plt.scatter 生成散点图。也许您想详细了解您尝试过的内容以及遇到的问题。
  • 也许,我的描述有点不清楚。我更新了我的帖子。

标签: python distance seaborn scatter-plot errorbar


【解决方案1】:

产生多行的最有效方法是使用LineCollection。要同时显示这些点,您可以使用额外的 scatter

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

some_y=[1,2,3,7,9,10]
expected_y=[2, 2.5, 2, 5, 8.5, 9.5]

x = np.repeat(np.arange(len(some_y)), 2).reshape(len(some_y), 2)
y = np.column_stack((some_y, expected_y))
verts = np.stack((x,y), axis=2)

fig, ax = plt.subplots()
ax.add_collection(LineCollection(verts))
ax.scatter(np.arange(len(some_y)), some_y)

plt.show()

【讨论】:

  • 是的,就是这样。谢谢!
猜你喜欢
  • 2013-11-01
  • 2021-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 2016-12-14
  • 2021-09-12
  • 2014-11-28
相关资源
最近更新 更多