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

Legend control #232

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions src/plopp/backends/matplotlib/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/plopp/backends/matplotlib/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions src/plopp/functions/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -102,6 +106,7 @@ def plot(
'vmax': vmax,
'autoscale': autoscale,
'figsize': figsize,
'legend': legend,
**kwargs,
}

Expand Down
5 changes: 5 additions & 0 deletions src/plopp/graphics/figline.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class FigLine(BaseFig):
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:

Expand All @@ -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)
Expand All @@ -98,6 +102,7 @@ def __init__(
vmin=vmin,
vmax=vmax,
autoscale=autoscale,
legend=legend,
**kwargs
)
self.canvas.yscale = norm
Expand Down
Loading