自定义的画多图函数mysubplot.py和画傅里叶频谱函数fftandshow.py

仅作为存储,大佬请跳过。

mysubplot.py

包含四个函数,第一个是只画纵轴值的图,第二个是横纵轴值都画上;
(第三个函数是只画纵轴值的画成两列的多图(并标注序号标题),第四个是横纵轴值都画上的画成两列的多图(并标注序号标题)):

import matplotlib.pyplot as plt

def mysubplot(file):
    l=len(file)

    if l==2:
        plt.figure()
        plt.subplot(211);plt.plot(file[0])
        plt.subplot(212);plt.plot(file[1])

    if l==3:
        plt.figure()
        plt.subplot(311);plt.plot(file[0])
        plt.subplot(312);plt.plot(file[1])
        plt.subplot(313);plt.plot(file[2])

    if l==4:
        plt.figure()
        plt.subplot(411);plt.plot(file[0])
        plt.subplot(412);plt.plot(file[1])
        plt.subplot(413);plt.plot(file[2])
        plt.subplot(414);plt.plot(file[3])

    if l==6:
        plt.figure()
        plt.subplot(611);plt.plot(file[0])
        plt.subplot(612);plt.plot(file[1])
        plt.subplot(613);plt.plot(file[2])
        plt.subplot(614);plt.plot(file[3])
        plt.subplot(615);plt.plot(file[4])
        plt.subplot(616);plt.plot(file[5])

    if l==8:
        plt.figure()
        plt.subplot(811);plt.plot(file[0])
        plt.subplot(812);plt.plot(file[1])
        plt.subplot(813);plt.plot(file[2])
        plt.subplot(814);plt.plot(file[3])
        plt.subplot(815);plt.plot(file[4])
        plt.subplot(816);plt.plot(file[5])
        plt.subplot(817);plt.plot(file[6])
        plt.subplot(818);plt.plot(file[7])

    plt.show()

def mysubplot_addx(file):
    l=len(file)

    if l==2:
        plt.figure()
        plt.subplot(211);plt.plot(file[0][0],file[0][1])
        plt.subplot(212);plt.plot(file[1][0],file[1][1])

    if l==3:
        plt.figure()
        plt.subplot(311);plt.plot(file[0][0],file[0][1])
        plt.subplot(312);plt.plot(file[1][0],file[1][1])
        plt.subplot(313);plt.plot(file[2][0],file[2][1])

    if l==4:
        plt.figure()
        plt.subplot(411);plt.plot(file[0][0],file[0][1])
        plt.subplot(412);plt.plot(file[1][0],file[1][1])
        plt.subplot(413);plt.plot(file[2][0],file[2][1])
        plt.subplot(414);plt.plot(file[3][0],file[3][1])

    if l==6:
        plt.figure()
        plt.subplot(611);plt.plot(file[0][0],file[0][1])
        plt.subplot(612);plt.plot(file[1][0],file[1][1])
        plt.subplot(613);plt.plot(file[2][0],file[2][1])
        plt.subplot(614);plt.plot(file[3][0],file[3][1])
        plt.subplot(615);plt.plot(file[4][0],file[4][1])
        plt.subplot(616);plt.plot(file[5][0],file[5][1])

    if l==8:
        plt.figure()
        plt.subplot(811);plt.plot(file[0][0],file[0][1])
        plt.subplot(812);plt.plot(file[1][0],file[1][1])
        plt.subplot(813);plt.plot(file[2][0],file[2][1])
        plt.subplot(814);plt.plot(file[3][0],file[3][1])
        plt.subplot(815);plt.plot(file[4][0],file[4][1])
        plt.subplot(816);plt.plot(file[5][0],file[5][1])
        plt.subplot(817);plt.plot(file[6][0],file[6][1])
        plt.subplot(818);plt.plot(file[7][0],file[7][1])

    plt.show()



def mysubplot_row(file):
    l=len(file)

    if l==2:
        plt.figure()
        plt.subplot(211);plt.plot(file[0])
        plt.subplot(212);plt.plot(file[1])

    if l==3:
        plt.figure()
        plt.subplot(311);plt.plot(file[0])
        plt.subplot(312);plt.plot(file[1])
        plt.subplot(313);plt.plot(file[2])

    if l==4:
        plt.figure()
        plt.subplot(221);plt.plot(file[0]);plt.title('1')
        plt.subplot(222);plt.plot(file[1]);plt.title('2')
        plt.subplot(223);plt.plot(file[2]);plt.title('3')
        plt.subplot(224);plt.plot(file[3]);plt.title('4')

    if l==6:
        plt.figure()
        plt.subplot(321);plt.plot(file[0]);plt.title('1')
        plt.subplot(322);plt.plot(file[1]);plt.title('2')
        plt.subplot(323);plt.plot(file[2]);plt.title('3')
        plt.subplot(324);plt.plot(file[3]);plt.title('4')
        plt.subplot(325);plt.plot(file[4]);plt.title('5')
        plt.subplot(326);plt.plot(file[5]);plt.title('6')

    if l==8:
        plt.figure()
        plt.subplot(421);plt.plot(file[0]);plt.title('1')
        plt.subplot(422);plt.plot(file[1]);plt.title('2')
        plt.subplot(423);plt.plot(file[2]);plt.title('3')
        plt.subplot(424);plt.plot(file[3]);plt.title('4')
        plt.subplot(425);plt.plot(file[4]);plt.title('5')
        plt.subplot(426);plt.plot(file[5]);plt.title('6')
        plt.subplot(427);plt.plot(file[6]);plt.title('7')
        plt.subplot(428);plt.plot(file[7]);plt.title('8')

    plt.show()

