I have a pyqt4 matplotlib application and I would like the figure to start out (default) to the pan zoom mode when the application opens. I can't find any example or clue from the documentation on how to do this programmatically.
Also along this same line how would I enable the shortcut keys in my application (http://matplotlib.sourceforge.net/users/navigation_toolbar.html)
Thanks for any suggestions.
Colin
I had a similar issue and in my case I solved it by using toolbar.zoom():
import pylab
plt.Figure()
thismanager = get_current_fig_manager()
thismanager.toolbar.zoom()
In that case the figure will appear with the zoom tool already selected
Related
I'm plotting some graphs and then I interact with them using the canvas.mpl_connect commands. So when I show the plot, I want the window to be the active one since the beginning.
At the moment, when the script is run, the plot pops up but it's not the active window, the terminal still is (i.e. if I type a key, that is written in the Terminal, and not interpreted by the plot-window). I need to click on the plot-window to make it the active one and then I'm able to interact with the graphs. It would be way nicer if it was already active the first time it pops up.
Working on MacOSX 10.10.3 with python 2.7.5 and matplotlib 1.3.1
EDIT
I don't just need to bring the window in front, I want to be able to interact with the window without having to click on it. So if I type some keys, the graph will respond and not the terminal.
You can set the order windows are displayed in using canvas manager but it only works with some graphical backends. The following example uses the TkAgg backend which works but the same idea won't work the macosx backend.
import matplotlib
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
from pylab import get_current_fig_manager
fig1 = plt.figure()
fig2 = plt.figure()
fig1.canvas.manager.window.attributes('-topmost', 1)
plt.show()
Figure 1 should show up on top of figure 2.
So, I've finally found a solution here https://github.com/matplotlib/matplotlib/pull/663
Apparently a work around is to import
from AppKit import NSApplication
and issuing just before show():
NSApplication.sharedApplication().activateIgnoringOtherApps_(True)
I'm not really sure what it does, and apparently it works just in non-interactive mode, but that worked perfectly for me.
You might also want to check here
https://github.com/matplotlib/matplotlib/issues/665/
I have noticed that when I run:
import pylab as pl
pl.ion()
# Plot something
pl.show()
pl.close()
The last statement does not fully close the Figure. The figure goes dark, and the contents go away, but the Figure stays on the screen until I exit IPython as shown below
I am using the latest stable version of matplotlib (1.3.1) using an Anaconda distribution, on Linux 64 bit, and I connect remotely using ssh -X.
The backend I am using is below:
backend : QT4Agg
backend.qt4 : PySide
you have to specify wich figure you want to close. In case you want to close all of them:
pl.close('all')
Also, there is a way to just clear but not close a figure:
pl.clf()
Also, seen below from another SO question:
Remember that plt.show() is a blocking function, so in the example code you used above, plt.close() isn't being executed until the window is closed, which makes it redundant.
You can use plt.ion() at the beginning of your code to make it non-blocking, although this has other implications.
You can also use the following lines after your plotting
#Your Plotting function
plt.waitforbuttonpress(0)
plt.close(fig)
The plt.waitforbuttonpress(0) will wait until a user input (Key press) is given. After that it will properly close the matplotlib window properly. It is very important to specify which figure to close.
I'm 100% certain that I have, in the past, seen a matplotlib toolbar with a button which brought up a dialogue allowing the marker styles, line colours etc for the plot to be changed interactively.
This seemed really useful but it's definitely not in the current standard toolbar as documented here and I cannot remember what I was using at the time!
Does anyone have any ideas?!
thanks
As bmu says, it is possible in Spyder (and as far as I know it is so by default). There is an extra button on the toolbar (a green check), as you can see here:
This option must be somewhere in matplotlib. When I do a GUI with matplotlib widget with toolbar, the button for the "figure options" is available. If I just use the plot command, the button is missing.
Edit:
I found http://matplotlib.1069221.n5.nabble.com/How-to-modify-the-navigation-toolbar-easily-in-a-matplotlib-figure-window-td39172.html . If you change your backend in matplotlibrc to Qt4Agg ("backend : Qt4Agg") the button is shown.
Whenever I call show() in matplotlib the plot window appears behind all other windows and I have to minimize everything to see it. Is there any way I can prevent this or programmatically bring it to the front. On OSX Lion. Python 2.7
Well. This answer is presented in the comments to the accepted answer but I think it deserves to have a place as a separate one as it solves the issue nicely. Besides the author's problem I additionally had the problem that matplotlib window wasn't in the tray of active windows so I couldn't switch to it by <Alt>+<Tab>.
Changing of the matplotlib's default backend macosx solved everything. You can try immediately in your code like this:
import matplotlib
matplotlib.use("Qt5agg")
Qt5agg might not be presented in your system then please pick the one that is. You can find them:
import matplotlib.rcsetup as rcsetup
print(rcsetup.all_backends)
If it helped then you might want to change your backend in the configuration. For that locate it first:
import matplotlib
matplotlib.matplotlib_fname()
Then change backend: value:
backend : macosx
to your backend. I used in the end:
backend : TkAgg
Not exactly an answer to your question, but I'm using ipython instead of the default python console. When launched it with ipython --pylab I can plot e.g. by typing
>> plot([1,3,2])
and have the plot pop up in front. It also has some other very nice features ;)
Matplotlib developer's seem to be aware of this issue. But looking into https://github.com/matplotlib/matplotlib/issues/596 it looks like it's going to be a while until getting a solution, apparently because some people find it annoying that figure.show() "steals" screen space.
It may be OS-specific, but using interactive plotting (which plots as soon as you instruct) causes figures to come up in the foreground as soon as they're made on Ubuntu:
import pylab as P
P.ion()
P.figure(1)
P.plot([1,2,3],[1,4,9])
I have the same set-up as nickponline. What works for me is:
from pylab import get_current_fig_manager()
get_current_fig_manager().window.raise_()
If you have multiple figures, this only raises the currently active one. For that case, I found that the following works:
fig1=figure(1)
cfm1=get_current_fig_manager().window
fig2=figure(2)
cfm2=get_current_fig_manager().window
...
cfm1.activateWindow()
cfm1._raise()
pause(.1) # or something else that uses up some time
cfm2.activateWindow()
cfm2.raise_()
I've been using matplotlib in python for some time now and I've finally gotten around to asking this question about an issue on my mac. When a plot shows up (after the plot() command, draw(), or show()), I have all the functionality I could want; I can move, zoom, etc. that I didn't do in the code.
When I go to save a figure with the view as I desire the save as box opens up and prompts for a filename. Anything I type appears in the terminal I used to execute the command! Selecting X11 and then typing has same result. Nothing seems to put the keyboards output into that box, but I can paste into the box using the mouse->Paste action and I can select files in the menu to overwrite and it works fine.
What's up with this?
Update:
The problem was wonderfully outlined and now has some solutions posted in this post: Why doesn't the save button work on a matplotlib plot?
Just installed matplotlib 0.99.1 on Python 2.6.2 on Snow Leopard and ran the following code:
from pylab import *
plot([1,2,3])
show()
Then, I fiddled around with the plot for a while and clicked the save button. The save dialog box popped up normally and allowed me to save (and type) fine. This was using the TkAgg backend. However, I did get this error:
2009-12-08 00:40:18.772 Python[728:60f] -deltaZ is deprecated for NSEventTypeMagnify. Please use -magnification.
Which seems to be something to do with Snow Leopard changing some APIs.
Sorry for using typing this as a post instead of a comment, but code tags aren't allowed in comments :(