Commit 44e55cd3 authored by René Fritze's avatar René Fritze Committed by René Fritze

[vis] fix phantom axes disaply in animations

parent b2c6a22a
......@@ -6,7 +6,7 @@
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook"
"%matplotlib inline"
]
},
{
......
......@@ -75,8 +75,6 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
if isinstance(legend, str):
legend = (legend,)
assert legend is None or isinstance(legend, tuple) and len(legend) == len(U)
if len(U) < 2:
columns = 1
class Plot:
......@@ -97,21 +95,22 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
self.vmins = (min(np.min(u) for u in U),) * len(U)
self.vmaxs = (max(np.max(u) for u in U),) * len(U)
rows = int(np.ceil(len(U) / columns))
self.figure = figure = plt.figure()
self.plots = plots = []
axes = plt.subplots(nrows=rows, ncols=columns, squeeze=False)
coord = itertools.product(range(rows), range(columns))
for i, (vmin, vmax, u, c) in enumerate(zip(self.vmins, self.vmaxs, U, coord)):
ax = axes[c]
plots.append(MatplotlibPatchAxes(U=u, ax=ax, figure=figure, grid=grid, bounding_box=bounding_box, vmin=vmin, vmax=vmax,
# this _supposed_ to let animations run in sync
sync_timer = None
for i, (vmin, vmax, u) in enumerate(zip(self.vmins, self.vmaxs, U)):
figure = plt.figure(i)
ax = plt.axes()
sync_timer = sync_timer or figure.canvas.new_timer()
plots.append(MatplotlibPatchAxes(U=u, ax=ax, figure=figure, sync_timer=sync_timer, grid=grid,
bounding_box=bounding_box, vmin=vmin, vmax=vmax,
codim=codim, colorbar=separate_colorbars or i == len(U)-1))
if legend:
ax.set_title(legend[i])
plt.tight_layout()
plt.close(figure)
def set(self, U, ind):
if rescale_colorbars:
......@@ -128,8 +127,6 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
plot = Plot()
if len(U[0]) > 1:
# otherwise the subplot displays twice
plt.close(plot.figure)
return concat_display(*[p.html for p in plot.plots])
return plot
......@@ -209,20 +206,23 @@ def visualize_matplotlib_1d(grid, U, codim=1, title=None, legend=None, separate_
rows = int(np.ceil(len(U) / columns))
self.plots = []
# this _supposed_ to let animations run in sync
sync_timer = None
self.figure, axes = plt.subplots(nrows=rows, ncols=columns, squeeze=False)
coord = itertools.product(range(rows), range(columns))
for i, (vmin, vmax, u, c) in enumerate(zip(self.vmins, self.vmaxs, U, coord)):
for i, (vmin, vmax, u) in enumerate(zip(self.vmins, self.vmaxs, U)):
count = 1
if not separate_plots:
count = len(U[0])
ax = axes[c]
self.plots.append(Matplotlib1DAxes(u, ax, self.figure, grid, count, vmin=vmin, vmax=vmax,
figure = plt.figure(i)
ax = plt.axes()
sync_timer = sync_timer or figure.canvas.new_timer()
self.plots.append(Matplotlib1DAxes(u, ax, figure, sync_timer, grid, count, vmin=vmin, vmax=vmax,
codim=codim))
if legend:
ax.set_title(legend[i])
plt.tight_layout()
plt.tight_layout()
plt.close(figure)
def set(self, U):
if separate_plots:
......
......@@ -18,7 +18,7 @@ from pymor.discretizers.builtin.grids.referenceelements import triangle, square
class MatplotlibPatchAxes:
def __init__(self, U, ax, figure, grid, bounding_box=None, vmin=None, vmax=None, codim=2,
def __init__(self, U, ax, figure, sync_timer, grid, bounding_box=None, vmin=None, vmax=None, codim=2,
colorbar=True):
assert grid.reference_element in (triangle, square)
assert grid.dim == 2
......@@ -49,7 +49,7 @@ class MatplotlibPatchAxes:
delay_between_frames = 200 # ms
self.anim = animation.FuncAnimation(figure, self.set,
frames=U, interval=delay_between_frames,
blit=True)
blit=True, event_source=sync_timer)
# generating the HTML instance outside this class causes the plot display to fail
self.html = HTML(self.anim.to_jshtml())
......@@ -68,7 +68,7 @@ class MatplotlibPatchAxes:
class Matplotlib1DAxes:
def __init__(self, U, axes, figure, grid, count, vmin=None, vmax=None, codim=1):
def __init__(self, U, axes, figure, sync_timer, grid, count, vmin=None, vmax=None, codim=1):
assert isinstance(grid, OnedGrid)
assert codim in (0, 1)
......@@ -101,7 +101,7 @@ class Matplotlib1DAxes:
delay_between_frames = 200 # ms
self.anim = animation.FuncAnimation(figure, self.set,
frames=U, interval=delay_between_frames,
blit=True)
blit=True, event_source=sync_timer)
# generating the HTML instance outside this class causes the plot display to fail
self.html = HTML(self.anim.to_jshtml())
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment