![04c489d8754e58cc1d1135c9da660dca.png](https://i-blog.csdnimg.cn/blog_migrate/17732cb1f4f57a5a3118e0f5a2d2b072.jpeg)
Python有很多小操作和包的使用值得记录和积累一下的。
数组增加维度
tmp = np.arange(1, 8, 1)
print(np.shape(tmp))
tmp = tmp[:, np.newaxis]
print(np.shape(tmp))
jupyter 求助函数使用
? np.random.randint
数组作为索引,从数组取对应值
# 一维数组里取
MATRIX = np.arange(1,11,1)
print(MATRIX) # [ 1 2 3 4 5 6 7 8 9 10]
ind = np.random.randint(0,10,15)
print(ind) # [4 5 8 3 1 5 2 2 5 8 5 4 2 9 1]
result = MATRIX[ind]
print(result) # [ 5 6 9 4 2 6 3 3 6 9 6 5 3 10 2]
# 二维
MATRIX = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(MATRIX)
# [[1 2 3]
# [4 5 6]
# [7 8 9]]
ind = np.random.randint(0,3,3)
print(ind) # [0 0 1]
result = MATRIX[ind,range(3)] # 逐列按行取
print(result) # [1 2 6]
result = MATRIX[range(3), ind] # 逐行按列取
print(result) # [1 4 8]
多条线段画图
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10.1, 0.1)
sin_y = np.sin(x)
sin_y2 = sin_y+1
fig = plt.figure(figsize=(9,6))
ax = fig.add_subplot(111)
x1 = x
y1 = sin_y
x2 = x
y2 = sin_y2
l1, = ax.plot(x1, y1, marker='.', label='sin(x)')
l2, = ax.plot(x2, y2, marker='*', label='sin(x) + 1')
ax.legend(loc='lower right')
plt.tick_params(direction='in') # 横纵坐标的标尺向内in向外out
plt.grid() # 网格线
plt.xticks(np.arange(0,11,1)) # 横坐标的值和步长
plt.xlim(left=0, right=10) # 横坐标的最大长度
plt.title('Plot Sample from Frank')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
fig.savefig('filename.tif', bbox_inches='tight') # 保存图片的白边。不设thight,白边会很宽。
![96c1a9d45fd475c7972f2d5c79625365.png](https://i-blog.csdnimg.cn/blog_migrate/6a8c9a5c977e7699876ed5833d0fd78c.png)
![dbc59b5718108bb9d25a97d3a9ed0f72.png](https://i-blog.csdnimg.cn/blog_migrate/c8a276fdbd35e947f2036358c85e6a26.png)