graphinglib.SmartFigureWCS.__setitem__#
- SmartFigureWCS.__setitem__(key: int | slice | tuple[int | slice], element: Plottable | Iterable[Plottable | None] | SmartFigure | None) None#
Assigns a Plottable, a list of Plottable objects, or a SmartFigure to a specified position in the SmartFigure. The indexing follows classical 2D numpy-like indexing, where the first element corresponds to the row and the second element corresponds to the column.
- Parameters:
- keyint | slice | tuple[int | slice]
The key specifying the location(s) in the SmartFigure to assign the element(s). If a tuple of ints is provided, the element is placed in the corresponding square of the grid, following classical 2D numpy-like indexing. If slices are provided, the element can span multiple squares in the grid. If
num_rowsornum_colsis set to 1, the key can be a single int or slice. Otherwise, the key must be a two-tuple.- elementPlottable | Iterable[Plottable | None] | SmartFigure | None
The element(s) to assign. Must be a Plottable, an iterable of Plottable objects, or a SmartFigure. If a Plottable or an iterable of Plottables is provided, the selected area becomes a child plot containing those elements. If
None, any child overlapping with the specified key will be removed.Note
SmartFigures used as a single plot do not support subplot assignment. To use
__setitem__, first create a layout by settingnum_rowsornum_colslarger than 1.You can access and modify multi-cell child figures by indexing any cell they occupy.
Setting a cell to None will delete the entire child figure occupying that cell.
Assigning new Plottables to a cell with an existing child figure replaces that child figure’s plotted elements while preserving its span.
You can add elements to an existing child plot using the
+=operator.If the requested slice overlaps with multiple different child figures, a GraphingException is raised.
Examples
Create a SmartFigure with 2 rows and 2 columns, and assign Plottables to specific subplots:
fig = SmartFigure(num_rows=2, num_cols=2) fig[0, 0] = gl.Curve(x, y) fig[0, 1] = [gl.Scatter(x, y), gl.Text(1, 1, "text")] fig[1, :] = gl.Histogram(x, n_bins)
Now we have a 2x2 SmartFigure with the following layout:
+------------+------------+ | 0,0 | 0,1 | | Curve | Scatter | | | Text | +------------+------------+ | 1,0 1,1 | | Histogram | +-------------------------+
We can add elements using the
+=operator and remove them usingNone. Notice that we can access the multi-cell Histogram by indexing any cell it occupies:fig[0, 0] += [gl.Curve(x2, y2)] fig[0, 1] = None fig[1, 0] = None # This deletes the Histogram even though it spans both cells # Or equivalently: # fig[1, :] = None
Which will result in the following layout:
+------------+------------+ | 0,0 | 0,1 | | Curve | | | Curve | | +------------+------------+ | 1,0 | 1,1 | | | | +------------+------------+
We can also insert a nested SmartFigure. If it overlaps with existing elements, they will be replaced:
subfigure = SmartFigure(num_rows=2, num_cols=1) subfigure.add_elements(gl.Heatmap(data1), gl.Heatmap(data2)) fig[0, 1] = subfigure # Placed in the top-right cell
Which will lead to the following layout:
+------------+------------+ | 0,0 | Heatmap | | Curve +------------+ | Curve | Heatmap | +------------+------------+ | 1,0 | 1,1 | | | | +------------+------------+