5

My aim is to create a function that outputs a figure.

If I try to call my function multiple times in a script, it only shows the figure from the first call. Only once I close the first figure, that the program creates the second and so on and so forth.

my function roughly looks like this:

def graphs(...,fig):
...
ax = plt.figure(fig)
...
...
plt.show

For the moment the function doesn't return anything, is there a way I could output the graph or at least make it so that it doesn't stop creating figures after the first one?

chubaz
  • 53
  • 1
  • 3
  • are you plotting something new everytime? or Is it the same graph that is updating its values? – Digvijayad Jul 31 '15 at 13:18
  • Something new everytime. Ideally a new figure everytime the function is called, without closing the ones already created – chubaz Jul 31 '15 at 13:23
  • 1
    http://matplotlib.org/faq/usage_faq.html#coding-styles You should write functions which take in an axes and return the new artists added. – tacaswell Jul 31 '15 at 13:50

1 Answers1

2

Use plt.figure().canvas.draw(). Check this thread: How to update a plot in matplotlib?

Your method then becomes

def graphs(..., fig):
    ax = plt.figure(fig)
    plt.figure().canvas.draw()

Finally, call plt.show() at the end of your loop

for ...:
  graphs(...., arg)
plt.show()

This will plot all figures in multiple windows. Beware that it will create one window per figure. Use subplots if you have huge number of images

Community
  • 1
  • 1
Sudeep Juvekar
  • 4,898
  • 3
  • 29
  • 35