-
Notifications
You must be signed in to change notification settings - Fork 207
MRG: Dont close figures until end of script #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
An alternative solution would be to do everything that is needed for a given figure within a single code block, but that's not as flexible as this solution. |
@@ -351,6 +363,7 @@ def save_figures(image_path, fig_count, gallery_conf): | |||
current_fig = image_path.format(fig_count + fig_mngr.num) | |||
fig.savefig(current_fig, **kwargs) | |||
figure_list.append(current_fig) | |||
fig._sg_captured = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than this monkey-patching I think it would be slightly better to have an additional argument in save_figures, which is a set that remembers the already captured figures.
Alternatively we could have a matplotlib index and a mayavi index keeping track of the number of the last plot saved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I could do that. I don't think indexing will work:
>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> fig.number
1
>>> plt.close('all')
>>> fig = plt.figure()
>>> fig.number
1
Instead I could maintain a list and check with is
. The problem with that approach is that it might lead to memory issues, because the figures can't be garbage-collected even if they are closed by the user. So I'm actually starting to think that the monkey patching, although ugly, might be better.
It would be great to add tests, preferentially both with matplotlib and mayavi. Not a matplotlib expert, I could not find something that goes wrong because of closing all the figures at the end of a block, suggestions welcome! |
I'm not sure about whether it causes problems with I'll look into the Travis failure and try to add tests soon. |
Okay, tests added. I put in a bunch of fixes for Windows-related building because I'm on my Windows dev machine currently. It would be worth adding AppVeyor testing at some point and cleaning up Windows support properly. |
travis is not happy :( |
All better |
I do agree there is some tension between seeing the example as a Jupyter notebook and seing it as a script, for example when you do multiple In @Eric89GXL's case though, I think that we are not in such a tricky case. IIUC he plots two different figures in two different cells. The problem is that closing the first figure (let's call it I would be in favour of not closing the figures since that seems to be a useful work-around in this case, unless we can spot a real blocker in the fine case of trying to plot figures in different cells. I would leave plotting on top of an existing figure and recapturing as undefined behaviour for now. |
But for mayavi or for matplotlib? |
I guess for both, be it only for consistency reasons. |
Closing for now until consensus |
I am +1 on closing the figures only at the end of the script. @Eric89GXL what is your current work-around this in MNE-python? |
In the example where we needed to modify an existing (mayavi) figure, we instead recreated it in the next block and modified it. |
So you have some duplication (the lines to create the figure) in your example? |
Yeah, that's what we ended up going with I think |
Sometimes it is necessary to have figures that are not closed within a given code-block. This modifies the gallery behavior to leave the figures open until the script is done. This is useful e.g. when creating a figure in one code block, and querying some parameter from it later. Our particular use case came from creating a Mayavi figure in one code block, and needing to get the viewport transformation from it in another code-block.