简体   繁体   English

如何使用Matplotlib绘制两个元组列表

[英]How to plot two lists of tuples with Matplotlib

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. 在一个图中,我想为所有i绘制所有这些点并(yi_begin, yi_end)(xi_begin, xi_end)之间绘制线。

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()

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 给你

在此处输入图片说明

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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM