Matplotlib如何在两个Y点之间绘制垂直线 [英] Matplotlib how to draw vertical line between two Y points

查看:35
本文介绍了Matplotlib如何在两个Y点之间绘制垂直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个x点有两个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()

这是输出:

如何绘制一条连接每个y点对的细线?所需输出为:

推荐答案

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

这篇关于Matplotlib如何在两个Y点之间绘制垂直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