Skip to content

Commit 8027838

Browse files
committed
defer plot close until end
1 parent e960ff0 commit 8027838

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

src/sdf_xarray/plotting.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,6 @@ def animate(
366366
# Create plot if no ax is provided
367367
if ax is None:
368368
fig, ax = plt.subplots()
369-
# Prevents figure from prematurely displaying in Jupyter notebook
370-
plt.close(fig)
371369

372370
animation = _generate_animation(
373371
data,
@@ -382,14 +380,19 @@ def animate(
382380
kwargs=kwargs,
383381
)
384382

385-
return FuncAnimation(
386-
ax.get_figure(),
383+
anim = FuncAnimation(
384+
fig,
387385
animation.update,
388386
frames=range(animation.n_frames),
389387
interval=1000 / fps,
390388
repeat=True,
391389
)
392390

391+
# Prevents figure from prematurely displaying in Jupyter notebook
392+
plt.close(fig)
393+
394+
return anim
395+
393396

394397
def animate_multiple(
395398
*datasets: xr.DataArray,
@@ -458,8 +461,6 @@ def animate_multiple(
458461
# Create plot if no ax is provided
459462
if ax is None:
460463
fig, ax = plt.subplots()
461-
# Prevents figure from prematurely displaying in Jupyter notebook
462-
plt.close(fig)
463464

464465
n_datasets = len(datasets)
465466
if datasets_kwargs is None:
@@ -508,14 +509,19 @@ def update(frame):
508509
if show_legend:
509510
ax.legend(loc="upper right")
510511

511-
return FuncAnimation(
512-
ax.get_figure(),
512+
anim = FuncAnimation(
513+
fig,
513514
update,
514515
frames=range(n_frames),
515516
interval=1000 / fps,
516517
repeat=True,
517518
)
518519

520+
# Prevents figure from prematurely displaying in Jupyter notebook
521+
plt.close(fig)
522+
523+
return anim
524+
519525

520526
def show(anim):
521527
"""Shows the FuncAnimation in a Jupyter notebook.

0 commit comments

Comments
 (0)