Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib: generate a new graph in a new window for subsequent program runs

I have a Python program that generates graphs using matplotlib. I am trying to get the program to generate a bunch of plots in one program run (the user is asked if they want to generate another graph) all in separate windows. Any way I can do this?

like image 368
Syd Avatar asked Jul 28 '15 20:07

Syd


People also ask

How do I create a separate plot in matplotlib?

To create multiple plots use matplotlib. pyplot. subplots method which returns the figure along with Axes object or array of Axes object. nrows, ncols attributes of subplots() method determine the number of rows and columns of the subplot grid.

How do you dynamically plot in Python?

To dynamically update plot in Python matplotlib, we can call draw after we updated the plot data. to define the update_line function. In it, we call set_xdata to set the data form the x-axis. And we call set_ydata to do the same for the y-axis.


2 Answers

Using the latest matlibplot, I found the following to work for my purposes:

# create figure (will only create new window if needed)
plt.figure()
# Generate plot1
plt.plot(range(10, 20))
# Show the plot in non-blocking mode
plt.show(block=False)

# create figure (will only create new window if needed)
plt.figure()
# Generate plot2
plt.plot(range(10, 20))
# Show the plot in non-blocking mode
plt.show(block=False)

...

# Finally block main thread until all plots are closed
plt.show()
like image 64
driedler Avatar answered Sep 22 '22 23:09

driedler


To generate a new figure, you can add plt.figure() before any plotting that your program does.

import matplotlib.pyplot as plt
import numpy as np

def make_plot(slope):
    x = np.arange(1,10)
    y = slope*x+3
    plt.figure()
    plt.plot(x,y)

make_plot(2)
make_plot(3)
like image 33
Zach Fox Avatar answered Sep 25 '22 23:09

Zach Fox