1
Geek Speak / Re: How do I open a new figure window in Python?
« 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)
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)
The following users thanked this post: Eternal Student