简体   繁体   中英

Visualize distance to expected value in a scatter plot in Seaborn

I want to create a scatter plot (for discrete x and only one dot per x) and for each dot, I want to visualize the distance to its expected value with a line, preferably in Seaborn.

Basically, I want something like this (taken from this post ), but I want the error bars to only go into one direction, not up and down. The line of the error bar should end where my expected value is.

Edit: An example.

Some code:

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

Also, it does not have to look exactly like this. Just something in this direction.

The most efficient way to produce several lines is to use a LineCollection . To also show the dots, you would use an additional 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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM