Commit 4195a51c authored by René Fritze's avatar René Fritze Committed by René Fritze

[gui] prototype jupyter matplotlib output for 1D data

parent a0c8bae8
......@@ -4,7 +4,7 @@
import numpy as np
from pymor.core.config import config
from pymor.discretizers.builtin.gui.matplotlib import MatplotlibPatchAxes
from pymor.discretizers.builtin.gui.matplotlib import MatplotlibPatchAxes, Matplotlib1DAxes
from pymor.vectorarrays.interface import VectorArray
......@@ -88,8 +88,12 @@ 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)
plots.append(MatplotlibPatchAxes(figure, grid, bounding_box=bounding_box, vmin=vmin, vmax=vmax,
codim=codim, colorbar=separate_colorbars))
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))
if legend:
ax.set_title(legend[i])
......
......@@ -57,6 +57,52 @@ class MatplotlibPatchAxes:
p.set_clim(self.vmin, self.vmax)
class Matplotlib1DAxes:
def __init__(self, figure, grid, bounding_box=None, vmin=None, vmax=None, codim=1,
colorbar=True):
assert isinstance(grid, OnedGrid)
assert codim in (0, 1)
self.codim = codim
self.grid = grid
centers = grid.centers(1)
if grid.identify_left_right:
centers = np.concatenate((centers, [[grid.domain[1]]]), axis=0)
self.periodic = True
else:
self.periodic = False
if codim == 1:
xs = centers
else:
xs = np.repeat(centers, 2)[1:-1]
a = figure.gca()
self.lines, = a.plot(xs, np.zeros_like(xs))
# TODO
import matplotlib.pyplot as plt
self.p = plt.cm.ScalarMappable(cmap=plt.get_cmap("viridis"), norm=plt.Normalize(vmin=vmin or 0, vmax=vmax or 1))
if colorbar:
figure.colorbar(self.p, ax=a)
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]])))
else:
self.lines.set_ydata(u)
else:
self.lines.set_ydata(np.repeat(u, 2))
self.p.set_clim(self.vmin, self.vmax)
if config.HAVE_QT and config.HAVE_MATPLOTLIB:
from Qt.QtWidgets import QSizePolicy
......
......@@ -111,12 +111,14 @@ class OnedVisualizer(BasicObject):
If `True`, block execution until the plot window is closed.
"""
def __init__(self, grid, codim=1, block=False):
def __init__(self, grid, codim=1, block=False, backend=None):
assert isinstance(grid, OnedGrid)
assert codim in (0, 1)
backend = backend or ('jupyter' if is_jupyter() else None)
self.__auto_init(locals())
def visualize(self, U, m, title=None, legend=None, block=None):
def visualize(self, U, m, title=None, legend=None, separate_colorbars=False,
rescale_colorbars=False, block=None, filename=None, columns=2):
"""Visualize the provided data.
Parameters
......@@ -137,6 +139,15 @@ class OnedVisualizer(BasicObject):
If `True`, block execution until the plot window is closed. If `None`, use the
default provided during instantiation.
"""
block = self.block if block is None else block
from pymor.discretizers.builtin.gui.qt import visualize_matplotlib_1d
visualize_matplotlib_1d(self.grid, U, codim=self.codim, title=title, legend=legend, block=block)
if filename is not None:
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)
else:
block = self.block if block is None else block
from pymor.discretizers.builtin.gui.qt import visualize_matplotlib_1d
return visualize_matplotlib_1d(self.grid, U, codim=self.codim, title=title, legend=legend, block=block)
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