Skip to content

Commit 020fb92

Browse files
committed
Refactor: Enhance subplot adjustment method to allow optional spacing parameters and improve error handling for axis scale settings
1 parent abec7ec commit 020fb92

6 files changed

Lines changed: 28 additions & 16 deletions

File tree

anyplotlib/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _norm_linestyle(ls: str) -> str:
5151
if canonical is None:
5252
raise ValueError(
5353
f"Unknown linestyle {ls!r}. Expected one of: "
54-
"'solid', 'dashed', 'dotted', 'dashdot', 'step-mid' "
54+
"'solid', 'dashed', 'dotted', 'dashdot', 'step-mid' (alias: 'steps-mid') "
5555
"or shorthands '-', '--', ':', '-.'."
5656
)
5757
return canonical

anyplotlib/figure/_figure.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,23 +151,27 @@ def set_help(self, text: str) -> None:
151151
"""
152152
self.help_text = self._resolve_help(text)
153153

154-
def subplots_adjust(self, hspace: float = 0.0, wspace: float = 0.0) -> None:
154+
def subplots_adjust(self, hspace: float | None = None,
155+
wspace: float | None = None) -> None:
155156
"""Set the spacing between subplot panels.
156157
158+
Only the arguments that are explicitly provided are updated; omitting
159+
an argument leaves the current value unchanged.
160+
157161
Parameters
158162
----------
159163
hspace : float, optional
160164
Fraction of the average row height to use as vertical gap between
161165
panels. ``0.1`` adds a gap of 10 % of the mean row height.
162-
Default ``0.0`` (no gap). Before ``subplots_adjust`` is called,
163-
figures use a 4 px browser default gap.
166+
``None`` (default) leaves the current hspace unchanged.
164167
wspace : float, optional
165168
Fraction of the average column width to use as horizontal gap.
166-
Default ``0.0`` (no gap). Before ``subplots_adjust`` is called,
167-
figures use a 4 px browser default gap.
169+
``None`` (default) leaves the current wspace unchanged.
168170
"""
169-
self._hspace = float(hspace)
170-
self._wspace = float(wspace)
171+
if hspace is not None:
172+
self._hspace = float(hspace)
173+
if wspace is not None:
174+
self._wspace = float(wspace)
171175
self._push_layout()
172176

173177
# ── subplot creation ──────────────────────────────────────────────────────
@@ -502,7 +506,7 @@ def close(self) -> None:
502506
if hasattr(plot, "callbacks"):
503507
plot.callbacks.fire(close_event)
504508
try:
505-
self.layout = {"display": "none"}
509+
self.layout.display = "none"
506510
except Exception:
507511
pass
508512

anyplotlib/figure_esm.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,8 +2468,8 @@ function render({ model, el }) {
24682468
const [cx,cy]= tfm==='data' ? _offToCanvas(off) : _tc2d(off[0],off[1]!=null?off[1]:0);
24692469
const wd=ms.widths[i]!=null?ms.widths[i]:(ms.widths[0]||10);
24702470
const hd=ms.heights[i]!=null?ms.heights[i]:(ms.heights[0]||10);
2471-
const rw=Math.max(1,Math.abs(_xPx(off[0]+wd/2)-_xPx(off[0]-wd/2))/2);
2472-
const rh=Math.max(1,Math.abs(_yPx((off[1]||0)-hd/2)-_yPx((off[1]||0)+hd/2))/2);
2471+
const rw=Math.max(1, tfm==='data' ? Math.abs(_xPx(off[0]+wd/2)-_xPx(off[0]-wd/2))/2 : wd/2);
2472+
const rh=Math.max(1, tfm==='data' ? Math.abs(_yPx((off[1]||0)-hd/2)-_yPx((off[1]||0)+hd/2))/2 : hd/2);
24732473
const ang=((ms.angles&&(ms.angles[i]!=null?ms.angles[i]:ms.angles[0])||0)*Math.PI)/180;
24742474
mkCtx.beginPath();mkCtx.ellipse(cx,cy,rw,rh,ang,0,Math.PI*2);
24752475
if(fch){mkCtx.save();mkCtx.globalAlpha=fa;mkCtx.fillStyle=fch;mkCtx.fill();mkCtx.restore();}
@@ -2482,8 +2482,8 @@ function render({ model, el }) {
24822482
const [cx,cy]= tfm==='data' ? _offToCanvas(off) : _tc2d(off[0],off[1]!=null?off[1]:0);
24832483
const wd=ms.widths[i]!=null?ms.widths[i]:(ms.widths[0]||10);
24842484
const hd=heights[i]!=null?heights[i]:(heights[0]||10);
2485-
const rw=Math.max(1,Math.abs(_xPx(off[0]+wd/2)-_xPx(off[0]-wd/2)));
2486-
const rh=Math.max(1,Math.abs(_yPx((off[1]||0)-hd/2)-_yPx((off[1]||0)+hd/2)));
2485+
const rw=Math.max(1, tfm==='data' ? Math.abs(_xPx(off[0]+wd/2)-_xPx(off[0]-wd/2)) : wd);
2486+
const rh=Math.max(1, tfm==='data' ? Math.abs(_yPx((off[1]||0)-hd/2)-_yPx((off[1]||0)+hd/2)) : hd);
24872487
const ang=((ms.angles&&(ms.angles[i]!=null?ms.angles[i]:ms.angles[0])||0)*Math.PI)/180;
24882488
mkCtx.save();mkCtx.translate(cx,cy);mkCtx.rotate(ang);
24892489
if(fch){mkCtx.save();mkCtx.globalAlpha=fa;mkCtx.fillStyle=fch;mkCtx.fillRect(-rw/2,-rh/2,rw,rh);mkCtx.restore();}

anyplotlib/plot1d/_plot1d.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ def __init__(self, data: np.ndarray,
250250
self._id: str = ""
251251
self._fig: object = None
252252

253+
if yscale not in ("linear", "log"):
254+
raise ValueError("yscale must be 'linear' or 'log'")
255+
253256
data = np.asarray(data, dtype=float)
254257
if data.ndim != 1:
255258
raise ValueError(f"data must be 1-D, got {data.shape}")

anyplotlib/plot1d/_plotbar.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ def __init__(self, x, height=None, width: float = 0.8, bottom: float = 0.0, *,
107107
self._id: str = ""
108108
self._fig: object = None
109109

110+
if align not in ("center", "edge"):
111+
raise ValueError("align must be 'center' or 'edge'")
112+
if orient not in ("v", "h"):
113+
raise ValueError("orient must be 'v' or 'h'")
114+
110115
# ── legacy resolution ──────────────────────────────────────────
111116
if height is None:
112117
if values is not None:

anyplotlib/plot3d/_plot3d.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,15 @@ def set_title(self, label: str) -> None:
199199
self._push()
200200

201201
def set_xlabel(self, label: str) -> None:
202-
self._state["x_label"] = label
202+
self._state["x_label"] = str(label)
203203
self._push()
204204

205205
def set_ylabel(self, label: str) -> None:
206-
self._state["y_label"] = label
206+
self._state["y_label"] = str(label)
207207
self._push()
208208

209209
def set_zlabel(self, label: str) -> None:
210-
self._state["z_label"] = label
210+
self._state["z_label"] = str(label)
211211
self._push()
212212

213213
def get_xlim(self) -> tuple:

0 commit comments

Comments
 (0)