'Ensures that pyplot has been imported into the embedded IPython shell.
Also, makes sure to set the backend appropriately if not set already.'

def ensure_pyplot(self):

    if (not self._pyplot_imported):
        if ('matplotlib.backends' not in sys.modules):
            import matplotlib
            matplotlib.use('agg')
        self.process_input_line('import    matplotlib.pyplot    as    plt', store_history=False)
        self._pyplot_imported = True
