Example matplotlib.org does not work in Canopy - python

I am trying to make an animation with matplotlib in python (2.7) with 3 subplots. To see how this works I tried to understand the example on matplolib.org:
http://matplotlib.org/examples/animation/subplots.html
However when I run this program I only see one of the three plots displayed. Sometimes its the upper right one, sometimes the lower right one.
In my own code I also only see one of the three displayed. Does anyone know if this is a problem with Enthought Canopy and how to fix this?.

Solved, if you set in canopy: preferences > python > PyLab backend: interactive (wx), for some reason it works.

Related

Issues with too many interactive plotly figures

I am using Jupyter notebook on my laptop (the version coming with Anaconda) to perform some sensitivity analysis.
I use plotly to display the results and I like the interactive features that it has.
However, when I am trying to display more than 7/8 interactive plots on the same notebook, some plots disappears and the output cells of those plots go crazy (see picture attached).
Issue with plotly
A solution I found was to disable the interactive feature at least for some of the plots, changing the diplay mode in config as:
config = {'staticPlot': True}
fig.show(config=config)
This method works, however, I like the feature and I was wondering if there was a solution that does not imply disabling the interactive view.
I read about this post where they say it might be a memory issue (even though their graphs are going blank while mine are behaving crazy):
Plotly: How to prevent graphs from going blank when there are too many interactive plots?
However I did not manage to find/change the jupyter configuration file, maybe because I installed it via Anaconda?
I was also wondering if someone experienced exactly the same or there might be a simpler solution to this issue.
Thanks a lot in advance
I believe that in the second link the config file should be generated if not existing.
You can also try changing to gl rendering:
https://plotly.com/python/webgl-vs-svg/

Strange autocomplete suggestions in IPython Shell

I use the IPython shell fairly often and have just started to notice it giving me strange autocomplete suggestions without any prompting from me. In this example, I just typed "im" and it suggests importing matplotlib?
This is very strange for several reasons: I've never seen this kind of grayed out code suggestion before that appears just as I type without the need to press tab or anything like that, the suggestions seem to be very arbitrary (why would typing im mean I want to import matplotlib of all things) and sometimes the suggestions make so sense (image 2: it just asks me to run plt.show() even though I haven't plotted anything yet).
Any clues to what could be going on here?
Try this.
import IPython
terminal = IPython.get_ipython()
terminal.pt_app.auto_suggest = None
https://github.com/ipython/ipython/issues/13451

Figure is not properly displayed whiile using %matplotlib notebook in Jupyter Notebook

As you can see on the image posted below, for some reason the notebook doesn't want to properly display a figure while using the interactive matplotlib interface - the %matplotlib notebook and it only prints the top left corner of the wanted plot. I've browsed the Internet, mostly this forum but I haven't found solution either, so I've got no other choice than to make a post about it.
If I switch it back to classic %matplotlib inline, I've got no issues and the figure is displayed correctly. I'd really appreciate any help you can offer, it drives me crazy because I need to properly display these interactive plots, in order to learn from other notebooks which use them. So I'm not searching for any other alternatives to using interactive figures, but I'd preferably want this particular issue to be resolved.
Thank you.
This was rather a quick solution to my suprise. I was even thinking of pulling this question off the site, since I resolved this quite promptly, but then again you never know, someone might end up with the very same problem as I encountered before.
So anyway, changing the browser does the trick. At least in my case changing from Chrome to Firefox solved this issue of mine.

reliably show matplotlib (0.99 to 1.3.1) figures without blocking

Is there any way to show a pyplot figure in Python 2.65, Matplotlib 0.99, without locking everything else?
I have a program with a Pmw GUI running on Python 2.75 with Matplotlib 1.3.1. on Windows (64-bit Winpython).
Everytime a figure is drawn (or everytime something is added to an existing one), the routine calls plt.show().
It is possible to show pyplot figures while allowing the user to keep using the GUI and manipulate the figures, nothing blocks anything, as I want it to be since I need the user to look at the plots when deciding what to do next in the GUI, and to have several plots next to each other for comparison.
Now, I need to make all of this work on a system with Python 2.65 and Matplotlib 0.99. The behaviour now seems to be that nothing is visible unless plt.show() is called, and then it will block everything until the plot window is closed.
I tried using plt.draw() instead but then the figure does not even show up.
Worse: Some routines who draw into existing figures never get a chance because they can't execute while the figure is being shown, and when they can, the figure is gone already. I cannot wait until the last drawing operation because the graphs are meant to build up incremental, adding information to existing graphs which the user needs to be able to see.
I am not allowed to update matplotlib. I am allowed to use non-binary parts of libraries if I make them part of "my" software package.
Ideally, I am looking for code that will work in both environments I'm working in, but at this point I am willing to make compromises...
Update:
I have found some code running in the same environment that is able to do all of these things, where I can remove the plt.show() calls, even run it in IDLE (which according to matplotlib docs has problems in these regards), and it "just works" -- I haven't been able to find any difference in how the two codes handle the task: Both define a figure.axes object, pass it to routines that draw into them (using plt.plot()) and store it for future use.
I've also found that my current code will change its behaviour on the current set of libraries, depending on how I run it: In a "regular" console in Spyder or a system console, all's fine, but on a dedicated console in Spyder, the first plot will lock up the interface
=> I'm beginning to think this isn't about the matplotlib version after all, but have no idea where else to look --is there anything else that changes matplotlib's behaviour, based on how it's launched?
It looks like you are looking for:
plt.show(block=False)
or plt.ion:
plt.ion()
plt.show()
# update figure, calc stuff
plt.pause()
Not really sure if they are available in 0.99.
elyase's answer sent me into the right direction:
import matplotlib
matplotlib.interactive(True)
This at the beginning of a script is what it takes to make sure that matplotlib will always and in all environments create plots in a way that allows scripts to continue running, while allowing the user to manipulate the figure and the code to keep drawing into it.
Matplotlib has two modes of operating: The interactive and the non-interactive, and my script going into the latter mode was the problem.
I have still no idea why this is the default behaviour in some cases and not in others (had believed it was one standard setting per matplotlib installation). Since other scripts are able to run without the above. and there must be something that my script inadvertently does to land in non-interactive mode, but the above code is what will override the setting, come what may; at least in all scenarios I've tried so far: python 2.75, matplotlib 1.3.1 using Spyder's regular and dedicated shells and system shells, and Py 2.65, matplotlib 0.99, using system shells and IDLE.
P.S.:
This does not seem to help on Linux. I tried to run the same skript (Tkinter GUI that opens plots, is able to draw into existing plots) on SuSE 13.1 (current version of python 2.7 and all libraries) and no single plot window pops up until the GUI is closed, and then they all jump at me ... it seems that it's not trivial to make matplotlib behave the same everywhere.

Matplotlib window appears at the back?

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_()

Categories