我想使用python绘制一个分散的图。我有这两个2D数组,我想在相同的散点图中显示它们。
[[69, 72], [72, 80], [74, 81], [70, 75], [78, 87], [71, 73], [69, 70], [71, 77]]
[[78, 139], [80, 158], [85, 154], [72, 105], [84, 148], [74, 87], [73, 106], [71, 109]]
我该怎么做?我希望不同阵列的点具有不同的颜色。 我使用的是python 3.x
答案 0 :(得分:1)
您可以使用 Matplotlib scatter工具来绘制您的观点。以下是您将如何应用他们的示例:
import matplotlib.pyplot as plt
import matplotlib
array1 = [[69, 72], [72, 80], [74, 81], [70, 75], [78, 87], [71, 73], [69, 70], [71, 77]]
array2 = [[78, 139], [80, 158], [85, 154], [72, 105], [84, 148], [74, 87], [73, 106], [71, 109]]
x1 = [point[0] for point in array1]
y1 = [point[1] for point in array1]
x2 = [point[0] for point in array2]
y2 = [point[1] for point in array2]
s = 20
plt.scatter(x1, y1, s, c="r", alpha=0.5, marker=r'o',
label="Array 1")
plt.scatter(x2, y2, s, c="b", alpha=0.5, marker=r'o',
label="Array 2")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend(loc=0)
plt.show()
这将为您提供这个漂亮的图表: