-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscatter.py
More file actions
45 lines (41 loc) · 1.11 KB
/
scatter.py
File metadata and controls
45 lines (41 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import matplotlib.pyplot as plt
import numpy as np
# 定义画散点图的函数
def draw_scatter(n, s):
"""
:param n: 点的数量,整数
:param s:点的大小,整数
:return: None
"""
# 加载数据
data = np.loadtxt('/Users/jinx/Downloads/results.txt', encoding='utf-8', delimiter=',')
# 通过切片获取横坐标x1
x1 = data[:, 0]
# 通过切片获取纵坐标R
y1 = data[:, 1]
# 横坐标x2
x2 = np.random.uniform(0, 5, n)
# 纵坐标y2
y2 = np.array([1] * n)
# 创建画图窗口
fig = plt.figure()
# 将画图窗口分成1行1列,选择第一块区域作子图
ax1 = fig.add_subplot(1, 1, 1)
# 设置标题
ax1.set_title('散点图')
# 设置横坐标名称
ax1.set_xlabel('x')
# 设置纵坐标名称
ax1.set_ylabel('y')
# 画散点图
ax1.scatter(x1, y1, s=s, c='k', marker='.')
# 画直线图
ax1.plot(x2, y2, c='b', ls='--')
# 调整横坐标的上下界
plt.xlim(xmax=60, xmin=-60)
# 显示
plt.show()
# 主模块
if __name__ == "__main__":
# 运行
draw_scatter(n=2000, s=20)