Issue
In the Spyder IDE, I want to keep the inline console plotting (I don't want separate windows to spawn for each plot), but I want to programmatically disable plotting, i.e. in different cells.
In my workflow I need to plot a few simple graphs, and then generate figures and save them as video frames (many thousands). My frames are created by loading a jpg image, and then overlaying some annotation i.e.;
for jpg_path in path_list:
img = mpl.image.imread(jpg_path)
ax.imshow(img)
ax.text(etc...)
fig.savefig(etc...)
I want to keep the inline backend; %matplotlib inline
.
But turn off plotting with something like plt.ioff()
.
But plt.ioff()
only works with i.e. %matplotlib qt
backend, not inline
!
I've had several cases where I forget to change to %matplotlib qt
(because it's not a python command and I have to enter it into the console seperately) and then plt.ioff()
- resulting in 10000 images being posted in the console, freezing my machine.
Solution
Ok I think I found an answer, thanks to this answer;
https://stackoverflow.com/a/46360516/789215
The key was the python command for the line magics get_ipython().run_line_magic('matplotlib', 'inline')
. I created a context manager to wrap my video frame for-loop;
from IPython import get_ipython
class NoPlots:
def __enter__(self):
get_ipython().run_line_magic('matplotlib', 'qt')
plt.ioff()
def __exit__(self, type, value, traceback):
get_ipython().run_line_magic('matplotlib', 'inline')
plt.ion()
Or is there a better approach?
Answered By - Marcus Jones
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.