def mysubplot_addx_row(file):
    l=len(file)

    if l==2:
        plt.figure()
        plt.subplot(211);plt.plot(file[0][0],file[0][1])
        plt.subplot(212);plt.plot(file[1][0],file[1][1])

    if l==3:
        plt.figure()
        plt.subplot(311);plt.plot(file[0][0],file[0][1])
        plt.subplot(312);plt.plot(file[1][0],file[1][1])
        plt.subplot(313);plt.plot(file[2][0],file[2][1])

    if l==4:
        plt.figure()
        plt.subplot(221);plt.plot(file[0][0],file[0][1]);plt.title('1')
        plt.subplot(222);plt.plot(file[1][0],file[1][1]);plt.title('2')
        plt.subplot(223);plt.plot(file[2][0],file[2][1]);plt.title('3')
        plt.subplot(224);plt.plot(file[3][0],file[3][1]);plt.title('4')

    if l==6:
        plt.figure()
        plt.subplot(321);plt.plot(file[0][0],file[0][1]);plt.title('1')
        plt.subplot(322);plt.plot(file[1][0],file[1][1]);plt.title('2')
        plt.subplot(323);plt.plot(file[2][0],file[2][1]);plt.title('3')
        plt.subplot(324);plt.plot(file[3][0],file[3][1]);plt.title('4')
        plt.subplot(325);plt.plot(file[4][0],file[4][1]);plt.title('5')
        plt.subplot(326);plt.plot(file[5][0],file[5][1]);plt.title('6')

    if l==8:
        plt.figure()
        plt.subplot(421);plt.plot(file[0][0],file[0][1]);plt.title('1')
        plt.subplot(422);plt.plot(file[1][0],file[1][1]);plt.title('2')
        plt.subplot(423);plt.plot(file[2][0],file[2][1]);plt.title('3')
        plt.subplot(424);plt.plot(file[3][0],file[3][1]);plt.title('4')
        plt.subplot(425);plt.plot(file[4][0],file[4][1]);plt.title('5')
        plt.subplot(426);plt.plot(file[5][0],file[5][1]);plt.title('6')
        plt.subplot(427);plt.plot(file[6][0],file[6][1]);plt.title('7')
        plt.subplot(428);plt.plot(file[7][0],file[7][1]);plt.title('8')

    plt.show()




fftandshow.py

包含两个函数,第一个是直接画出频谱,第二个是求出频率和幅度的值-用于后续在一个图里画多个频谱:

import numpy as np
import matplotlib.pyplot as plt

def fftspectrum(thesignal,fs):  # add fs
    nsel=thesignal.size
    fsel=fs*(np.arange(0,nsel/2)/nsel)  # add fs*
    ysel=np.fft.fft(thesignal)
    ysel=np.abs(ysel)
    ysel=ysel[0:len(fsel)]
    # ysel=20*np.log(ysel)
    # plt.figure()
    plt.plot(fsel,ysel)
    # plt.show()

# fs=200
# t=np.arange(0,1,1/fs)
# sig=np.cos(2*np.pi*40*t)
# plt.figure()    # function needs
# fftspectrum(sig,fs)
# plt.show() # function needs

def fftspectrum_value(thesignal,fs):  # add fs
    nsel=thesignal.size
    fsel=fs*(np.arange(0,nsel/2)/nsel)  # add fs*
    ysel=np.fft.fft(thesignal)
    ysel=np.abs(ysel)
    ysel=ysel[0:len(fsel)]
    # ysel=20*np.log(ysel)
    # plt.figure()
    # plt.plot(fsel,ysel)
    # plt.show()
    return fsel,ysel

使用

在这里插入图片描述

实例

做出心电图及其频谱

import scipy.io as sio
# from matplotlib.pyplot import *
import numpy as np
import matplotlib.pyplot as plt
import pywt
from scipy import signal
import mysubplot
import fftandshow

mat_contents = sio.loadmat(r'E:\2013mat\a05.mat')

tm=mat_contents['tm']
fs=mat_contents['Fs']
sig=mat_contents['signal']

# sig_ecg=sig[:,0]

mysubplot.mysubplot([sig[:,0],sig[:,1],sig[:,2],sig[:,3]])

mysubplot.mysubplot_addx([[tm,sig[:,0]],[tm,sig[:,1]],[tm,sig[:,2]],[tm,sig[:,3]]])

mysubplot.mysubplot_row([sig[:,0],sig[:,1],sig[:,2],sig[:,3]])

mysubplot.mysubplot_addx_row([[tm,sig[:,0]],[tm,sig[:,1]],[tm,sig[:,2]],[tm,sig[:,3]]])


fftandshow.fftspectrum(sig[:,0],fs[0]);plt.show()

f1,y1=fftandshow.fftspectrum_value(sig[:,0],fs[0]);f2,y2=fftandshow.fftspectrum_value(sig[:,1],fs[0])
f3,y3=fftandshow.fftspectrum_value(sig[:,2],fs[0]);f4,y4=fftandshow.fftspectrum_value(sig[:,3],fs[0])
mysubplot.mysubplot_addx([[f1,y1],[f2,y2],[f3,y3],[f4,y4]]);plt.show()
mysubplot.mysubplot_addx_row([[f1,y1],[f2,y2],[f3,y3],[f4,y4]]);plt.show()



在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41529093/article/details/113185568