>

LoeiJe

:D 获取中...

何以解忧?唯有暴富

numpy学习笔记

numpy学习笔记
matplotlib 基础

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 2019-10-23
# numpy 学习笔记
# icenaive
# 参考: https://github.com/lijin-THU/notes-python
# 仅供个人学习使用
#
# 2. matplotlib 基础

import matplotlib.pyplot as plt
import numpy as np
# from numpy import *

# import 模块:导入一个模块;注:相当于导入的是一个文件夹,是个相对路径。
# from…import:导入了一个模块中的一个函数;注:相当于导入的是一个文件夹中的文件,
# 是个绝对路径。
# from…import *语句与import区别在于:
# import 导入模块,每次使用模块中的函数都要是定是哪个模块。
# from…import * 导入模块,每次使用模块中的函数,直接使用函数就可以了;
# 注因为已经知道该函数是那个模块中的了。


# 参数形式
# plot(y)
# plot(x, y)
# plot(x, y, format_string)
# 仅给定y值,默认以下标为x轴
x = np.linspace(0, 2 * np.pi, 50)
# plt.plot(sin(x))
# plt.show()

# 给定x和y值
# plt.plot(x, sin(x))
# plt.show()

# 多条数据线
# plt.plot(x, sin(x), x, sin(2 * x), x, sin(x ** 2))
# plt.show()

# 使用字符串 指定线条参数
# plt.plot(x, sin(x ** 2), 'b-^')
# plt.show()

# 多线条
# plt.plot(x, sin(x), 'r-o', x, sin(x ** 2), 'g-^')
# plt.show()


# 散点图
# 参数形式
# scatter(x, y)
# scatter(x, y, size)
# scatter(x, y, size, color)

# plt.plot(x, sin(x), 'bo')
# plt.show()

# plt.title('scatter')
# # plt.scatter(x, sin(x), 'bo')
# plt.xlabel('x')
# plt.ylabel('sin(x)')
# plt.scatter(x, sin(x))
# plt.show()

x = np.random.rand(300)
y = np.random.rand(300)
size = np.random.rand(400) * 30
color = np.random.rand(300)
# plt.scatter(x, y, size, color)
# plt.colorbar()
# plt.show()

# 多图
t = np.linspace(0, 2 * np.pi, 50)
x = np.sin(t)
y = np.cos(t)
# fig1 = plt.figure()
# plt.plot(x)
# # plt.show()
# fig2 = plt.figure()
# plt.plot(y)
# plt.show()

# plt.subplot(1, 2, 1)
# plt.plot(x)
# # plt.show()
# plt.subplot(1, 2, 2)
# plt.plot(y)
# plt.show()

# 向图中添加数据

# plt.plot(x)
# # plt.hold(False) # 新图会将原图覆盖 # 高版本那中已删除
# plt.plot(y)
# plt.show()

# 标签
# 加入label 通过legend 加入图例
# plt.plot(x, label='sin')
# plt.plot(y, label='cos')
# plt.legend()
# 在legend中加入
# plt.plot(x)
# plt.plot(y)
# plt.legend(['sin', 'cos'])
# plt.show()

# 坐标轴 标题 网格

# plt.plot(x, np.sin(x))
# plt.xlabel('radians')

# # 字体大小
# plt.ylabel('amplitude', fontsize = 'large')
# plt.title('sin(x)')

# # 清楚已有的图像
# plt.clf()
# plt.show()
# plt.plot(y)
# plt.show()
# # 关闭当前图像
# # plt.close() # 参数 ‘all’关闭所有
# # 网格
# plt.grid()
# plt.show()

# imshow 显示图片
# 灰度图片可以看作是二维数组

# lena 高版本中移除了
from scipy.misc import ascent
img = ascent()
# print(img)
plt.imshow(img,
# 设置坐标范围
extent = [-25, 25, -25, 25],
# 设置colormap
# cmap = plt.cm.bone
cmap=plt.cm.RdGy_r
)
plt.colorbar()
# plt.show()
plt.clf()
# 直方图
# 高斯分布中随机生成1000个点得到直方图
plt.hist(np.random.randn(1000))
plt.show()