I wrote an app to plot a graph from an input file when you drag and drop the file onto a gui interface. However after the execution of plt.show() in the program, the gui interface becomes inactve.
Using plt.draw() instead of plt.show() or using plt.ion() solves this issue, but then the python graph stop updating, ie. resizing/ closing the graph doesn't work until the next time a file is dragged and dropped.
Try to make two different files and put the plt.show() in the other one, to see if it works.
Related
I am using VS Code and python. What happens is that in a program like:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,10)
plt.plot(x, np.sin(x))
plt.show()
plt.plot(x, np.cos(x))
plt.show()
plt.plot(x, np.tan(x))
plt.show()
is that it will stop the exicution of the skript at line 5 and show the plot in a seperate window. It will only continiue to execute the program if I close the plot. Then it will continue until line 7 and stop there.
In other edidors I dont have this behaviour. Like in Spyder. There it will show the polt at the corresponding line, but it will continue the execution of the skript. Is there a way to make the same thing happen in VS Code?
A workaround would be to use plt.figure(). But I am forced to use templates were I can not change the plt.show()'s in the skript. So, I cant use this.
In VSCode, when running and debugging the code, the results will be executed in the same terminal and then output the results, so the above code is executed and output in sequence.
However, according to your description, it is recommended that you use the Jupyter notebook function in VSCode without changing your code. The results will be output in turn and then displayed together.
Use: "Ctrl+Shift+P", "Python: Create Blank New Jupyter Notebook", input the code.
Result:
Reference: Jupyter Notebooks in Visual Studio Code.
If this is not what you want, please describe it in detail and let me know.
Are you looking for this? It turns on "interactive mode". Plotting no longer blocks.
matplotlib.pyplot.ion()
With that, behaviour feels similar to plotting in R, even while in debug mode.
I'm writing an app that allows you to drag and drop datafiles unto a gui which then plots it. However using plt.show() causes the interface to become non-interactive, that means I need to first close the plot before dragging and dropping another file. I wanted to avoid doing this.
I've tried using plt.ion() but doing that causes the plot gui from updating when you resize it or try to manually close it.
I have spent over an hour searching, just to figure this simple thing. So, before considering this a duplicate question, please compare my question to any question out there.
This is my code:
import pandas
import matplotlib.pyplot as plt
dataset = pandas.read_csv('international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=1)
print dataset, type(dataset)
plt.plot(dataset)
plt.show()
plt.close()
Firstly, plt.show() to my understanding is a blocking function. So what is the way to close the figure. There is no point in writing plt.close() after it. So where is the right way to put it.
Secondly, how can I make sure all the windows are closed when I execute a new process of the same python code. For example in MATLAB, one could easily say close all in the beginning of their file and it closes all the opened plots which were the result of previous MATLAB code execution. plt.close('all') is not working either.
I am using PyCharm. The results I found for the first situation, might work on IDLE but not in the PyCharm. How can I do it PyCharm.
plt.show(block=False) will do the trick- This is the way you can make this function non-blocking (both in run & debug mode). The main dis-advantage is that if the code ends, the figure is automatically closes...
I had the same problem.
I fix it making the python file in Pycharm to run in only one console. Go to: run---Edit configuration-- check that "single instance only" is activated
There are two ways to run matplotlib, non-interactive and interactive. In non-interactive mode, the default, you are right that plt.show() is blocking. In this case, calling plt.close() is pointless, the code won't stop as long as the figure is open. In interactive mode, however (which can be triggers by plt.ion()), this code will open then immediately close the figure. You would need to put something to wait for user input if you run code like this in a script. Interactive mode, as the name implies, is designed more for running interactively rather than in a script.
As for closing figures from multiple runs of a python script, this isn't possible. If you open multiple instances of MATLAB, close all in one instance won't close the figures in another instance. Running multiple processes of the same python code is the same as opening multiple instances of MATLAB, one run has no knowledge of the others.
This is highly related to an earlier question by another person a couple of years ago: Matplotlib - Force plot display and then return to main code
I am using Canopy 1.5.5 on MacOSX 10.8.5, with matplotlib 1.4.3.
I will need to load data, look at it, press enter to approve and move to the next dataset (and do that a few thousand times, so it's kind of critical to get this functionality). Here is my MWE:
import numpy as np
from matplotlib import pyplot as plt
plt.ion()
plt.figure()
ind=np.arange(5)
for i in ind:
plt.clf()
plt.scatter(ind,ind+i)
plt.title('this is plot number %i' % i)
plt.show()
u=raw_input("Press any button")
The code seems to do everything EXCEPT actually showing me the plot. If I finish the script (or interrupt it), then I see the current figure.
I have tried everything from the previous answer: with and without interactive mode, with and without plt.show(block=False), every permutation of plt.draw and plt.show, and every backend on my available list.
This seems like a very basic functionality! Please tell me that this can be done. I find it weird that matplolib says here http://matplotlib.org/users/shell.html that "by default the drawing is deferred until the end of the script", but does not have suggestions on how to override the default. Please help!
Your example works for me (my backend is osx), although the figure window appears behind other windows at first. I needed to use alt-tab to raise it to the front.
Try starting your script with the --matplotlib option of IPython. You can select a backend or let it be auto-detected like so: ipython --matplotlib auto yourscript.py
Not sure if you now, but the raw_input function waits for you to press the return key, not just any key.
Edit:
About your last remark: this section explains how to force drawing before the end of the script. This can be done with the draw function. In interactive mode every pyplot command calls draw as well. Drawing in this context means rendering the figure by the backend.
I would like to save my figures to disk without rendering them on the screen and without having to change my rendering backend.
I tried the instructions in here, namely avoiding calling fig.show() nor fig.draw() and just calling fig.savefig, but I noticed that the mere statement fig = plt.figure() already opens a figure on the screen.
How can I save a figure to disk without having to render it, and without having to change my backend?
pyplot has an interactive functionality which will automatically call draw() after most plt.* calls for you.
draw is not automatically called if you don't go through the state machine interface (ex gca().plot(...) would not automatically redraw, but plt.plot(...) would).
See the code, the important function in draw_if_interactive.
This can be turn off via plt.ioff() or by not calling plt.ion() (ipython --pylab automatically turns it on for you).
doc