Debugging FuncAnimation - matplotlib

Consider
def animate(k):
# do stuff, update plots
anm = FuncAnimation(myFig, animate, interval=1, repeat=False)
Why is it not possible to debug anything inside animate?
I can run the same code without visualization in barebones Python and perfectly debug it, but somehow FuncAnimation is totally "opaque".
My IDE is Spyder 4.0.1
UPDATE
I am still struggling to debug FuncAnimation.
Does anyone know how to do that?
I can't see what's happening inside animate.

Related

Matplotlib key_press_event never fires in macosx backend

I'm using Matplotlib with the macosx backend. I'm following the example here to hook up a simple event handler:
import matplotlib.pyplot as plt
def _on_keypress(event):
print('Hit on_keypress()')
plt.connect('key_press_event', _on_keypress)
# Call plt.plot() a few times ...
plt.show()
When I run this script from the terminal a figure is shown, but pressing keys shows the pressed key in the terminal, and _on_keypress is never called.
It seems like the plot window is somehow not in focus? Yet I get the same behavior (characters go to terminal) whether I click on the figure or the terminal.
If I open a debugger I can verify the handler is attached to the canvas:
(Pdb) fig = plt.gcf()
(Pdb) fig.canvas.key_press_event('a')
Hit on_keypress()
What is going on?

Pycharm show matplotlib graph inline

I have searched this for long time, similar questions have been asked but none solved my problem.
Usually I run my py files by ctrl+shift+r, and what I expect is to show the graphs in side the session, like the way it works for Jupyter notebook. But instead it either omits the graph, or show it separately and if I have several graphs they will all pop once and lock the session.
Something like %matplotlib inline but for default python files.
So far I have tried to add the env var for Pycharm, and tried to play around with plt.isinterative, nothing worked :(
Thank you.

pydev autocomplete for matplotlib

Here is the code I write in pydev combined with eclipse.
import matplotlib.pyplot as plt
fig=plt.figure()
as I know, 'fig' is a instance of 'matplotlib.Figure' class,when I write :
fig.
it seems pydev can't provide method calltip for fig. I cannot figure out what's going on, since for other module , like numpy, it works well.by the way, if i use a matlab-like interface, for example,
plt.plot()
pydev does provide the calltip for function arguments.
is there a way to solve this problem? I will appreciate it if anyone give a solution .
Forgive my poor english:-D
fig is an instance of matplotlib.figure.Figure so what you can do is importing import matplotlib.figure and creating an instance of that. Then, writing fig to the editor, you should get the tooltip you want.
The following is a screenshot from Spyder, so I haven't actually tested it in pydev.
I am not aware of any other possibility. The reason is that for the requested functionality to work the editor would need to load all kinds of modules, which are not actually imported in the script.
The issue is that some cases are too dynamic for PyDev to know about the actual type of the object that some method returns (which appears to be the case).
If you know the type, you can manually type it locally.
i.e.: Add the comment:
#: :type fig: matplotlib.figure.Figure
right before the fig assignment.
See: http://www.pydev.org/manual_adv_type_hints.html for more details.

Transparent inline matplotlibs in IPython

I'd like the background of my matplotlib plots to be transparent in my IPython notebook. This may sound silly because the notebook itself defaults to a white background but:
1) I use a solarized background and
2) more importantly, I want them to be transparent for when I embed the notebook directly into my blog via nbconvert.
It's easy enough to use something like savefig('file', transparent=True) , but I'm not saving the figures, I am displaying them inline (by calling IPython with ipython notebook --matplotlib inline.
I've been playing around with the IPython notebook configuration file, especially with c.InlineBackend.rc. For example, I upgraded to the dev version of matplotlib to get access to its new savefig.transparent rcParam, and tried configuring that with c.InlineBackend.rc = {'savefig.transparent': True}, but as expected it only affects plots saved with savefig.
Note that I am using the recent IPython 2.0 release. This must be possible somehow, right? Any light that you can shed would be appreciated.
Just to follow up, the issue opened on Github by tillsten has been patched so something like this:
rcParams['figure.facecolor'] = (0,0,0,0)
should work now after you update IPython. Three cheers for open source.
The inline plots are html objects (<img>) with class ui-resizable. So you can change their default behavior by customizing the CSS for your notebooks:
locate your settings for notebooks: in a terminal, type
ipython locate
in the indicated directory, go to subdir profile_default\static\custom (or any profile you want to use instead)
edit or create a CSS file named custom.css
put this in it:
img.ui-resizable
{
opacity:0.4;
}
Close your notebooks, kill IPython and restart it (so that it recreates the served files).
It should work with exported notebooks, as long as you export them as html and you change the css there too.
It's not exactly what you want, but it does the job.

overriding matplotlib's panning tool (wx)

I'm using matplotlib housed in a wxPython panel to do some heavy duty plotting. My issues comes when using native panning tool - it's appears as though matplotlib tries to constantly redraw the canvas as you drag the pan handle around. With the amount of data I'm plotting this is getting really choppy (already optimized with Collections for data etc)
In terms of performance I think it would be much preferable for the canvas to just draw once when the mouse is released at the end of a pan. I realise this will mean I have to extend the WxAgg NavigationToolbar2 class with my own, but I'm wondering if anyone has attempted something similar to this and can advise me on which functions to override?
many thanks
I've spent a lot of time making modification on the matplotlib backends, I've never done this specific change, but I can show you one line of code to comment out that will stop the dynamic updating:
I presume you are using the WxAgg backend, if this is the case, open this file: C:\Python27\Lib\site-packages\matplotlib\backends\backend_wx.py
And comment out the line indicated here:
def dynamic_update(self):
d = self._idle
self._idle = False
if d:
#self.canvas.draw() #<--- Comment out to stop the redrawing during the Pan/Zoom
self._idle = True
I tested this and it seems to nicely solve your issue. I did some quick digging and I didn't see any other functions calling this procedure so you might even be able to just change it to:
def dynamic_update(self):
pass
...Which is the same code you'll find in the base NavigationToolbar2 class
(And of course, if you're happy with this change you can do a little more work to make your own custom backend with this kind of modification. Just to make sure you don't lose the change when upgrading matplotlib)