Matplotlib 中如何绘制 x,y 坐标列表
Jinku Hu
2020年6月25日
2020年1月28日
假设我们有一个类似 (x, y)
的二元组列表,我们需要将它们绘制为 (x, y)
坐标。
data = [
[1, 2],
[3, 2],
[4, 7],
[2, 4],
[2, 1],
[5, 6],
[6, 3],
[7, 5],
]
在 Matplotlib 中绘制此 (x, y)
坐标列表的完整代码如下,
import matplotlib.pyplot as plt
data = [
[1, 2],
[3, 2],
[4, 7],
[2, 4],
[2, 1],
[5, 6],
[6, 3],
[7, 5],
]
x, y = zip(*data)
plt.scatter(x, y)
plt.show()
x, y = zip(*data)
它使用 zip
函数将数据从成对的数据包解压缩到列表。
plt.scatter(x, y)
我们需要创建散点图,因此 scatter
是在此例中使用的正确的图型类型。
Author: Jinku Hu
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn