IPython / matplotlib: return a subtitle from a function

Using Matplotlib in an IPython Notebook, I would like to create a figure with subtitles that return from a function:

import matplotlib.pyplot as plt %matplotlib inline def create_subplot(data): more_data = do_something_on_data() bp = plt.boxplot(more_data) # return boxplot? return bp # make figure with subplots f, (ax1, ax2) = plt.subplots(1, 2, sharey=True, figsize=(10,5)) ax1 -> how can I get the plot from create_subplot() and put it on ax1? ax1 -> how can I get the plot from create_subplot() and put it on ax2? 

I know that I can directly add a graph to the axis:

 ax1.boxplot(data) 

But how can I return a graph from a function and use it as a subtitle?

+7
python matplotlib ipython
source share
1 answer

Usually you do something like this:

 def create_subplot(data, ax=None): if ax is None: 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 do not "return the plot from the function and use it as a subtitle." Instead, you need to plot a square on the axes in the subtitle.

The if ax is None is there, so the transfer in explicit axes is optional (if not, the current piping axes identical to the calling plt.boxplot will be used.). If you prefer, you can leave it and ask for specific axes.

+13
source share

All Articles