Skip to content

Matplotlib

Example Template

  • Setting for Jupyter Notebook

    import numpy as np 
    
    # Ploting Configuration
    import matplotlib.pyplot as plt  
    import matplotlib.ticker as ticker
    
    #-----------------------------------------------
    # Reference:
    # https://matplotlib.org/stable/tutorials/introductory/customizing.html
    #-----------------------------------------------
    
    # For axes (outer box)
    plt.rc('axes', labelsize='medium')  # fontsize of the x any y labels
    plt.rc('axes', linewidth=0.5) 
    plt.rc('axes', labelpad=4) 
    # For ticks and grid
    plt.rc('xtick.major', width=0.5, pad=7)
    plt.rc('ytick.major', width=0.5, pad=4)
    plt.rc('xtick.minor', width=0.5)
    plt.rc('ytick.minor', width=0.5)
    plt.rc('xtick', labelsize='small', direction='in', top='True')
    plt.rc('ytick', labelsize='small', direction='in', right='True')
    plt.rc('grid', linewidth=0.5, color='0.9') 
    # Text and others
    plt.rc('text', usetex=True)     # Latex support
    plt.rc('font', family='serif')
    plt.rc('lines', linewidth=0.5)  # Linewidth of data
    plt.rc('savefig', dpi=300)
    
    # Setting for Jupyter Notebook
    %matplotlib inline
    %config InlineBackend.figure_format = 'retina'
    

  • Ploting

    # Functions for example plot
    def db(x):
        return 20.*np.log10(x)
    def model(f):
        return 10./(f-1.j*10)
    
    # figsize=(x, y), change x and y by yourself!!!
    fig = plt.figure(figsize=(5, 3.5)) 
    
    
    # Magnitude
    plt.subplot(2, 1, 1)    # subplot(nrows, ncols, index)
    plt.title("Low Pass Filter")
    f = np.logspace(0, 3, 1000)
    plt.semilogx(f, db(np.abs(model(f))), "b", label="LFP")
    
    plt.xlim(1e0, 1e3)
    plt.ylabel('Magnitude(dB)')
    plt.grid(True, which='major')
    plt.grid(True, which='minor', linestyle=':')
    plt.legend(loc=1, fontsize=8, fancybox=False)
    
    
    # Phase
    plt.subplot(2, 1, 2)
    plt.semilogx(f, np.angle(model(f), deg=True), "b", label="LPF")
    
    plt.xlim(1e0, 1e3)
    plt.ylim(0, 90)
    plt.xlabel('Frequency (Hz)')
    plt.ylabel('Phase (deg) ')
    plt.grid(True, which='major')
    plt.grid(True, which='minor', linestyle=':')
    plt.legend(loc=1, fontsize=8, fancybox=False)
    
    
    plt.tight_layout()
    fig.savefig('plot.pdf')
    fig.savefig('plot@2x.png')