Skip to content
Draft
Show file tree
Hide file tree
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
56 changes: 6 additions & 50 deletions docs/plotting/inspector-plot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -213,56 +213,12 @@
"it is sometimes preferable to only update the target figure when we have stopped dragging or resizing one of the rectangles/polygons,\n",
"instead of every step of the way.\n",
"\n",
"This can be done using the `continuous_update` argument:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14",
"metadata": {},
"outputs": [],
"source": [
"p = pp.inspector(\n",
" da,\n",
" dim='z',\n",
" orientation='vertical',\n",
" logc=True,\n",
" mode='rectangle',\n",
" continuous_update=False,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "15",
"metadata": {
"nbsphinx": "hidden"
},
"outputs": [],
"source": [
"tool = p.children[0].toolbar['inspect']\n",
"tool.value = True\n",
"tool._tool.click(-132, -68)\n",
"tool._tool.click(-56, -21)\n",
"tool._tool.click(4.8, 14)\n",
"tool._tool.click(57, 58)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16",
"metadata": {},
"outputs": [],
"source": [
"p"
"This can be done using `continuous_update=False` in the argument list."
]
},
{
"cell_type": "markdown",
"id": "17",
"id": "14",
"metadata": {},
"source": [
"## Changing the reduction operation\n",
Expand All @@ -279,7 +235,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "18",
"id": "15",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -292,7 +248,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "19",
"id": "16",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -302,7 +258,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "20",
"id": "17",
"metadata": {
"nbsphinx": "hidden"
},
Expand All @@ -321,7 +277,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "21",
"id": "18",
"metadata": {},
"outputs": [],
"source": [
Expand Down
38 changes: 27 additions & 11 deletions src/plopp/plotting/inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from ..core import Node
from ..core.typing import Plottable
from ..core.utils import coord_as_bin_edges
from ..graphics import imagefigure, linefigure
from ..graphics import linefigure
from ..widgets import Box, PointsTool, PolygonTool, RectangleTool
from .common import preprocess, require_interactive_figure
from .slicer import Slicer


def _to_bin_edges(da: sc.DataArray, dim: str) -> sc.DataArray:
Expand All @@ -34,13 +35,6 @@ def _to_bin_centers(da: sc.DataArray, dim: str) -> sc.DataArray:
return da


def _apply_op(da: sc.DataArray, op: str, dim: str) -> sc.DataArray:
out = getattr(sc, op)(da, dim=dim)
if out.name:
out.name = f'{op} of {out.name}'
return out


def _slice_xy(da: sc.DataArray, xy: dict[str, dict[str, int]]) -> sc.DataArray:
x = xy['x']
y = xy['y']
Expand Down Expand Up @@ -318,9 +312,10 @@ def inspector(
dim = data.dims[-1]
bin_edges_node = Node(_to_bin_edges, in_node, dim=dim)
bin_centers_node = Node(_to_bin_centers, bin_edges_node, dim=dim)
op_node = Node(_apply_op, da=bin_edges_node, op=operation, dim=dim)
f2d = imagefigure(
op_node,

slicer = Slicer(
bin_edges_node,
keep=set(data.dims) - {dim},
aspect=aspect,
cbar=cbar,
clabel=clabel,
Expand All @@ -333,13 +328,17 @@ def inspector(
mask_color=mask_color,
nan_color=nan_color,
norm=norm,
operation=operation,
title=title,
vmax=vmax,
vmin=vmin,
xlabel=xlabel,
ylabel=ylabel,
**kwargs,
)

f2d = slicer.figure

match mode:
case 'point':
tool = PointsTool(
Expand Down Expand Up @@ -385,6 +384,23 @@ def inspector(
continuous_update=continuous_update,
)

span = f1d.ax.axvspan(
data.coords[dim].min().value,
data.coords[dim].max().value,
color='gray',
alpha=0.2,
zorder=-np.inf,
)

def update_span(change: dict) -> None:
start, end = change['owner'].controls[dim].value
start = data.coords[dim][dim, start].value
end = data.coords[dim][dim, end + 1].value
span.set_bounds(start, 0, end - start, 1)
f1d.canvas.draw()

slicer.slider.observe(update_span, names='value')

f2d.toolbar['inspect'] = tool
out = [f2d, f1d]
if orientation == 'horizontal':
Expand Down
Loading