.Spyder opens a figure instead of absence of plt.show() - python

I actually know a problem in Spyder. When I Work with matplotlib.pyplot, it automatically shows the figure without any kind of plt.show(). So when I make many different figures, it always shows them all on the same.
I've recently made a program, which is saving one figure by iteration in an for loop, it also opens a figure and at the end, the loop crashes before ending because of too many figures showed.
I would like to know if someone also knows such problems, because I've encountered the in many computers running spyder

You likely are running in iPython with Spyder. iPython automatically runs plt.show(). Press f6 (default), and see if "execute in current python or iPython console" is selected. If so, check the console, the 'tab' should show a blue icon with "iP" and have 'kernal' (or similar) following it.
Simple fix: switch to "execute in dedicated python console" or open a new python console (tab header "python 1")
Source: had same problem.

Related

Show matplotlib figures during debug in visual studio code (using ssh conection)

I am working with visual studio code (python) using ssh to access a remote server (where the code is located). I am able to write code, run and debug without any problem. However I am not able to generate matplotlib figures during debug in the same way as i can do it without the ssh connection.
I've tried to follow several suggestions from internet and other post here but none of them is fully working.
I've tried the option of " Jupiter> Debug current file in interactive window " but it does not work. I am not sure why, but the debug in the interactive window is not responding and i can not work during debug.
I've tried python debug and the only matplotlib backend that works is (matplotlib.use("WebAgg"). However for this backend it only works with one figure and the debug is block after the plot.
I have also tried to use X.Org (in my case xquartz over mac) but the figures are really slow and it is not useful.
Any idea on how to plot figures during debug in the same way as in local development?
Thanks!
I asked this question in GitHub and got the following answers:
One way to do this is to do the 'Debug your current file in the interactive window'.
There's a bunch of caveats though.
You need to have ipykernel installed into the python environment
you're using. If you run that command it should have asked you to do
so.
It's likely easier to use if you put cell markers around pieces of
code # %%. This would also allow you to prerun bits of your script
before you need to debug the part that's causing problems. This is
what it looks like when you do that:
When debugging in the interactive window, your code in your script
is running as an IPython cell. If you split it up, it might be
multiple cells or it could be one large cell (for the whole file).
As you step, the cell execution is moved forward one line at a time.
However the execution is paused in between steps. This means
anything you type in the interactive window won't be executed.
Instead you have to run extra code in the 'debugger console'. Shown
below:
This debugger console is where you can run normal matplotlib
commands.

Jupyter Notebook debugging not working on VS Code

I'm trying to activate the debugging functions that are now available on VSCode for Jupyter Notebooks but I'm having some problems with that.
In my interface I can see the button to 'Run by Line' next to every cell but when I click it nothing happens except the normal execution of the entire cell, but that mode is supposed to execute your cell line by line... I've tried putting some breakpoints even I know it is not necessary in that mode but still nothing.
As you can see here the buttons for debugging are shown a few seconds after hitting the 'Run by Line' button but they do nothing.
As a workaround to not being able to use that useful mode, I tried the standard debug mode of VSCode for Jupyter Notebooks which let you go breakpoint to breakpoint on your cell, the problem (I think it is) is that when I use that what VSCode does is open another tab with just the code of the cell I'm debugging in a temporary .py file, which is not the behaviour I would like to have, because I think it should do that on the Notebook tab itself without any new file...
Here I show you a screenshot of the temporary .py that it's created:
That's just the code of my notebook first cell!
Any help with this? I would like to solve both problems or at least one of them.
I've checked the requisites for the debug modes and make sure I fullfill everyone of them but I'm not so used to use the debug functionalities of VSCode so maybe I'm skipping something.
This is a current bug. It was notified to the dev team here:
https://github.com/microsoft/vscode-jupyter/issues/8258
The bug is caused by vscode using a shortened username. In my case GUILLE~1 instead of Guillermo.
The current solution available is to create a new user in your computer with a maximum length of 8 characters.

How to run code selection in Spyder without changing the active window to IPython console?

