Naked Science Forum

Non Life Sciences => Geek Speak => Topic started by: Eternal Student on 29/10/2021 01:45:26

Title: How do I open a new figure window in Python?
Post by: Eternal Student on 29/10/2021 01:45:26
Hi.

   The question is in the title:
I've been asked to add a command that will open a new figure window in the Python programming language.

I think this will do it:

plt.figure()          # matplotlib.pyplot  has already been imported as plt

However, I'm finding loads of references that suggest every figure should be numbered, so I would need something like this:         plt.figure(1)
Which is just a nuisance because I would have to check through all the previous code to check that figure(1) hasn't already been used for something else and might still be left open and could be messed with.
I'm using a Jupyter notebook environment to run this code and so I don't really ever need to open a new winodw for figures and I've never bothered with it before.  However, this piece of code apparently must run in some other environments that aren't the Jupyter notebook,  so  I'm just looking for something that will make the system aware that all the commands that follow are to be implemented in a new figure window.

Best Wishes.
Title: Re: How do I open a new figure window in Python?
Post by: nicephotog on 30/10/2021 11:36:55
That's a whole math framework, try stackoverflow.com , in the while, this does its math apparently according the narrator.
Title: Re: How do I open a new figure window in Python?
Post by: Iannguyen on 20/01/2022 09:18:23
Opening a new figure window in python will require you to follow some programs. Although we can do it in different ways, I want to forward two different ways that I have got from different programmers. Make sure you try them and if you can find or get your hands on a better one, you can try that one too!
If you want to plot in a specific figure number your figure using fig1 = plt.figure(1), fig2 = plt.figure(2) etc. To plot a graph in a specific figure define axes ax1 = fig1.gca() gca = get current axis and instead of using plt.plot() use ax1.plot() to plot in the figure 1
Program 1:
import matplotlib.pyplot as plt

x1 = [0,1]
x2 = [0,2]

y1 = [0,1]
y2 = [0,-1]

fig1 = plt.figure(1)
ax1 = fig1.gca()

fig2 = plt.figure(2)
ax2 = fig2.gca()


ax1.plot(x1,y1,'b')
ax2.plot(x2,y2,'r')

plt.show()

If you want to create 5 figures use lists :
fig = []
ax = []
for i in range(5) :
    fig.append(plt.figure(i))
    ax.append(fig.gca())
if the figure 1 is already opened and you want to plot an additional curve you just have to type these lines :
fig3 = plt.figure(1)
ax3 = fig1.gca()
ax3.plot(x1,y2,'g')
fig3.canvas.draw()

Program 2:
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)
Title: Re: How do I open a new figure window in Python?
Post by: Eternal Student on 20/01/2022 15:39:48
Hi and thanks.

@lannguyen
    I went with the the idea of what you are calling program 2 in then end.  It worked.

It was a long time ago that I was doing this.  Hence, I'm not really following this thread anymore.  People are welcome to continue writing or doing something with this thread if they wish but I may not notice or reply.
   Thanks for your time.

Best Wishes.
Title: Re: How do I open a new figure window in Python?
Post by: Iannguyen on 21/01/2022 12:03:07
Hi and thanks.

@lannguyen
    I went with the the idea of what you are calling program 2 in then end.  It worked.

It was a long time ago that I was doing this.  Hence, I'm not really following this thread anymore.  People are welcome to continue writing or doing something with this thread if they wish but I may not notice or reply.
   Thanks for your time.

Best Wishes.

Hello,
Thank you so much for the appreciation.