Commit 3328aa65 authored by René Fritze's avatar René Fritze Committed by René Fritze

[vis] working example of mpl animation

parent db88a217
......@@ -3,6 +3,7 @@
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
import numpy as np
from ipywidgets import HTML, HBox
from matplotlib import pyplot
from pymor.core.config import config
from pymor.discretizers.builtin.gui.matplotlib import MatplotlibPatchAxes, Matplotlib1DAxes
......@@ -88,6 +89,7 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
self.plots = plots = []
axes = []
for i, (vmin, vmax, u) in enumerate(zip(self.vmins, self.vmaxs, U)):
# we do not use figures.subplots since conditionally returns an axes array or a single object
ax = figure.add_subplot(rows, columns, i+1)
axes.append(ax)
plots.append(MatplotlibPatchAxes(U=u, ax=ax, figure=figure, grid=grid, bounding_box=bounding_box, vmin=vmin, vmax=vmax,
......@@ -95,7 +97,7 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
if legend:
ax.set_title(legend[i])
plt.tight_layout()
# plt.tight_layout()
if not separate_colorbars:
if hasattr(plots[0], 'p'):
figure.colorbar(plots[0].p, ax=axes)
......@@ -115,8 +117,20 @@ 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)
def __repr__(self):
return '\n\n'.join(repr(a) for a in self.args)
return HBox([HTML(p.anim.to_jshtml()) for p in plot.plots])
return concat_display(*[p.html for p in plot.plots])
return plot
......
......@@ -8,7 +8,8 @@ scalar data assigned to one- and two-dimensional grids using
"""
import numpy as np
from matplotlib import animation
from IPython.core.display import display, HTML
from matplotlib import animation, pyplot
from pymor.core.config import config
from pymor.discretizers.builtin.grids.constructions import flatten_grid
......@@ -33,7 +34,7 @@ class MatplotlibPatchAxes:
self.vmax = vmax
self.codim = codim
self.U = U
a = ax
if self.codim == 2:
self.p = ax.tripcolor(self.coordinates[:, 0], self.coordinates[:, 1], self.subentities,
np.zeros(len(self.coordinates)),
......@@ -43,17 +44,23 @@ class MatplotlibPatchAxes:
facecolors=np.zeros(len(self.subentities)),
vmin=self.vmin, vmax=self.vmax, shading='flat')
if colorbar:
figure.colorbar(self.p, ax=a)
delay_between_frames = 200 # ms
figure.colorbar(self.p, ax=ax)
delay_between_frames = 500 # ms
# TODO blit=True
self.anim = animation.FuncAnimation(figure, self.animate,
self.anim = animation.FuncAnimation(figure, self.set,
frames=U, interval=delay_between_frames,
blit=False)
# generating the HTML instance outside this class causes the plot display to fail
self.html = HTML(self.anim.to_jshtml())
# otherwise the subplot displays twice
pyplot.close(figure)
def animate(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
p = self.p
if self.codim == 2:
p.set_array(U)
elif self.reference_element is triangle:
......
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