I've created an application which after choosing some files and clicking a specific button, it generates 3 to 5 plots using matplotlib (and some uses plotly). For this GUI I used Tkinter.
When I run the .py code it works perfectly, after pressing the button a plot is shown, then after closing its window a new one is displayed and so on until finish.
I tried creating the .exe file for this application using Py2exe and PyInstaller. Both of them have the same behaviour: after pressing the button the first plot is shown, but when I close its window no one is displayed. Only after closing the Tkinter windows created (one main Frame that creates another Frame which has the buttons I click for displaying the plot) the other plots appear (the second one, then after closing its window the third one, and so on).
Any clue about this? I've tried also
import matplotlib
matplotlib.use('TkAgg')
without success. TkAgg is the one that worked for generating the .exe (had to change it in the matplotlibrc).
The code runs the following way: main_app with Tkinter functionalities, second_app with some useful functions and only one Tkinter message (if conditions satisfied) and a bunch of minor .py files that each one is a function with its own calculations and plots.
If a button from the main_app is pressed it calls one specific of these minor .py files (functions) for calculating and generating the plots.
I ran into the same problem. I was using Python 3.4 and PyInstaller 3.3, and I would try to make several plots at once but only one would show until I closed the whole executable (made with PyInstaller's --onefile option). I solved the problem by using
plt.show(block=False)
instead of
plt.show()
when plotting. This did the trick for me, hope it helps you!
Related
I want to show plots in a separate window. Currently I have the IPython graphics backend set to "automatic".
When I re-run the code (or plot another figure), Spyder opens a new plot window. Is it possible to refresh the figure in the window that is already opened instead of opening a new one?
The GUI window that opens when you call plt.show() is bound to a figure. You cannot change the figure inside it. (Well, to be precise, there might be an option of obtaining a handle from the operating system and manipulating its content, but I assume this is not worth the effort.)
Re-running the code actually means that you produce a new figure since the code does not know that it's been run before.
So, exchanging the figure or reusing the window to plot a different figure is not possible.
What is possible however is to use the figure and manipulate the figure while it's open. This is done via plt.ion(). After calling this command in IPython you can adapt the figure, e.g. adding new lines to it etc.
See this example:
At IN [6] the window opens and when IN [7] is executed, the figure stays open and the content changes.
Sure, it is possible with Spyder while in the same running kernel. Try the following example using num as parameter to plt.figure(), where num will always refer to the same figure and refresh it if already opened. Also works with plt.subplots().
import matplotlib.pyplot as plt
from scipy import *
t = linspace(0, 0.1,1000)
w = rand(1)*60*2*pi
fig = plt.figure(num=10, clear=True, figsize = [10,8])
plt.plot(t,cos(w*t))
plt.plot(t,cos(w*t-2*pi/3))
plt.plot(t,cos(w*t-4*pi/3))
I am writing a graphical program in Python for my Raspberry Pi project. I have started writing it using Tkinter and wish to use the Matplotlib tools.
Due to limited screen space and the purpose of the project, I want it to be fullscreen without a window frame and menubar showing. Normally I use the following command:
app.overrideredirect(1)
This works great until I import Matplotlib. Once I do that, the window frame appears again even with the above line of code.
How can I get Matplotlib to not show the window frame or menubars and be completely fullscreen?
Thanks!
I found the problem. The example code I followed involved using the command canvas.show() along with canvas.get_tk_widget().grid(...). The canvas.show() was not needed and caused it to override the app.overrideredirect(1) command.
I am currently working on a project using Python and tkinter.
The problem is that I don't know what's the proper way to display multiple windows, or screens, I don't know how to call them. Let me explain better.
When the application starts the login screen appears. After that, if I click register, I want to go to the register screen, but I don't want it to be a separate window (I don't want to have 2 windows displayed at the same time), but rather another window with different content ?!
How should I handle properly this situation? Create a second window using Toplevel and hiding the first (can I do that?) or changing the widgets of the first?
Code I've written so far
You can do that- just call window.withdraw() on the Toplevel you need to hide after creating a new Toplevel. Changing the widgets in the first is also an option- if you like, you could always try a Notebook widget and disable manual flipping or just put each "screen" in a frame and grid_ or pack_forget them to remove them from the window.
I've been using the IEP from pyzo before trying out Sublime Text (ST).
There is an annoying behaviour with ST that IEP doesn't have.
In IEP, much like with Matlab or Octave, the editor and the interactive console talk to each other.
Typically if you compute some_stuff and plot it in a script, after execution of this script you can go to the console and check some values:
print some_stuff[0:10]
or modify your plot:
plt.whatever()
which will update your figure.
Also if you run your script several times with different parameters, the figure is simply updated.
However when you do so in ST, even with REPL, after execution of the script nothing is left, you can't access some_stuff[0:10] from REPL. Similarly, you can't modify your figure. And if you run your script several times with different parameters, a new figure is generated in a new window each time instead of updating the existing figure.
Is there an easy work around this? Thanks!
How about saving your figure to a file with plt.savefig("fig.png")? If you open that file with your image viewer it will be updated after running your program.
I"m running into issues when trying to interactively make plots with matplotlib.pyplot.
The general sequence of events is as follows:
1) open terminal, type ipython, copy and paste commands from my sublime text into ipython, eventually typing plt.show().
2) viewing my nice pretty plot be made, but with one small issue I want to fix or build upon
3) closing the window that houses the plot
4) seeing the command line return to normal again. Typing plt.close(), just to make sure the plot really closed
5) copying and pasting commands again, again ending with plt.show().
Only, the result of #5 results in no plot this time!
This seems to be a problem that repeats itself, so if anyone has any input I'd be very grateful. I noticed that when I create the plot the first time, it creates a plot window (which I can see with Command-Tab Application Switcher). When I try to close this after making the plot the first time, this window never actually disappears from the Application Switcher.
Thanks