@[1] 读取.npz格式数据
@[2] 绘图方式1:使用的是先创建画布=plt.figure(),在画布上添加子图=fig.add_subplot(),然后在子图上绘图
绘图方式2:创建画布plt.figure();添加子图plt.subplot(2, 2, 2),再plt上绘图
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
if __name__ == '__main__':
# 创建数组
x = [[1, 2, 20, 3], [7, 8, 10, 9]]
a = np.asarray(x)
for i in np.nditer(a):
print(i, '\r')
# 保存成.npz格式
print("保存成.npz格式")
np.savez('text.npz', date=a)
# 加载.npz
print("加载.npz")
b = np.load('text.npz')['date']
print(b)
#一 绘图
fig = plt.figure(num=1, figsize=(8, 8)) # 创建画布,可避免直接用subplot的重叠
fig.suptitle("TITLE PIC")
fig.subplots_adjust(wspace=0.2, hspace=0.5) # 图间距
ax1 = fig.add_subplot(311)
ax1.plot(b, '-o') # c是二维4列数组,数据是 每列上的点 连线;只有y值
ax1.legend([1, 2, 3, 4, 5]) #线图例
ax1.set_ylim(0, 30) #设置刻度范围
ax1.set_xlim((-0.2, 1.5))
ax1.set_xlabel('x-label') #标签
ax1.set_ylabel('y-label')
ax1.set_title('title1') #小表头
ax2 = fig.add_subplot(313)
ax2.plot(b, '--v')
plt.title("title2", loc="center")
ax2.set_title('title2')
# 保存
plt.savefig('title.jpg')
plt.savefig('title.png')
plt.show()
#二 多图
fig1 = plt.figure(num=1, figsize=(8, 8))
fig1.subplots_adjust(wspace=0.2, hspace=0.5) # 图间距
xpoint = np.array([1, 2, 20, 3])
ypoint = np.array([7, 8, 10, 9])
# plot1
plt.subplot(2, 2, 1)
plt.plot(xpoint, ypoint, '-o')
plt.title("plot 1")
# plot2
plt.subplot(2, 2, 2)
plt.plot(b, '-o')
plt.legend([1, 2, 3, 4])
plt.title("plot 2")
# plot3
x = np.linspace(-np.pi, np.pi, 10)
y = np.sin(x)
f = interpolate.interp1d(x, y, kind='cubic')
nx = np.linspace(-np.pi, np.pi, 30)
ny = f(nx)
plt.subplot(2, 2, 3)
plt.plot(x, y, '--')
plt.title("三阶插值")
# plot4
plt.subplot(2, 2, 4)
plt.plot(nx, ny, '.')
plt.title("plot 4")
plt.show()