Skip to content
Merged
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
18 changes: 17 additions & 1 deletion anyplotlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from anyplotlib.figure import Figure, GridSpec, SubplotSpec, subplots
from anyplotlib.axes import Axes, InsetAxes
from anyplotlib.plot1d import Plot1D, PlotBar
from anyplotlib.plot1d._plot1d import Line1D
from anyplotlib.plot2d import Plot2D, PlotMesh
from anyplotlib.plot3d import Plot3D
from anyplotlib.callbacks import CallbackRegistry, Event
from anyplotlib.markers import MarkerRegistry, MarkerGroup
from anyplotlib.widgets import (
Widget, RectangleWidget, CircleWidget, AnnularWidget,
CrosshairWidget, PolygonWidget, LabelWidget,
Expand All @@ -15,12 +17,26 @@
# Default True: badges appear whenever a figure has help text set.
show_help: bool = True

_COLOR_CYCLE: list[str] = [
"#4fc3f7", "#ff7043", "#aed581", "#ffd54f",
"#ba68c8", "#4db6ac", "#f06292", "#90a4ae",
"#ffb74d", "#a5d6a7",
]


def get_color_cycle() -> list[str]:
"""Return the default color cycle as a list of CSS hex strings."""
return list(_COLOR_CYCLE)


__all__ = [
"Figure", "GridSpec", "SubplotSpec", "subplots",
"Axes", "InsetAxes", "Plot1D", "Plot2D", "PlotMesh", "Plot3D", "PlotBar",
"Line1D",
"CallbackRegistry", "Event",
"MarkerRegistry", "MarkerGroup",
"Widget", "RectangleWidget", "CircleWidget", "AnnularWidget",
"CrosshairWidget", "PolygonWidget", "LabelWidget",
"VLineWidget", "HLineWidget", "RangeWidget",
"show_help",
"show_help", "get_color_cycle",
]
20 changes: 11 additions & 9 deletions anyplotlib/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import numpy as np

_LINESTYLE_ALIASES: dict[str, str] = {
"-": "solid",
"--": "dashed",
":": "dotted",
"-.": "dashdot",
"solid": "solid",
"dashed": "dashed",
"dotted": "dotted",
"dashdot": "dashdot",
"-": "solid",
"--": "dashed",
":": "dotted",
"-.": "dashdot",
"solid": "solid",
"dashed": "dashed",
"dotted": "dotted",
"dashdot": "dashdot",
"step-mid": "step-mid",
"steps-mid": "step-mid",
}


Expand Down Expand Up @@ -49,7 +51,7 @@ def _norm_linestyle(ls: str) -> str:
if canonical is None:
raise ValueError(
f"Unknown linestyle {ls!r}. Expected one of: "
"'solid', 'dashed', 'dotted', 'dashdot', "
"'solid', 'dashed', 'dotted', 'dashdot', 'step-mid' (alias: 'steps-mid') "
"or shorthands '-', '--', ':', '-.'."
)
return canonical
Expand Down
11 changes: 9 additions & 2 deletions anyplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def plot(self, data: np.ndarray,
alpha: float = 1.0,
marker: str = "none",
markersize: float = 4.0,
label: str = "") -> "Plot1D":
label: str = "",
yscale: str = "linear") -> "Plot1D":
"""Attach a 1-D line to this axes cell.

Parameters
Expand Down Expand Up @@ -265,10 +266,16 @@ def plot(self, data: np.ndarray,
color=color, linewidth=linewidth,
linestyle=ls if ls is not None else linestyle,
alpha=alpha, marker=marker, markersize=markersize,
label=label)
label=label, yscale=yscale)
self._attach(plot)
return plot

def semilogy(self, data: np.ndarray,
axes: list | None = None, **kwargs) -> "Plot1D":
"""Attach a 1-D line with a logarithmic y-axis."""
kwargs.setdefault("yscale", "log")
return self.plot(data, axes=axes, **kwargs)

def bar(self, x, height=None, width: float = 0.8, bottom: float = 0.0, *,
align: str = "center",
color: str = "#4fc3f7",
Expand Down
2 changes: 1 addition & 1 deletion anyplotlib/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
VALID_EVENT_TYPES = frozenset({
"pointer_down", "pointer_up", "pointer_move", "pointer_settled",
"pointer_enter", "pointer_leave", "double_click", "wheel",
"key_down", "key_up", "*",
"key_down", "key_up", "close", "*",
})


Expand Down
46 changes: 46 additions & 0 deletions anyplotlib/figure/_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def __init__(self, nrows=1, ncols=1, figsize=(640, 480),
self._axes_map: dict = {}
self._plots_map: dict = {}
self._insets_map: dict = {}
self._hspace: float | None = None
self._wspace: float | None = None
with self.hold_trait_notifications():
self.fig_width = figsize[0]
self.fig_height = figsize[1]
Expand Down Expand Up @@ -149,6 +151,29 @@ def set_help(self, text: str) -> None:
"""
self.help_text = self._resolve_help(text)

def subplots_adjust(self, hspace: float | None = None,
wspace: float | None = None) -> None:
"""Set the spacing between subplot panels.

Only the arguments that are explicitly provided are updated; omitting
an argument leaves the current value unchanged.

Parameters
----------
hspace : float, optional
Fraction of the average row height to use as vertical gap between
panels. ``0.1`` adds a gap of 10 % of the mean row height.
``None`` (default) leaves the current hspace unchanged.
wspace : float, optional
Fraction of the average column width to use as horizontal gap.
``None`` (default) leaves the current wspace unchanged.
"""
if hspace is not None:
self._hspace = float(hspace)
if wspace is not None:
self._wspace = float(wspace)
self._push_layout()

# ── subplot creation ──────────────────────────────────────────────────────
def add_subplot(self, spec) -> Axes:
"""Add a subplot cell and return its :class:`Axes`.
Expand Down Expand Up @@ -303,6 +328,8 @@ def _mg(flag, key):
"panel_specs": panel_specs,
"share_groups": share_groups,
"inset_specs": inset_specs,
"hspace": self._hspace,
"wspace": self._wspace,
})

# ── inset creation ────────────────────────────────────────────────────────
Expand Down Expand Up @@ -464,6 +491,25 @@ def _repr_html_(self) -> str:
"""
return repr_html_iframe(self)

def close(self) -> None:
"""Close the figure.

Fires a ``"close"`` event on every panel's :attr:`callbacks`, then
hides the widget by setting its CSS ``display`` to ``"none"``.
Subsequent calls are no-ops.
"""
if getattr(self, "_closed", False):
return
self._closed = True
close_event = Event(event_type="close")
for plot in self._plots_map.values():
if hasattr(plot, "callbacks"):
plot.callbacks.fire(close_event)
try:
self.layout.display = "none"
except Exception:
pass
Comment on lines +504 to +511

def __repr__(self) -> str:
return (f"Figure({self._nrows}x{self._ncols}, "
f"panels={len(self._plots_map)}, "
Expand Down
Loading
Loading