diff --git a/src/plopp/backends/matplotlib/canvas.py b/src/plopp/backends/matplotlib/canvas.py index 2fcb8c2f..292100f8 100644 --- a/src/plopp/backends/matplotlib/canvas.py +++ b/src/plopp/backends/matplotlib/canvas.py @@ -54,6 +54,9 @@ class Canvas: The aspect ratio for the axes. cbar: Add axes to host a colorbar if ``True``. + legend: + Show legend if ``True``. If ``legend`` is a tuple, it should contain the + ``(x, y)`` coordinates of the legend's anchor point in axes coordinates. """ def __init__( @@ -68,6 +71,7 @@ def __init__( autoscale: Literal['auto', 'grow'] = 'auto', aspect: Literal['auto', 'equal'] = 'auto', cbar: bool = False, + legend: Union[bool, Tuple[float, float]] = True, **ignored, ): # Note on the `**ignored`` keyword arguments: the figure which owns the canvas @@ -88,6 +92,7 @@ def __init__( self.dims = {} self._own_axes = False self._autoscale = autoscale + self._legend = legend if self.ax is None: self._own_axes = True diff --git a/src/plopp/backends/matplotlib/line.py b/src/plopp/backends/matplotlib/line.py index cc6cb44a..4ecd1ce8 100644 --- a/src/plopp/backends/matplotlib/line.py +++ b/src/plopp/backends/matplotlib/line.py @@ -159,8 +159,11 @@ def _make_line( fmt="none", ) - if self.label: - self._ax.legend() + if self.label and self._canvas._legend: + leg_args = {} + if isinstance(self._canvas._legend, (list, tuple)): + leg_args = {'loc': self._canvas._legend} + self._ax.legend(**leg_args) def _make_data(self) -> dict: x = self._data.meta[self._dim] diff --git a/src/plopp/graphics/lineview.py b/src/plopp/graphics/lineview.py index 32173d58..371daa45 100644 --- a/src/plopp/graphics/lineview.py +++ b/src/plopp/graphics/lineview.py @@ -57,6 +57,9 @@ class LineView(View): Format of the figure displayed in the Jupyter notebook. If ``None``, a SVG is created as long as the number of markers in the figure is not too large. If too many markers are drawn, a PNG image is created instead. + legend: + Show legend if ``True``. If ``legend`` is a tuple, it should contain the + ``(x, y)`` coordinates of the legend's anchor point in axes coordinates. **kwargs: All other kwargs are forwarded to Matplotlib: @@ -80,6 +83,7 @@ def __init__( title: Optional[str] = None, figsize: Tuple[float, float] = None, format: Optional[Literal['svg', 'png']] = None, + legend: Union[bool, Tuple[float, float]] = True, **kwargs ): super().__init__(*nodes) @@ -98,6 +102,7 @@ def __init__( vmin=vmin, vmax=vmax, autoscale=autoscale, + legend=legend, **kwargs ) self.canvas.yscale = norm diff --git a/src/plopp/plotting/plot.py b/src/plopp/plotting/plot.py index 1a0011ba..32b018c9 100644 --- a/src/plopp/plotting/plot.py +++ b/src/plopp/plotting/plot.py @@ -30,6 +30,7 @@ def plot( vmin: Optional[Union[Variable, int, float]] = None, vmax: Optional[Union[Variable, int, float]] = None, autoscale: Literal['auto', 'grow'] = 'auto', + legend: Union[bool, Tuple[float, float]] = True, **kwargs, ): """Plot a Scipp object. @@ -78,6 +79,9 @@ def plot( The behavior of the axis (1d plots) or the color range limits (2d plots). If ``auto``, the limits automatically adjusts every time the data changes. If ``grow``, the limits are allowed to grow with time but they do not shrink. + legend: + Show legend if ``True``. If ``legend`` is a tuple, it should contain the + ``(x, y)`` coordinates of the legend's anchor point in axes coordinates. **kwargs: All other kwargs are directly forwarded to Matplotlib, the underlying plotting library. The underlying functions called are the following: @@ -102,6 +106,7 @@ def plot( 'vmax': vmax, 'autoscale': autoscale, 'figsize': figsize, + 'legend': legend, **kwargs, }