python matplotlib.pyplot绘图

@[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()

方式1:

在这里插入图片描述方式2:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值