Skip to content

Commit 1416ea3

Browse files
Fixed linting errors and updated type hints for issue 18305
1 parent 92b8809 commit 1416ea3

4 files changed

Lines changed: 35 additions & 26 deletions

File tree

‎lib/matplotlib/figure.py‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,9 @@ def _validate_share_param(param_name, value):
21232123
return value
21242124
if value in ["all", "row", "col"]:
21252125
return value
2126-
raise ValueError(f"{param_name} must be True, False, 'all', 'row', or 'col'")
2126+
raise ValueError(
2127+
f"{param_name} must be True, False, 'all', 'row', or 'col'"
2128+
)
21272129

21282130
sharex = _validate_share_param("sharex", sharex)
21292131
sharey = _validate_share_param("sharey", sharey)
@@ -2299,20 +2301,26 @@ def _do_layout(gs, mosaic, unique_ids, nested):
22992301
def _apply_sharing(ret_dict, share_val, axis_name):
23002302
if share_val is False:
23012303
return
2302-
2304+
23032305
# Group the axes based on their spans
23042306
groups = {}
23052307
for ax in ret_dict.values():
23062308
span = ax.get_subplotspec()
23072309
grid = span.get_gridspec()
2308-
2310+
23092311
if share_val is True or share_val == "all":
23102312
groups.setdefault("all", []).append(ax)
23112313
elif share_val == "row":
2312-
groups.setdefault((grid, span.rowspan.start, span.rowspan.stop), []).append(ax)
2314+
groups.setdefault(
2315+
(grid, span.rowspan.start, span.rowspan.stop),
2316+
[]
2317+
).append(ax)
23132318
elif share_val == "col":
2314-
groups.setdefault((grid, span.colspan.start, span.colspan.stop), []).append(ax)
2315-
2319+
groups.setdefault(
2320+
(grid, span.colspan.start, span.colspan.stop),
2321+
[]
2322+
).append(ax)
2323+
23162324
# Bind the groups together
23172325
for group in groups.values():
23182326
if len(group) > 1:
@@ -2322,7 +2330,7 @@ def _apply_sharing(ret_dict, share_val, axis_name):
23222330
child_ax.sharex(parent)
23232331
else:
23242332
child_ax.sharey(parent)
2325-
2333+
23262334
# Handle tick label visibility per isolated group
23272335
if axis_name == 'x':
23282336
# Find the bottom-most edge within a specific group

‎lib/matplotlib/figure.pyi‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ class FigureBase(Artist):
255255
self,
256256
mosaic: str,
257257
*,
258-
sharex: bool = ...,
259-
sharey: bool = ...,
258+
sharex: bool | Literal["all", "row", "col"] = ...,
259+
sharey: bool | Literal["all", "row", "col"] = ...,
260260
width_ratios: ArrayLike | None = ...,
261261
height_ratios: ArrayLike | None = ...,
262262
empty_sentinel: str = ...,
@@ -269,8 +269,8 @@ class FigureBase(Artist):
269269
self,
270270
mosaic: list[HashableList[T]],
271271
*,
272-
sharex: bool = ...,
273-
sharey: bool = ...,
272+
sharex: bool | Literal["all", "row", "col"] = ...,
273+
sharey: bool | Literal["all", "row", "col"] = ...,
274274
width_ratios: ArrayLike | None = ...,
275275
height_ratios: ArrayLike | None = ...,
276276
empty_sentinel: T = ...,
@@ -283,8 +283,8 @@ class FigureBase(Artist):
283283
self,
284284
mosaic: list[HashableList[Hashable]],
285285
*,
286-
sharex: bool = ...,
287-
sharey: bool = ...,
286+
sharex: bool | Literal["all", "row", "col"] = ...,
287+
sharey: bool | Literal["all", "row", "col"] = ...,
288288
width_ratios: ArrayLike | None = ...,
289289
height_ratios: ArrayLike | None = ...,
290290
empty_sentinel: Any = ...,

‎lib/matplotlib/pyplot.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
from collections.abc import Callable, Hashable, Iterable, Sequence
9797
import pathlib
9898
import os
99-
from typing import Any, BinaryIO, Literal
99+
from typing import Any, BinaryIO
100100

101101
import PIL.Image
102102
from numpy.typing import ArrayLike

‎lib/matplotlib/tests/test_figure.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,18 +1342,18 @@ def test_share_row_col(self):
13421342
]
13431343
fig = plt.figure()
13441344
axd = fig.subplot_mosaic(layout, sharey="row", sharex="col")
1345-
1345+
13461346
# Testing row sharing for y-axis
13471347
axd["A"].set_ylim(0, 50)
13481348
axd["C"].set_ylim(-10, 10)
1349-
1349+
13501350
assert axd["B"].get_ylim() == (0, 50)
13511351
assert axd["D"].get_ylim() == (-10, 10)
13521352
assert axd["E"].get_ylim() != (0, 50)
1353-
1353+
13541354
axd["A"].set_xlim(-5, 5)
13551355
axd["C"].set_xlim(100, 200)
1356-
1356+
13571357
assert axd["E"].get_xlim() == (-5, 5)
13581358
assert axd["D"].get_xlim() != (100, 200)
13591359

@@ -1363,29 +1363,30 @@ def test_share_invalid(self):
13631363
["C", "D"]
13641364
]
13651365
fig = plt.figure()
1366-
1367-
with pytest.raises(ValueError, match="must be True, False, 'all', 'row', or 'col'"):
1366+
1367+
msg = "must be True, False, 'all', 'row', or 'col'"
1368+
with pytest.raises(ValueError, match=msg):
13681369
fig.subplot_mosaic(layout, sharex="invalid_string")
1369-
1370-
with pytest.raises(ValueError, match="must be True, False, 'all', 'row', or 'col'"):
1370+
1371+
with pytest.raises(ValueError, match=msg):
13711372
fig.subplot_mosaic(layout, sharey={"A": "B"})
13721373

13731374
def test_share_row_col_nested(self):
13741375
layout = [
1375-
["A", [["B", "C"],
1376+
["A", [["B", "C"],
13761377
["D", "E"]]]
13771378
]
13781379
fig = plt.figure()
13791380
axd = fig.subplot_mosaic(layout, sharey="row", sharex="col")
1380-
1381+
13811382
axd["B"].set_ylim(0, 50)
13821383
assert axd["C"].get_ylim() == (0, 50)
13831384
assert axd["D"].get_ylim() != (0, 50)
1384-
1385+
13851386
axd["B"].set_xlim(0, 50)
13861387
assert axd["D"].get_xlim() == (0, 50)
13871388
assert axd["C"].get_xlim() != (0, 50)
1388-
1389+
13891390
axd["A"].set_ylim(-10, 10)
13901391
assert axd["B"].get_ylim() != (-10, 10)
13911392

0 commit comments

Comments
 (0)