The Naked Scientists
  • Login
  • Register
  • Podcasts
      • The Naked Scientists
      • eLife
      • Naked Genetics
      • Naked Astronomy
      • In short
      • Naked Neuroscience
      • Ask! The Naked Scientists
      • Question of the Week
      • Archive
      • Video
      • SUBSCRIBE to our Podcasts
  • Articles
      • Science News
      • Features
      • Interviews
      • Answers to Science Questions
  • Get Naked
      • Donate
      • Do an Experiment
      • Science Forum
      • Ask a Question
  • About
      • Meet the team
      • Our Sponsors
      • Site Map
      • Contact us

User menu

  • Login
  • Register
  • Home
  • Help
  • Search
  • Tags
  • Member Map
  • Recent Topics
  • Login
  • Register
  1. Naked Science Forum
  2. Non Life Sciences
  3. Geek Speak
  4. How do I open a new figure window in Python?
« previous next »
  • Print
Pages: [1]   Go Down

How do I open a new figure window in Python?

  • 4 Replies
  • 7079 Views
  • 2 Tags

0 Members and 1 Guest are viewing this topic.

Offline Eternal Student (OP)

  • Naked Science Forum King!
  • ******
  • 1290
  • Activity:
    17%
  • Thanked: 287 times
    • View Profile
How do I open a new figure window in Python?
« 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.
Logged
 



Offline nicephotog

  • Sr. Member
  • ****
  • 457
  • Activity:
    0%
  • Thanked: 11 times
  • [ censored ]
    • View Profile
Re: How do I open a new figure window in Python?
« Reply #1 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.
Logged
How To Tutorial (all Java servers) HttpOutPutTools (port to .jar Hell Pig Entelodont) 2nd November 2022
https://drive.google.com/file/d/1gjHmdC-BW0Q2vXiQYmp1rzPU497sybNy/view?usp=share_link
 
The following users thanked this post: Zer0

Offline Iannguyen

  • Jr. Member
  • **
  • 17
  • Activity:
    0%
  • Thanked: 1 times
  • Naked Science Forum Newbie
    • View Profile
Re: How do I open a new figure window in Python?
« Reply #2 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)
Logged
 
The following users thanked this post: Eternal Student

Offline Eternal Student (OP)

  • Naked Science Forum King!
  • ******
  • 1290
  • Activity:
    17%
  • Thanked: 287 times
    • View Profile
Re: How do I open a new figure window in Python?
« Reply #3 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.
Logged
 

Offline Iannguyen

  • Jr. Member
  • **
  • 17
  • Activity:
    0%
  • Thanked: 1 times
  • Naked Science Forum Newbie
    • View Profile
Re: How do I open a new figure window in Python?
« Reply #4 on: 21/01/2022 12:03:07 »
Quote from: 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.

Hello,
Thank you so much for the appreciation.
Logged
 



  • Print
Pages: [1]   Go Up
« previous next »
Tags: python  / pythone 
 

Similar topics (5)

Can open science website help science?

Started by Martin J SallbergBoard General Science

Replies: 5
Views: 3841
Last post 14/10/2016 19:44:41
by Martin J Sallberg
If you open the door on a well lit room does the room become darker?

Started by John ChapmanBoard Physics, Astronomy & Cosmology

Replies: 7
Views: 7091
Last post 16/02/2009 04:12:26
by Chemistry4me
How does the transcription bubble remain open during the transcription process?

Started by khurrumBoard Cells, Microbes & Viruses

Replies: 2
Views: 5908
Last post 13/05/2009 19:48:15
by khurrum
What uses more energy, leaving the fridge door open for 15 seconds or opening and closing it twice?

Started by thedocBoard Physics, Astronomy & Cosmology

Replies: 0
Views: 2415
Last post 03/06/2016 03:50:01
by thedoc
What uses more energy, leaving the fridge door open for 15 seconds or opening and closing it twice?

Started by thedocBoard Technology

Replies: 2
Views: 4327
Last post 09/07/2016 15:06:34
by wolfekeeper
There was an error while thanking
Thanking...
  • SMF 2.0.15 | SMF © 2017, Simple Machines
    Privacy Policy
    SMFAds for Free Forums
  • Naked Science Forum ©

Page created in 0.076 seconds with 43 queries.

  • Podcasts
  • Articles
  • Get Naked
  • About
  • Contact us
  • Advertise
  • Privacy Policy
  • Subscribe to newsletter
  • We love feedback

Follow us

cambridge_logo_footer.png

©The Naked Scientists® 2000–2017 | The Naked Scientists® and Naked Science® are registered trademarks created by Dr Chris Smith. Information presented on this website is the opinion of the individual contributors and does not reflect the general views of the administrators, editors, moderators, sponsors, Cambridge University or the public at large.