What is the preferred way to import pylab at a function level in Python 2.7?
Question: I have written a relatively simple function in python that can be used to plot
the time domain history of a data set as well as the frequency domain response
of a data set after a fast fourier transform. In this function I use the
command `from pylab import *` to bring in all the necessary functionality.
However, despite successfully creating the plot, I get a warning stating

> import * only allowed at the module level.

So if using the command `from pylab import *` is not the preferred
methodology, how do I properly load all the necessary functionality from
pylab. The code is attached below. Also, is there a way to close the figure
after the function is exited, I have tried `plt.close()` which is not
recognized for subplots?

    
    
    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
    
        from pylab import *
        from matplotlib import rcParams
        rcParams.update({'figure.autolayout': True})
        Output_Location = Directory.rstrip() + Title.rstrip()
        fig,plt = plt.subplots()
        matplotlib.rc('xtick',labelsize=18)
        matplotlib.rc('ytick',labelsize=18)
        plt.set_xlabel(X_Label,fontsize=18)
        plt.set_ylabel(Y_Label,fontsize=18)
        plt.plot(X_Data,Y_Data,color='red')
        fig.savefig(Output_Location)
        plt.clear()
    


Answer: From the [matplotlib
documentation](http://matplotlib.org/faq/usage_faq.html#matplotlib-pyplot-and-
pylab-how-are-they-related):

> `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()`) you're going to
need to drop that first `fig` because it's not necessary, and you'll 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()
    

