I am using matplotlib.pyplot (with Eclipse on Windows). Every time I run my code it opens several pyplot figure windows.
The problem is that if I don't close those windows manually they accumulate. I would like to use pyplot to find those windows (opened by another process of python.exe) and re-use them. In other words, I do not want to have multiple windows for the same figure, even across interpreter processes.
Is there a simple way to do it?
There is no simple way to reuse plot windows if you must use eclipse to run it. When I am working interactively with matplotlib, I use either spyder or ipython. Edit class, reload class, and run code again. If you just want to get rid of all the open plot windows, hit the stacked stop icons to kill all your runing python instances.
Related
I have been running into a trouble whereby Spyder IPython console is not producing Matplotlib figures as desired. I thought initially that there is something wrong in my code since jupyter notebook gives me the same wrong figures. However, when running the script in Spyder using external terminal the figures are produced as desired. Also, when I run the code in VSC the correct figures are displayed.
So the only option I am left with in Spyder is to use the external terminal to execute the code. However, it is quite a pain every time to run some codes and then manually close the terminal.
I would like to know if there is a way to permanently attach the external terminal inside Spyder? I hate the IPython console when it comes to plotting matplotlib figures!!
(Spyder maintainer here) Sorry but there's no way to dock an external Python terminal inside Spyder.
I want to develop custom styles for plots in matplotlib. For this reason I have a custom stylesheet (my_style.mpstyle) located in the default directory (~/.config/matplotlib/styles). I can do this with Python scripts without a problem but recently I find myself using Jupyter more. Although the stylesheets generally work, I have the problem, that changes in the stylesheet are not considered unless I restart the Kernel - is there a way of reloading the stylesheet when I execute the cell such that I can develop the style with Jupyter?
Thanks.
I got around this problem by defining a PYTHONSTARTUP file. This is a file that contains a set of python commands (usually import statements) that are automatically executed every time you launch a python interpreter, even when launching a jupyter application or restarting a kernel within one.
Here's a portion of my startup file that imports pyplot and sets a style sheet.
try:
import matplotlib.pyplot as plt
print('import matplotlib.pyplot as plt')
except:
print('Could not import matplotlib.pyplot')
try:
plt.style.use('mystylesheet')
except:
print('No mpl style sheet set; could not find "mystylesheet"')
To make python execute the startup file, you need to define the environmental variable PYTHONSTARTUP=full/path/to/startupfile. In Linux, it's quite easy; just modify your .bashrc to include
export PYTHONSTARTUP="full/path/to/startupfile"
For windows it is a little more involved, and of course the process varies from one version to another. Here is one website that appears to summarize the process for several versions.
i want to interactively debug code that is deeply hidden in some class.
I know that it is possible open the ipdb using
from IPython.core.debugger import Pdb
Pdb().set_trace()
This work also in ipython (jupyter) notebook.
Another possibility that I find very convenient when I am working in the terminal is to open a shell at a specific point using
from IPython import embed
embed()
and then use ipython directly to interact with variables etc.
However, if I use this in notebook, I get a ipython shell embedded into the notebook, and this shell does not even work well (e.g., if I use print, the kernel blocks).
What I would find most convenient is to interrupt the kernel execution at some predefined point in the code and keep all current variables, so that I can use another cell for debugging and the possibility to, e.g., develop and test new code with the current variables.
Is there some way to achieve this?
I'm using Spyder3.1.2 IDE with Python 3.5 in Windows 10 and would like to know how to choose whether to show my plots in the iPython console or in a separate window. I found this other question but I get an error when I type %matplotlib qt (No module named PyQt4). I have also changed the preferences to show graphics "automatically" instead of "inline", but I'm still getting them inline.
What I want to end up with is a for loop where on every iteration I add a new point to my graph, which is visible immediately and is shown in a separate window to the console.
Many thanks for your time
You can run your script in a new python console every time you run it.
For that press F6 or go to Run/Configure... and select "Execute in a new dedicated Python console.
Since this python console will not be an IPython console, there is no need to perform any other settings.
The plot should show up in a window upon plt.show().
I'm writing plot functions in python scripts and using ipython to show them. I want to show the figures inside the page ,so that I used
%matplotlib inline
After that I can show the figures inside.
Is there a way to put this line inside the plot.py script so in ipython I just import the .py without specifying the %matplot code?
Thanks.
You should only have to run %matplotlib inline once per IPython session. All it does it tell matplotlib to plot graphs inline, as opposed to in a separate window.
Running it more than once (say once per script) seems pointless as it won't actually do anything. You could just run it at the top of your session and then not worry about it, your plots will still plot and you can still have plotting functions inside the Python files.
That aside, adding it inside a script is just not possible. It just returns a SyntaxError because you effectively run it as a Python script, not in the IPython terminal where it's set up to handle the magic methods.