What is the preferred way to import pylab at a function level in Python 2.7?

What is the preferred way to import pylab at a function level in Python 2.7?

From the matplotlib documentation:

pylab is a convenience module that bulk imports matplotlib.pyplot (for plotting) and numpy (for mathematics and working with arrays) in a single name space. Although many examples use pylab, it is no longer recommended.

I would recommend not importing pylab at all, and instead try using

import matplotlib
import matplotlib.pyplot as plt

And then prefixing all of your pyplot functions with plt.

I also noticed that you assign the second return value from plt.subplots() to plt. You should rename that variable to something like fft_plot (for fast fourier transform) to avoid naming conflicts with pyplot.

With regards to your other question (about fig, save fig()) youre going to need to drop that first fig because its not necessary, and youll call savefig() with plt.savefig() because it is a function in the pyplot module. So that line will look like

plt.savefig(Output_Location)

Try something like this:

def Time_Domain_Plot(Directory,Title,X_Label,Y_Label,X_Data,Y_Data):
    # Directory: The path length to the directory where the output file is 
    #            to be stored
    # Title:     The name of the output plot, which should end with .eps or .png
    # X_Label:   The X axis label
    # Y_Label:   The Y axis label
    # X_Data:    X axis data points (usually time at which Yaxis data was acquired
    # Y_Data:    Y axis data points, usually amplitude

    import matplotlib
    from matplotlib import rcParams, pyplot as plt

    rcParams.update({figure.autolayout: True})
    Output_Location = Directory.rstrip() + Title.rstrip()
    fig,fft_plot = plt.subplots()
    matplotlib.rc(xtick,labelsize=18)
    matplotlib.rc(ytick,labelsize=18)
    fft_plot.set_xlabel(X_Label,fontsize=18)
    fft_plot.set_ylabel(Y_Label,fontsize=18)
    plt.plot(X_Data,Y_Data,color=red)
    plt.savefig(Output_Location)
    plt.close()

What is the preferred way to import pylab at a function level in Python 2.7?

Leave a Reply

Your email address will not be published. Required fields are marked *