-
Notifications
You must be signed in to change notification settings - Fork 225
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
Closed Figure's state is silently changed in HTML export when plotting asynchronously #584
Comments
The reason why I can't just discard the |
Hi @mmatthe thank you for the report, this is definitely a bug! Unfortunately it's a bug in the section of code that's been most difficult to get workign properly over the years: saving a version of the plot into notebook. The threading is not necessary to reproduce this. You can reproduce with the following: #cell one
%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
plt.plot(x,x) Wait for that cell to finish then run cell 2: plt.plot(x, x+np.sin(x)) After saving, closing and reopening the notebook the plot will not only display the straight line, not both lines. In fact any modification of the plot that happens after the initial display will not be saved properly. The issue is that the ipympl/ipympl/backend_nbagg.py Lines 384 to 385 in fa3d346
I'm not sure if there is an easy way to update the mimebundle once displayed. Though I suspect that @martinRenou might know as the person who got us to this state where anything is saved (used to be that nothing was saved).
The import matplotlib.pyplot as plt
plt.rcParams.update({'figure.max_open_warning': 0}) |
Ahhh I should have looked harder. This is a known issue. See the discussion (from 2021) here: #359 (comment) I wonder if #376 might enable a fix for this, though my understanding of this stuff is a bit hazy now after 4 years. |
@mmatthe do you have this option checked? when I have that option on then then update the plot twice then I get to see the first update, but not the second! which seems to strongly suggest that an Line 253 in fa3d346
and then the animations should be saved properly |
Note for potential fix:(A note for future me unless someone else wants to implement this - im supposed to be thesis writing right now) I think we either need to wait on the Line 337 in fa3d346
apparently setting Line 251 in fa3d346
is async which is why the I think @mmatthe is seeing only the first image because of not having the save widget state option checked as true, in that case there is no obvious way I see to update the mimebundle that was created when the image was first displayed |
Hi @ianhi, thanks for the analysis! In fact, I have the option "Save Widget State Automatically" checked to "on". Otherwise, it would only show the straight line of the first call to Also, I believe that threading is necessary to raise this problem, because it seems to be that the execution of the cell itself needs to be finished but afterwards the ydata needs to be changed. Anyway, I have found a workaround - I close the figures all the time for development, but for export I just don't close them. Thi switch is done with a flag at the top of the notebook. Then the export is fine and development is not impeded by growing RAM requirements of too many open figures. |
Describe the issue
I know, this is a seemingly contrived example, but it's a real-world problem for me.
See attached Notebook and according HTML export
issue.zip
I'm running the notebook cell by cell, where I only start the next cell when the previous animation has been finished. The animation stops automatically. The code for animation is:
Within this code, a thread is started which shows a figure and continuously updates the ydata of the plotted line. The cell immediately returns, but the thread runs for the given amount of seconds. This leads to an animation in the notebook. Note that in the first iteration, a linear line is plotted instead of the sine wave. After running all cells, the notebook looks like this:
Upon pressing save and exporting to HTML, the exported HTML looks like this:
Apparently, for the HTML export, the state of the first plot widget has been replaced by the data that was first set by
set_ydata
. Adding more calls torunPlot
only keeps the last figure in the correct shape for export, whereas the other figures contain the data initially set byset_ydata
.The problem does not happen when I create a new figure without a number all the time, i.e. replace this
with
However, as all figures are closed by
close('all')
, no data should be changed in previously open figures anymore.Versions
The text was updated successfully, but these errors were encountered: