Commit 1a13ea31 authored by René Fritze's avatar René Fritze Committed by René Fritze

[vis] Quick&Dirty transfer of MPL animation to 1D

parent fb65067d
......@@ -10,6 +10,20 @@ from pymor.discretizers.builtin.gui.matplotlib import MatplotlibPatchAxes, Matpl
from pymor.vectorarrays.interface import VectorArray
class concat_display(object):
"""Display HTML representation of multiple objects"""
template = """<div style="float: left; padding: 10px;">{0}</div>"""
def __init__(self, *args):
self.args = args
def _repr_html_(self):
return '\n'.join(self.template.format(a._repr_html_()) for a in self.args)
def __repr__(self):
return '\n\n'.join(repr(a) for a in self.args)
def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None, legend=None,
separate_colorbars=False, rescale_colorbars=False, columns=2):
"""Visualize scalar data associated to a two-dimensional |Grid| as a patch plot.
......@@ -95,6 +109,7 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
if legend:
ax.set_title(legend[i])
def set(self, U, ind):
if rescale_colorbars:
if separate_colorbars:
......@@ -110,18 +125,11 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
plot = Plot()
if len(U[0]) > 1:
class concat_display(object):
"""Display HTML representation of multiple objects"""
template = """<div style="float: left; padding: 10px;">{0}</div>"""
def __init__(self, *args):
self.args = args
def _repr_html_(self):
return '\n'.join(self.template.format(a._repr_html_()) for a in self.args)
# otherwise the subplot displays twice
plt.close(plot.figure)
return concat_display(*[p.html for p in plot.plots])
def __repr__(self):
return '\n\n'.join(repr(a) for a in self.args)
return plot
# otherwise the subplot displays twice
plt.close(plot.figure)
......@@ -129,7 +137,6 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
return plot
return None
def visualize_matplotlib_1d(grid, U, codim=1, title=None, legend=None, separate_plots=False, separate_axes=False, columns=2):
......@@ -198,7 +205,7 @@ def visualize_matplotlib_1d(grid, U, codim=1, title=None, legend=None, separate_
self.plots = []
axes = []
for i, (vmin, vmax) in enumerate(zip(self.vmins, self.vmaxs)):
for i, (vmin, vmax, u) in enumerate(zip(self.vmins, self.vmaxs, U)):
if separate_plots:
ax = self.figure.add_subplot(rows, columns, i+1)
else:
......@@ -207,7 +214,7 @@ def visualize_matplotlib_1d(grid, U, codim=1, title=None, legend=None, separate_
count = 1
if not separate_plots:
count = len(U[0])
self.plots.append(Matplotlib1DAxes(ax, grid, count, vmin=vmin, vmax=vmax,
self.plots.append(Matplotlib1DAxes(u, ax, self.figure, grid, count, vmin=vmin, vmax=vmax,
codim=codim))
if legend:
ax.set_title(legend[i])
......@@ -222,4 +229,5 @@ def visualize_matplotlib_1d(grid, U, codim=1, title=None, legend=None, separate_
self.plots[0].set(U, vmin=self.vmins[0], vmax=self.vmaxs[0])
plot = Plot()
plot.set(U[0])
if len(U[0]) > 1:
\ No newline at end of file
......@@ -68,14 +68,16 @@ class MatplotlibPatchAxes:
class Matplotlib1DAxes:
def __init__(self, axes, grid, count, vmin=None, vmax=None, codim=1):
def __init__(self, U, axes, figure, grid, count, vmin=None, vmax=None, codim=1):
assert isinstance(grid, OnedGrid)
assert codim in (0, 1)
self.codim = codim
self.grid = grid
self.vmin = vmin
self.vmax = vmax
self.count = count
self.U = U
centers = grid.centers(1)
if grid.identify_left_right:
......@@ -96,15 +98,21 @@ class Matplotlib1DAxes:
self.axes = axes
self.lines = lines
delay_between_frames = 200 # ms
self.anim = animation.FuncAnimation(figure, self.set,
frames=U, interval=delay_between_frames,
blit=True)
# generating the HTML instance outside this class causes the plot display to fail
self.html = HTML(self.anim.to_jshtml())
def set(self, U, vmin=None, vmax=None):
def set(self, u, vmin=None, vmax=None):
self.vmin = self.vmin if vmin is None else vmin
self.vmax = self.vmax if vmax is None else vmax
for i in range(self.count):
u = np.array(U[i])
if self.codim == 1:
if self.periodic:
self.lines[i].set_ydata(np.concatenate((u, [u[0]])))
self.lines[i].set_ydata(np.concatenate((u, [self.U[0]])))
else:
self.lines[i].set_ydata(u)
else:
......@@ -112,6 +120,8 @@ class Matplotlib1DAxes:
pad = (self.vmax - self.vmin) * 0.1
self.axes.set_ylim(self.vmin - pad, self.vmax + pad)
return self.lines
if config.HAVE_QT and config.HAVE_MATPLOTLIB:
......
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