Commit f6fdfc91 authored by Hendrik Kleikamp's avatar Hendrik Kleikamp Committed by René Fritze

[gui] added a seperate visualize function for 1d in jupyter

parent facc2614
......@@ -15,8 +15,7 @@
"metadata": {},
"outputs": [],
"source": [
"from pymor.basic import *\n",
"set_defaults({'pymor.discretizers.builtin.gui.jupyter.get_visualizer.backend': 'not pythreejs'})"
"from pymor.basic import *"
]
},
{
......@@ -72,7 +71,7 @@
"U = m.solution_space.empty()\n",
"for mu in parameter_space.sample_uniformly(10):\n",
" U.append(m.solve(mu))\n",
"m.visualize(U, title='Solution for diffusionl in [0.1, 1]')"
"m.visualize(U, title='Solution for diffusionl in [0.1, 1]', separate_plots=False)"
]
}
],
......
......@@ -13,7 +13,7 @@ inside the given notebook.
import IPython
from pymor.core.defaults import defaults
from pymor.discretizers.builtin.gui.jupyter.patch import visualize_patch
from pymor.discretizers.builtin.gui.jupyter.matplotlib import visualize_patch
from pymor.core.config import config
......
......@@ -88,12 +88,8 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
for i, (vmin, vmax) in enumerate(zip(self.vmins, self.vmaxs)):
ax = figure.add_subplot(rows, columns, i+1)
axes.append(ax)
if grid.dim == 2:
plots.append(MatplotlibPatchAxes(figure, grid, bounding_box=bounding_box, vmin=vmin, vmax=vmax,
codim=codim, colorbar=separate_colorbars))
else:
plots.append(Matplotlib1DAxes(figure, grid, bounding_box=bounding_box, vmin=vmin, vmax=vmax,
codim=codim, colorbar=separate_colorbars))
plots.append(MatplotlibPatchAxes(figure, grid, bounding_box=bounding_box, vmin=vmin, vmax=vmax,
codim=codim, colorbar=separate_colorbars))
if legend:
ax.set_title(legend[i])
......@@ -127,3 +123,66 @@ def visualize_patch(grid, U, bounding_box=([0, 0], [1, 1]), codim=2, title=None,
interact(set_time, t=IntSlider(min=0, max=len(U[0])-1, step=1, value=0))
return None
def visualize_matplotlib_1d(grid, U, codim=1, title=None, legend=None, separate_plots=False, columns=2):
assert isinstance(U, VectorArray) \
or (isinstance(U, tuple)
and all(isinstance(u, VectorArray) for u in U)
and all(len(u) == len(U[0]) for u in U))
U = (U.to_numpy(),) if isinstance(U, VectorArray) else tuple(u.to_numpy() for u in U)
if not config.HAVE_MATPLOTLIB:
raise ImportError('cannot visualize: import of matplotlib failed')
if not config.HAVE_IPYWIDGETS and len(U[0]) > 1:
raise ImportError('cannot visualize: import of ipywidgets failed')
if isinstance(legend, str):
legend = (legend,)
assert legend is None or isinstance(legend, tuple) and len(legend) == len(U)
class Plot:
def __init__(self):
if separate_plots:
self.vmins = tuple(np.min(u) for u in U[0])
self.vmaxs = tuple(np.max(u) for u in U[0])
else:
self.vmins = (min(np.min(u) for u in U),)
self.vmaxs = (max(np.max(u) for u in U),)
import matplotlib.pyplot as plt
rows = int(np.ceil(len(U[0]) / columns))
self.figure = plt.figure()
self.plots = []
axes = []
for i, (vmin, vmax) in enumerate(zip(self.vmins, self.vmaxs)):
if separate_plots:
ax = self.figure.add_subplot(rows, columns, i+1)
else:
ax = self.figure.gca()
axes.append(ax)
count = 1
if not separate_plots:
count = len(U[0])
self.plots.append(Matplotlib1DAxes(ax, grid, count, vmin=vmin, vmax=vmax,
codim=codim))
if legend:
ax.set_title(legend[i])
plt.tight_layout()
def set(self, U):
if separate_plots:
for u, plot, vmin, vmax in zip(U, self.plots, self.vmins, self.vmaxs):
plot.set(u[np.newaxis, ...], vmin=vmin, vmax=vmax)
else:
self.plots[0].set(U, vmin=self.vmins[0], vmax=self.vmaxs[0])
plot = Plot()
plot.set(U[0])
return None
......@@ -59,14 +59,15 @@ class MatplotlibPatchAxes:
class Matplotlib1DAxes:
def __init__(self, figure, grid, bounding_box=None, vmin=None, vmax=None, codim=1,
colorbar=True):
def __init__(self, axes, grid, count, vmin=None, vmax=None, codim=1):
assert isinstance(grid, OnedGrid)
assert codim in (0, 1)
self.codim = codim
self.grid = grid
self.count = count
centers = grid.centers(1)
if grid.identify_left_right:
centers = np.concatenate((centers, [[grid.domain[1]]]), axis=0)
......@@ -77,22 +78,28 @@ class Matplotlib1DAxes:
xs = centers
else:
xs = np.repeat(centers, 2)[1:-1]
self.axes = figure.gca()
self.lines, = self.axes.plot(xs, np.zeros_like(xs))
lines = ()
for i in range(count):
l, = axes.plot(xs, np.zeros_like(xs))
lines = lines + (l,)
pad = (vmax - vmin) * 0.1
axes.set_ylim(vmin - pad, vmax + pad)
self.axes = axes
self.lines = lines
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
u = np.array(U)
if self.codim == 1:
if self.periodic:
self.lines.set_ydata(np.concatenate((u, [u[0]])))
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]])))
else:
self.lines[i].set_ydata(u)
else:
self.lines.set_ydata(u)
else:
self.lines.set_ydata(np.repeat(u, 2))
self.lines[i].set_ydata(np.repeat(u, 2))
self.axes.set_ylim(self.vmin, self.vmax)
......
......@@ -117,8 +117,8 @@ class OnedVisualizer(BasicObject):
backend = backend or ('jupyter' if is_jupyter() else None)
self.__auto_init(locals())
def visualize(self, U, m, title=None, legend=None, separate_colorbars=False,
rescale_colorbars=False, block=None, filename=None, columns=2):
def visualize(self, U, m, title=None, legend=None, separate_plots=False,
block=None, filename=None, columns=2):
"""Visualize the provided data.
Parameters
......@@ -143,10 +143,9 @@ class OnedVisualizer(BasicObject):
raise NotImplementedError
if self.backend == 'jupyter':
from pymor.discretizers.builtin.gui.jupyter import get_visualizer
return get_visualizer()(self.grid, U, bounding_box=self.grid.domain, codim=self.codim, title=title,
legend=legend, separate_colorbars=separate_colorbars,
rescale_colorbars=rescale_colorbars, columns=columns)
from pymor.discretizers.builtin.gui.jupyter.matplotlib import visualize_matplotlib_1d
return visualize_matplotlib_1d(self.grid, U, codim=self.codim, title=title, legend=legend,
separate_plots=separate_plots, columns=columns)
else:
block = self.block if block is None else block
from pymor.discretizers.builtin.gui.qt import visualize_matplotlib_1d
......
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