creating graphs and putting them in a figure afterwards

Hi,

I want to create graphs, fully specify their properties and only then combine them in a single figure. The figure instance holding these graphs is made after creating the graphs: I want to decide afterwards what selection of graphs (subplots) to combine in my final figure.

I have plotting functions for creating graphs. What should they return? An axes instance, figure instance ....? How to combine them in a single figure?

I've tried adding an axes instance to a figure using fig.set_axes()

Bram

Bram,

My usual practice is to have my plotting functions take an axes object and operate on that. So, my code looks something like this:

import matplotlib.pyplot as plt
import numpy as np

def plotfoo(X, Y1, Y2, ax) :
ax.plot(X, Y1)
ax.scatter(X, Y2)

X = np.linspace(1.0, 8.5, 35)
Y1, Y2 = np.random.random((2, 35))

fig = plt.figure()
ax = fig.add_subplot(111)

plotfoo(X, Y1, Y2, ax)

ax.set_title(“FooBar”)
ax.set_ylabel(“Bar”)
ax.set_xlabel(“Foo”)

plt.show()

Therefore, the actual process of creating the plot is separated from any organizational/arrangement aspects and can be easily reused and mixed for other purposes. Imagine having multiple plotting functions and just passing the same axes object into them to have all of them placed on the same plot.

I should note that it is easy to fall into the trap of setting graph labels in the same function where you are plotting. I have found this practice to be detrimental because often times, you want to have a different title or label for different purposes (or change the font size or whatever). It is just easier to keep them separate.

Of course, this isn’t the only style, and I am sure others have different approaches, but this has worked well for me the past couple of years.

I hope this is helpful!
Ben Root

···

On Wed, Oct 27, 2010 at 11:08 AM, Bram Sanders <sanders@…3336…> wrote:

Hi,

I want to create graphs, fully specify their properties and only then

combine them in a single figure. The figure instance holding these

graphs is made after creating the graphs: I want to decide afterwards

what selection of graphs (subplots) to combine in my final figure.

I have plotting functions for creating graphs. What should they return?

An axes instance, figure instance …? How to combine them in a single

figure?

I’ve tried adding an axes instance to a figure using fig.set_axes()

Bram