1

I have a main program main.py in which I call various functions with the idea that each function plots something to 1 figure. i.e. all the function plots append detail to the 1 main plot.

Currently I have it set up as, for example:

main.py:

import matplotlib.pylab as plt    

a,b,c = 1,2,3

fig = func1(a,b,c)

d,e,f = 4,5,6

fig = func2(d,e,f)

plt.show()

func1:

def func1(a,b,c):
    import matplotlib.pylab as plt
    ## Do stuff with a,b and c ##
    fig = plt.figure()    
    plt.plot()
    return fig

func2:

def func2(d,e,f):
    import matplotlib.pylab as plt
    ## Do stuff with d,e and f ##
    fig = plt.figure()    
    plt.plot()
    return fig

This approach is halfway there but it plots separate figures for each function instead of overlaying them.

How can I obtain 1 figure with the results of all plots overlaid on top of each other?

4
  • plt.figure() creates a new figure. If you want to plot an existing don't use it in your plotting functions. Commented Feb 25, 2015 at 19:00
  • If I remove plt.figure() from the function and plot directly in the function it still creates a new figure for each function. Commented Feb 25, 2015 at 19:12
  • I don't believe you :) - Note that you are not plotting anything in your functions, so with your example code it's hard to verify if it works. Commented Feb 25, 2015 at 19:16
  • Haha, In the functions there is the line "plt.plot", is that not plotting something? And if that line is not there, how do I create a figure object to send to the main program to plot there? Commented Feb 25, 2015 at 19:19

2 Answers 2

4

It is much better to use the OO interface for this puprose. See http://matplotlib.org/faq/usage_faq.html#coding-styles

import matplotlib.pyplot as plt    

a = [1,2,3]
b = [3,2,1]

def func1(ax, x):
    ax.plot(x)

def func2(ax, x):
    ax.plot(x)

fig, ax = plt.subplots()
func1(ax, a)
func2(ax, b)

It seems silly for simple functions like this, but following this style will make things much much less painful when you want to do something more sophisticated.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that guide was very insightful.
Is it possible/advisable to write a function with which I could just type ax.func1() followed by ax.func2()?
3

This should work. Note that I only create one figure and use the pyplot interface to plot to it without ever explicitly obtaining a reference to the figure object.

import matplotlib.pyplot as plt    

a = [1,2,3]
b = [3,2,1]

def func1(x):
    plt.plot(x)

def func2(x):
    plt.plot(x)

fig = plt.figure()
func1(a)
func2(b)

plot

3 Comments

Please don't use pylab, use import matplotlib.pyplot as plt for state-machine plotting and import numpy as np for the math related code.
@tcaswell, what is the difference between import matplotlib.pyplot as plt and import matplotlib.pylab as plt? For me it seems that pylab does not pull numpy into the namespace.
Doing it this way is better than doing a bulk import, but all of numpy is bulk imported into the pylab namespace. The official position of both mpl and ipython is to discourage/don't really talk about pylab. It would be great to depreciate it, but there are just too many example out there using it to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.