Thrastylon
Thrastylon

Reputation: 980

How to draw a line between a data point and an axis in matplotlib?

Using matplotlib one can use:

My question is: what is the simplest way to do half of each? Typically, drawing a segment from a datapoint to its coordinate on the x or y axis.

Upvotes: 0

Views: 1056

Answers (1)

Karina
Karina

Reputation: 1280

In my opinion, this is simple enough (4 lines):

fig, ax = plt.subplots()
x = np.arange(10)
y = np.arange(10)
ax.plot(x, y)
# starting from here
ymin, ymax = ax.get_ylim()
for i in range(len(x)):
    ax.vlines(x[i], ymin, y[i])
ax.set_ylim(ymin, ymax)

Output:

enter image description here

Upvotes: 1

Related Questions