如何使用 Matplotlib 绘制两个元组列表 [英] How to plot two lists of tuples with Matplotlib

查看:66
本文介绍了如何使用 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)] 

在一个图中,我想绘制所有这些点并在(yi_begin,yi_end)(xi_begin,xi_end)code>代表所有i.

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

推荐答案

如果确实要为每个元组请求一行,则代码如下.

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

给你

这篇关于如何使用 Matplotlib 绘制两个元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