简体   繁体   中英

Drawing scattered plot using python

I want to draw a scattered plot using python. I have these two 2D arrays and I want to show them in same scattered plot.

[[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]]

How can I do this? I want points of different arrays to be of different colors. I'm using python 3.x

You can use Matplotlib scatter tool to graph your points. Here is how you'll do it, applying their example:

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

This will give you this nice looking graph:

散点图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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