matplotlib / matplotlib/ipympl

Closed Figure's state is silently changed in HTML export when plotting asynchronously

オープン
#584 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Jupyter Notebook
スター
1.7k
フォーク
234
PR マージ指標
30日以内にマージされた PR はありません

説明

## 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](https://github.com/user-attachments/files/18343071/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:

```python
def runPlot(duration):
plt.close('all')
fig = plt.figure(1)
t = np.linspace(0, 1, 100)
data_line = plt.plot(t, np.sin(6.28*2*t))[0]

def _impl():
start = time.time()
it = 0
while time.time() - start < duration:
freq = time.time() - start
if it == 0:
data_line.set_ydata(t)
else:
data_line.set_ydata(np.sin(6.28*t*freq))
it = it + 1

fig.canvas.draw()
time.sleep(0.03)


T = threading.Thread(target=_impl)
T.start()
```

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:
![image](https://github.com/user-attachments/assets/161dad71-bace-4220-990d-b3c33726055d)

Upon pressing save and exporting to HTML, the exported HTML looks like this:
![image](https://github.com/user-attachments/assets/2ca271e9-4821-4b92-a92a-80a583407d57)

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 to `runPlot` only keeps the last figure in the correct shape for export, whereas the other figures contain the data initially set by `set_ydata`.

The problem does not happen when I create a new figure without a number all the time, i.e. replace this

```
plt.close('all')
fig = plt.figure(1)
```

with

```
fig = plt.figure()
```

However, as all figures are closed by `close('all')`, no data should be changed in previously open figures anymore.

## Versions

```
$ python -c "import sys; print('\n',sys.version); import ipympl; print('ipympl version:', ipympl.__version__)" && jupyter --version && jupyter nbextension list && jupyter labextension list

3.13.1 (main, Dec 19 2024, 10:35:14) [GCC 11.1.0]
ipympl version: 0.9.5
Selected Jupyter core packages...
IPython : 8.30.0
ipykernel : 6.29.5
ipywidgets : 8.1.5
jupyter_client : 8.6.3
jupyter_core : 5.7.2
jupyter_server : 2.14.2
jupyterlab : 4.3.4
nbclient : 0.10.2
nbconvert : 7.16.4
nbformat : 5.10.4
notebook : 7.3.1
qtconsole : not installed
traitlets : 5.14.3
```

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

添付された notebook と HTML エクスポートから始め、報告されている Python、ipympl、Jupyter、ipywidgets のバージョンを使用します。番号付きの figure を使って非同期アニメーションを再現し、毎回新しい figure を作成する場合と比較します。エクスポートされた HTML が、以前に閉じた各 figure の最終状態を保持し、最初の set_ydata 値を表示しなければ完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
jupyter-notebook, python
領域
frontend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。