Skip to content Skip to sidebar Skip to footer

Return A Subplot From A Function

Using Matplotlib in an IPython Notebook, I would like to create a figure with subplots which are returned from a function: import matplotlib.pyplot as plt %matplotlib inline def

Solution 1:

Typically, you'd do something like this:

defcreate_subplot(data, ax=None):
    if ax isNone:
        ax = plt.gca()
    more_data = do_something_on_data()  
    bp = ax.boxplot(more_data)
    return bp

# make figure with subplots
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(10,5))
create_subplot(data, ax1)

You don't "return a plot from a function and use it as a subplot". Instead you need to plot the boxplot on the axes in the subplot.

The if ax is None portion is just there so that passing in an explicit axes is optional (if not, the current pyplot axes will be used, identical to calling plt.boxplot.). If you'd prefer, you can leave it out and require that a specific axes be specified.

Post a Comment for "Return A Subplot From A Function"