Python >> Tutoriel Python >  >> Python Tag >> Matplotlib

Matplotlib comment tracer une ligne verticale entre deux points Y

ajoutez simplement plt.plot((x,x),([i for (i,j) in y], [j for (i,j) in y]),c='black')


Alternativement, vous pouvez également utiliser LineCollection. La solution ci-dessous est adaptée de cette réponse.

from matplotlib import collections as matcoll

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

lines = []
for i, j in zip(x,y):
    pair = [(i, j[0]), (i, j[1])]
    lines.append(pair)

linecoll = matcoll.LineCollection(lines, colors='k')

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