Using matplotlib one can use:
plt.hlines/vlines
to draw segments (e.g. between two data points)plt.axhline/axvline
to draw lines relative to the axes
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.
Advertisement
Answer
In my opinion, this is simple enough (4 lines):
JavaScript
x
10
10
1
fig, ax = plt.subplots()
2
x = np.arange(10)
3
y = np.arange(10)
4
ax.plot(x, y)
5
# starting from here
6
ymin, ymax = ax.get_ylim()
7
for i in range(len(x)):
8
ax.vlines(x[i], ymin, y[i])
9
ax.set_ylim(ymin, ymax)
10
Output: