在python matplotlib中绘制(x,y)坐标列表 [英] Plotting a list of (x, y) coordinates in python matplotlib

查看:635
本文介绍了在python matplotlib中绘制(x,y)坐标列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个成对的(a,b)列表,我想用python中的 matplotlib 进行绘制作为实际的xy坐标。当前,它正在制作两个图,其中列表的索引给出x坐标,第一个图的y值是对中的 a s,第二个图的y值是成对的 b

I have a list of pairs (a, b) that I would like to plot with matplotlib in python as actual x-y coordinates. Currently, it is making two plots, where the index of the list gives the x-coordinate, and the first plot's y values are the as in the pairs and the second plot's y values are the bs in the pairs.

为澄清起见,我的数据如下所示: li = [(a,b),(c,d),...,(t,u)]
我想做一个只调用<$ c $的单线c> plt.plot()不正确。
如果我不需要一线纸,我可以轻松地做一下:

To clarify, my data looks like this: li = [(a,b), (c,d), ... , (t, u)] I want to do a one-liner that just calls plt.plot() incorrect. If I didn't require a one-liner I could trivially do:

xs = [x[0] for x in li]
ys = [x[1] for x in li]
plt.plot(xs, ys)

如何获取matplotlib将这些对绘制为xy坐标?

How can I get matplotlib to plot these pairs as x-y coordinates?

推荐答案

按照此示例

import numpy as np
import matplotlib.pyplot as plt

N = 50
x = np.random.rand(N)
y = np.random.rand(N)

plt.scatter(x, y)
plt.show()

将会产生:

要将数据从成对的数据包分解为列表,请使用 zip

To unpack your data from pairs into lists use zip:

x, y = zip(*li)

因此,单线:

plt.scatter(*zip(*li))

这篇关于在python matplotlib中绘制(x,y)坐标列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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