forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07.generate_array.py
More file actions
65 lines (52 loc) · 1.54 KB
/
07.generate_array.py
File metadata and controls
65 lines (52 loc) · 1.54 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
@description:
@author:XuMing
"""
from __future__ import print_function
from __future__ import unicode_literals
import numpy as np
# 生成数组的函数
# arange 生成数组,[start,stop)
# arange(start, stop=None, step=1, dtype=None)
print(np.arange(5)) # [0 1 2 3 4]
a = np.arange(0, 2 * np.pi, np.pi / 4)
print(a)
# linspace
# linspace(start,stop,N)
# 产生N个等距分布在[start,stop]间的元素组成的数组,包括start,stop
print(np.linspace(0, 1, 5)) # [ 0. 0.25 0.5 0.75 1. ]
# logspace
# logspace(start, stop, N)
# 产生 N 个对数等距分布的数组,默认以10为底:
print(np.logspace(0, 1, 5))
# 产生的值为$\left[10^0, 10^{0.25},10^{0.5},10^{0.75},10^1\right]$。
# 二维平面中生成一个网格:meshgrid
x_ticks = np.linspace(-1, 1, 5)
y_ticks = np.linspace(-1, 1, 5)
x, y = np.meshgrid(x_ticks, y_ticks)
print(x_ticks)
print(x)
# 图例
import matplotlib.pyplot as plt
from matplotlib import cm
def f(x, y):
# sinc 函数
r = np.sqrt(x ** 2 + y ** 2)
result = np.sin(r) / r
result[r == 0] = 1.0
return result
x_ticks = np.linspace(-10, 10, 51)
y_ticks = np.linspace(-10, 10, 51)
x, y = np.meshgrid(x_ticks, y_ticks, sparse=True)
print(x) # x, y 中有很多冗余的元素,这里提供了一个 sparse 的选项去冗余
z = f(x, y)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z,
rstride=1, cstride=1,
cmap=cm.YlGnBu_r)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()