1

I have two lists where each element is a tuple that should be interpreted as

x = [(x1_begin, x1_end), (x2_begin, x2_end), ... , (xn_begin, xn_end)]
y = [(y1_begin, y1_end), (y2_begin, y2_end), ... , (yn_begin, yn_end)] 

In one figure, I would like to plot all these points and draw lines only between (yi_begin, yi_end) vs (xi_begin, xi_end) for all i.

The following code manages to plot all the points. But I'm not sure how to draw the lines properly between the points. Any help is much appreciated.

import matplotlib.pyplot as plt

x = [(1, 27), (32, 55), (56, 80), (84, 103)]
y = [(5, 7), (3, 6), (4, 9), (6, 11)]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(x, y, color='black')
plt.show()
Hunter
  • 357
  • 8
  • 15
  • I am a bit confused. Is your x and y corresponding to coordinates on the x, y axis? – Spinor8 Aug 05 '18 at 08:59
  • @Spinor8 yes, so the range of the x-axis should be 1 to 103, and of the y-axis 3 to 11. I hope that clarifies your question? – Hunter Aug 05 '18 at 09:02
  • So your data is essentially: list_1 = [(line1_start_x, line1_end_x) ,.... ] and list_2 = [(line1_start_y, line1_end_y) ....], right? – Spinor8 Aug 05 '18 at 09:06
  • (yi_begin, yi_end) vs (xi_begin, xi_end) means a line between (xi_begin,yi_begin) to (xi_end,yi_end) ? – Krishna Aug 05 '18 at 09:08
  • In the example code above, you'd expect 4 lines, right? – Spinor8 Aug 05 '18 at 09:09
  • 1
    Also, seems like a duplicate of [this question](https://stackoverflow.com/questions/35363444/plotting-lines-connecting-points). Please use search before posting. – Maksim Terpilowski Aug 05 '18 at 09:20

2 Answers2

2

Iterate over your tuples:

import matplotlib.pyplot as plt

x = [(1, 27), (32, 55), (56, 80), (84, 103)]
y = [(5, 7), (3, 6), (4, 9), (6, 11)]

fig = plt.figure()
ax = fig.add_subplot(111)
for xt, yt in zip(x,y):
    ax.plot(xt, yt, color='black')
plt.show()

enter image description here

1

If indeed you are asking for one line per tuple, here's the code.

fig = plt.figure()
ax = fig.add_subplot(111)
assert len(x) == len(y)
for i in range(len(x)):
    plt.plot(x[i], y[i])
plt.show()

giving you

enter image description here

Spinor8
  • 1,587
  • 4
  • 21
  • 48