new plot window plot every time I run my script - python

I'm running a simple script in ipython, and no problem to get the plot and update the figure every time I run it
plt.clf()
plt.plot(x,y,'bo')
plt.show()
However, if I try to plot multiple panels
fig, axs = plt.subplots(1,3)
axs[0].plot(x,y,'bo')
axs[1].plot(x,z,'bo')
axs[2].plot(y,z,'bo')
plt.show()
a new window with the three panels is created every time I run my scrip (unless I exit and restart the ipython session). What shall I do? Thanks!

subplots creates a new figure by default, but you can also specify which figure to plot into, if you do so then it will only open that one figure / window:
fig, axs = plt.subplots(1,3, num=1) # force to plot into figure 1

Related

Radar plot labels

I have fairly basic Python knowledge but I am trying to use it to plot a radar graph. I have done this before but now my script is causing an issue.
I don't need any data to demonstrate so I won't add it, my script draws a radar plot, and plots the data but when I add variable labels for some reason it zooms in on one quadrant of the radar plot.
fig = plt.figure()
plt.clf()
ax = fig.add_subplot(1, 1, 1, projection='radar')
ax.set_varlabels(labels)
Any ideas why and how to fix this, it used to work so I think there must have been an update and maybe I need a different command? I am using Python 3.6.4 via Anaconda.

Inscribing Plot window in the console and do not want the plot window popping up separately

I am facing couple of issues. First, I wanted all the plots in a separate window. For this, I successfully changed the settings and I got the separate window. The problem is, I got all the plots in same figures, which is bad. Second issue is, how do I inscribe window pan to the Ipconsole? I donot want a separate window. I want this window inside the console?
For the first issue, you can have your plots in different figures by using figure this way:
import matplotlib.pyplot as plt
plt.figure()
# Plot your first graph(s)
plt.figure()
# Plot your other graph(s)
plt.show()
Each time you call figure, a new window is created. For more information on figure, you can check the doc

Python: close the figure window and let the program go

I have a python program, say, train.py. It can be run in anaconda prompt by typing:
python train.py
In train.py, some part is written for drawing and saving figures:
import matplotlib.pyplot as plt
....... #I omit these codes
plt.savefig(....)
plt.show() #this will produce a figure window
plt.close()
In implementing the program, some figures are to be generated, which will bring the program to a temporary halt, even though plt.close() present. Then I need to manually close the pop-up figure window due to plt.show() and continue the program. How to avoid this inconvenience.
It is noted that spyder can run the program continuously, with figures displayed in its console.
plt.show() is meant to be used once in a script to show all figures present. So you can create your figures and show them all at the end
fig1 = plt.figure(1)
# do something with figure 1
fig2 = plt.figure(2)
# do something with figure 2
plt.show() # show both figures

multiple graph (not subplot) using python and matplotlib

I would like to plot two or more graphs at once using python and matplotlib. I do not want to use subplot since it is actually two or more plots on the same drawing paper.
Is there any way to do it?
You can use multiple figures and plot some data in each of them. The easiest way of doing so is to call plt.figure() and use the pyplot statemachine.
import matplotlib.pyplot as plt
plt.figure() # creates a figure
plt.plot([1,2,3])
plt.figure() # creates a new figure
plt.plot([3,2,1])
plt.show() # opens a window for each of the figures
If for whatever reason after creating a second figure you want to plot to the first one, you need to 'activate' it via
plt.figure(1)
plt.plot([2,3,1]) # this is plotted to the first figure.
(Figure numbers start at 1)

Empty python plot

I'm running a script remotely on a cluster to generate a scatter plot. I wish to save the plot, but I don't want the plot to be display or a window to come up (as when you execute plt.show() ).
My saved plots are always empty. This is the code that I'm using (below). Any tips would be very helpful. Thanks!
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlim([-1,maxX+1])
ax.set_ylim([0,maxY+1])
ax.set_xlabel('Comparison number (n)', fontsize=18, fontweight='bold')
ax.set_ylabel('Normalized cross correlation score', fontsize=18, fontweight='bold')
ax.scatter(xaxis,yaxis)
plt.savefig('testfig.png')
In order to use avoid showing plot windows (i.e. to do off-screen rendering) you probably want to use a different matplotlib backend.
Before any matplotlib import statements, add
import matplotlib
matplotlib.use('Agg')
and subsequent calls to matplotlib will not show any plot windows.
If your plot file shows an empty axis, then the problem lies in the plotting arguments as calling plot with empty arguments creates an empty axis.

Categories