Matplotlib 如何在两个 Y 点之间绘制垂直线

iso*_*01_ 6 python plot matplotlib scatter-plot

每个 x 点我有 2 个 y 点。我可以用这个代码绘制图:

import matplotlib.pyplot as plt

x = [0, 2, 4, 6]
y = [(1, 5), (1, 3), (2, 4), (2, 7)]


plt.plot(x, [i for (i,j) in y], 'rs', markersize = 4)
plt.plot(x, [j for (i,j) in y], 'bo', markersize = 4)

plt.xlim(xmin=-3, xmax=10)
plt.ylim(ymin=-1, ymax=10)

plt.xlabel('ID')
plt.ylabel('Class')
plt.show()
Run Code Online (Sandbox Code Playgroud)

这是输出:

样本图

如何绘制连接每个 y 点对的细线?期望的输出是:

期望输出

Gui*_*ski 6

只需添加 plt.plot((x,x),([i for (i,j) in y], [j for (i,j) in y]),c='black')

在此处输入图片说明