This is mostly a question regarding work-flow in Spyder, and not a pure programming question. If anyone thinks it has no place here, then please let me know and I'll remove it.
Here we go:
I'm running Spyder on a Windows 64-bit operating system. The interaction between the Spyder IDE and IPython console depends on how you choose to run your code, and I would like to specify how this interaction works.
Among several options to run parts of the code, the difference between the following two is what puzzles me:
Option 1 - Select parts of the code and hit F9
This runs the selected code. No surprise there.
Hit F9 again, and the same selection is executed once more.
Option 2 - Run the same selection using Cells (#%%) and Ctrl+Enter
The code is run just like before. But hit Ctrl-Enter again, and nothing happens. Probably because the IPython console now is the active window.
I would like to find some way of changing this so I can use Ctrl+Enter and run the cell contents multiple times without having to make the Spyder IDE the active window by manually clicking on it. I don't know if this is possible at all. Googling and messing around in the settings under Run / configure and Tools / Preferences has so far been useless.
Thank you for any suggestions!
P.S
In case you're wondering why this is such a big deal to me, I'm rendering multiple charts and exporting them to Power Point with minor changes each time the code is run. This way, new parts of the charts (lines, textboxes, etc) are revealed when I'm working my way through a finished Power Point presentation. And for my basic data science needs, this has turned out to be a pretty much optimal work-flow. Except, of course, that I have to manually select the Spyder IDE each time I run different selections of the code.
In the latest spyder version, the keyboard shortcut to run the highlighted lines of code is F9 but it doesn't seem to work.
Go change it from TOOLS>>PREFERENCES>>KEYBOARD SHORTCUTS then double click on any shortcut you want to change and change with your shortcut. But be very careful if you used the same shortcut that is used for any other work then for that it will remove.
I changed the shortcut for 'run selection' from F9 to ctrl + Enter
in Option 2, you should use Shift+Enter instead

pick_event command in Canopy

I recently upgraded computers and I switch from using Python to Canopy Express since all the packages I use in my coding comes included in Canopy. However, when I went to run a program I wrote that uses matplotlib to create interactive plots that I could click on data points using 'pick_event' in Canopy, I'm getting a few issues with the program that I didn't have in Python. When I run the program, Canopy displays the graph and then immediately moves to the next line of code. This creates a problem for me since I want the graph to be interactive and it doesn't give me a chance to choose my data points I want. I have found ways to leave the graph up (adding in a raw input after displaying the plot or not closing the plot in the program) but Canopy crashes when I try to move or click on the plot. Is there any way I can fix this issue so I can use 'pick_event' command?
Thanks for the help
It seems likely that your code is not written to optionally run in IPython's %pylab mode (or, equivalently in this context, %matplotlib mode), which starts a GUI event loop for you and in many cases makes interactions between prompt and GUI easier. You might consider adapting your code to be able to do this, but meanwhile you can test this hypothesis by disabling pylab mode in the Canopy Preference menu (Python tab).
This article may be relevant:
Using Wx/WxPython in Canopy's IPython panel

Spyder + PythonXY - Python Interpreter and IPython OUTPUT issues - no output

In Spyder (installed together with PythonXY) on a win 8 64 bit machine, both the Python interpreter and the IPython interpreter have output issues.
The first one, gives output at first but as soon as I run some particular code, it doesn't anymore. I noticed the issue happens easily if I get some error messages first, or if I use ipdb, but not only. Even with some code that run smoothly, it happens that it stops giving output afterward.
IPython is opened in a dedicated IPython console tab, although it's kernel opens a window in the console tab (this did not happen to me with previous versions of spyder and it all opened in the console tab). I need to input commands to the interpreter in the IPython console tab window, but the output is sent to the console tab window instead. Very annoying.
Anybody having the same issues? Solutions?
Thanks a lot!
Edit for Carlos Cordoba:
So, for example, in the normal Python interpreter I do:
import module
module.method1(par1)
where method1 has no errors in it. The method works fine and I get the right output. If I try method2 which contains an error, I get no output.
module.method2(par2)
After this, I get no output at all for any code I right. I have to restart the kernel.
I could not reproduce the same behavior right away on the "IPython console" window. It displays the error message correctly and the output keeps coming out correctly. However, if I use ipdb there, the output goes to the Kernel window instead of the console window, which is inconvenient.
Thank you!

Categories