Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symmetric_clim for hv.scalar #343

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions discretisedfield/plotting/hv.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def callback(*args, **kwargs):
else:
return self.scalar(kdims=kdims, **scalar_kw)

def scalar(self, kdims, roi=None, n=None, **kwargs):
def scalar(self, kdims, roi=None, n=None, symmetric_clim=False, **kwargs):
"""Create an image plot for scalar data or individual components.

This method creates a dynamic holoviews plot (``holoviews.DynamicMap``) based on
Expand Down Expand Up @@ -270,6 +270,11 @@ def scalar(self, kdims, roi=None, n=None, **kwargs):
Re-sampling of the array with the given number of points. If not specified
no re-sampling is done.

symmetric_clim : bool, optional

If set to ``True`` the colourbar is centred around 0. Defaults to ``False``.
This argument has no effect if ``clim`` is passed as an argument.

kwargs

Additional keyword arguments that are forwarded to ``.opts`` of the
Expand Down Expand Up @@ -315,22 +320,27 @@ def scalar(self, kdims, roi=None, n=None, **kwargs):
roi = self._setup_roi(roi, kdims)
self._check_n(n)

def _plot(*values):
def _plot(symmetric_clim, *values):
data = self.callback(**dict(zip(dyn_kdims, values)))
data = self._filter_values(
data, roi, kdims, dyn_kdims=dict(zip(dyn_kdims, values))
)
data = self._resample(data, kdims, n)

if symmetric_clim and "clim" not in kwargs:
vabs_max = np.nanmax(np.abs(data.data))
kwargs["clim"] = (-vabs_max, vabs_max)

plot = hv.Image(data=data, kdims=kdims).opts(**kwargs)

for dim in plot.kdims:
dim.unit = self.key_dims[dim.name].unit

return plot

return hv.DynamicMap(_plot, kdims=dyn_kdims).redim.values(
**{dim: self.key_dims[dim].data for dim in dyn_kdims}
)
return hv.DynamicMap(
functools.partial(_plot, symmetric_clim), kdims=dyn_kdims
).redim.values(**{dim: self.key_dims[dim].data for dim in dyn_kdims})

def vector(
self,
Expand Down