Commit db88a217 authored by René Fritze's avatar René Fritze Committed by René Fritze

[vis] hack the MPL anim stuff into our patch axes

parent 19c454f5
......@@ -2,6 +2,7 @@
# Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
import numpy as np
from ipywidgets import HTML, HBox
from pymor.core.config import config
from pymor.discretizers.builtin.gui.matplotlib import MatplotlibPatchAxes, Matplotlib1DAxes
......@@ -64,6 +65,7 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
def __init__(self):
if separate_colorbars:
# todo rescaling not set up
if rescale_colorbars:
self.vmins = tuple(np.min(u[0]) for u in U)
self.vmaxs = tuple(np.max(u[0]) for u in U)
......@@ -85,10 +87,10 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
self.plots = 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)):
ax = figure.add_subplot(rows, columns, i+1)
axes.append(ax)
plots.append(MatplotlibPatchAxes(figure, grid, bounding_box=bounding_box, vmin=vmin, vmax=vmax,
plots.append(MatplotlibPatchAxes(U=u, ax=ax, figure=figure, grid=grid, bounding_box=bounding_box, vmin=vmin, vmax=vmax,
codim=codim, colorbar=separate_colorbars))
if legend:
ax.set_title(legend[i])
......@@ -111,16 +113,12 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
plot.set(u[ind], vmin=vmin, vmax=vmax)
plot = Plot()
plot.set(U, 0)
if len(U[0]) > 1:
from ipywidgets import interact, IntSlider
def set_time(t):
plot.set(U, t)
return HBox([HTML(p.anim.to_jshtml()) for p in plot.plots])
interact(set_time, t=IntSlider(min=0, max=len(U[0])-1, step=1, value=0))
return plot
return None
......@@ -216,5 +214,3 @@ def visualize_matplotlib_1d(grid, U, codim=1, title=None, legend=None, separate_
plot = Plot()
plot.set(U[0])
return None
......@@ -8,6 +8,7 @@ scalar data assigned to one- and two-dimensional grids using
"""
import numpy as np
from matplotlib import animation
from pymor.core.config import config
from pymor.discretizers.builtin.grids.constructions import flatten_grid
......@@ -16,7 +17,7 @@ from pymor.discretizers.builtin.grids.referenceelements import triangle, square
class MatplotlibPatchAxes:
def __init__(self, figure, grid, bounding_box=None, vmin=None, vmax=None, codim=2,
def __init__(self, U, ax, figure, grid, bounding_box=None, vmin=None, vmax=None, codim=2,
colorbar=True):
assert grid.reference_element in (triangle, square)
assert grid.dim == 2
......@@ -31,22 +32,27 @@ class MatplotlibPatchAxes:
self.vmin = vmin
self.vmax = vmax
self.codim = codim
a = figure.gca()
self.U = U
a = ax
if self.codim == 2:
self.p = a.tripcolor(self.coordinates[:, 0], self.coordinates[:, 1], self.subentities,
self.p = ax.tripcolor(self.coordinates[:, 0], self.coordinates[:, 1], self.subentities,
np.zeros(len(self.coordinates)),
vmin=self.vmin, vmax=self.vmax, shading='gouraud')
else:
self.p = a.tripcolor(self.coordinates[:, 0], self.coordinates[:, 1], self.subentities,
self.p = ax.tripcolor(self.coordinates[:, 0], self.coordinates[:, 1], self.subentities,
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
# TODO blit=True
self.anim = animation.FuncAnimation(figure, self.animate,
frames=U, interval=delay_between_frames,
blit=False)
def set(self, U, vmin=None, vmax=None):
def animate(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
U = np.array(U)
p = self.p
if self.codim == 2:
p.set_array(U)
......@@ -55,6 +61,7 @@ class MatplotlibPatchAxes:
else:
p.set_array(np.tile(U, 2))
p.set_clim(self.vmin, self.vmax)
return (p,)
class Matplotlib1DAxes:
......@@ -123,7 +130,7 @@ if config.HAVE_QT and config.HAVE_MATPLOTLIB:
# noinspection PyShadowingNames
class Matplotlib1DWidget(FigureCanvas):
def __init__(self, parent, grid, count, vmin=None, vmax=None, legend=None, codim=1,
def __init__(self, U, parent, grid, count, vmin=None, vmax=None, legend=None, codim=1,
separate_plots=False, dpi=100):
assert isinstance(grid, OnedGrid)
assert codim in (0, 1)
......
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