8000 Add tests to check .rectangles and .lines fallback · matplotlib/matplotlib@ba30bdf · GitHub
[go: up one dir, main page]

Skip to content

Commit ba30bdf

Browse files
committed
Add tests to check .rectangles and .lines fallback
1 parent 143cb9a commit ba30bdf

File tree

3 files changed

+70
-26
lines changed

3 files changed

+70
-26
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
``CheckButtons``
2-
~~~~~~~~~~~~~~~~~~~~~~~~~
1+
``CheckButtons.rectangles`` and ``CheckButtons.lines``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
``CheckButtons.rectangles`` and ``CheckButtons.lines`` are deprecated. (``CheckButtons`` now draws itself using `~.Axes.scatter`.)

lib/matplotlib/tests/test_widgets.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import matplotlib.colors as mcolors
88
import matplotlib.widgets as widgets
99
import matplotlib.pyplot as plt
10+
from matplotlib.patches import Rectangle
11+
from matplotlib.lines import Line2D
1012
from matplotlib.testing.decorators import check_figures_equal, image_comparison
1113
from matplotlib.testing.widgets import (click_and_drag, do_event, get_ax,
1214
mock_event, noop)
@@ -1009,8 +1011,7 @@ def test_check_radio_buttons_image():
10091011
cb = widgets.CheckButtons(rax2, ('Check 1', 'Check 2', 'Check 3'),
10101012
(False, True, True))
10111013
with pytest.warns(DeprecationWarning):
1012-
# Trigger old-style Rectangle check boxes
1013-
cb.rectangles
1014+
cb.rectangles # Trigger old-style Rectangle check boxes
10141015

10151016

10161017
@check_figures_equal(extensions=["png"])
@@ -1035,6 +1036,55 @@ def test_check_buttons(fig_test, fig_ref):
10351036
ax.text(.25, 1/3, "coffee", transform=ax.transAxes, va="center")
10361037

10371038

1039+
@check_figures_equal(extensions=["png"])
1040+
def test_check_buttons_rectangles(fig_test, fig_ref):
1041+
# Test should be removed once .rectangles is removed
1042+
cb = widgets.CheckButtons(fig_test.subplots(), ["", ""],
1043+
[False, False])
1044+
with pytest.warns(DeprecationWarning):
1045+
cb.rectangles
1046+
ax = fig_ref.add_subplot(xticks=[], yticks=[])
1047+
ys = [2/3, 1/3]
1048+
dy = 1/3
1049+
w, h = dy / 2, dy / 2
1050+
rectangles = [
1051+
Rectangle(xy=(0.05, ys[i] - h / 2), width=w, height=h,
1052+
edgecolor="black",
1053+
facecolor="none",
1054+
transform=ax.transAxes
1055+
)
1056+
for i, y in enumerate(ys)
1057+
]
1058+
for rectangle in rectangles:
1059+
ax.add_patch(rectangle)
1060+
1061+
1062+
@check_figures_equal(extensions=["png"])
1063+
def test_check_buttons_lines(fig_test, fig_ref):
1064+
# Test should be removed once .lines is removed
1065+
cb = widgets.CheckButtons(fig_test.subplots(), ["", ""], [True, True])
1066+
with pytest.warns(DeprecationWarning):
1067+
cb.lines
1068+
for rectangle in cb._rectangles:
1069+
rectangle.set_visible(False)
1070+
ax = fig_ref.add_subplot(xticks=[], yticks=[])
1071+
ys = [2/3, 1/3]
1072+
dy = 1/3
1073+
w, h = dy / 2, dy / 2
1074+
lineparams = {'color': 'k', 'linewidth': 1.25,
1075+
'transform': ax.transAxes,
1076+
'solid_capstyle': 'butt'}
1077+
for i, y in enumerate(ys):
1078+
x, y = 0.05, y - h / 2
1079+
l1 = Line2D([x, x + w], [y + h, y], **lineparams)
1080+
l2 = Line2D([x, x + w], [y, y + h], **lineparams)
1081+
1082+
l1.set_visible(True)
1083+
l2.set_visible(True)
1084+
ax.add_line(l1)
1085+
ax.add_line(l2)
1086+
1087+
10381088
def test_slider_slidermin_slidermax_invalid():
10391089
fig, ax = plt.subplots()
10401090
# test min/max with floats

lib/matplotlib/widgets.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,13 +1012,11 @@ def __init__(self, ax, labels, actives=None):
10121012

10131013
self._squares = ax.scatter(
10141014
[0.15] * len(ys), ys, marker='s', s=text_size**2,
1015-
c=["none" for _ in range(len(ys))],
1016-
linewidth=1, transform=ax.transAxes, edgecolor="k"
1015+
c="none", linewidth=1, transform=ax.transAxes, edgecolor="k"
10171016
)
1018-
mask = [not x for x in actives]
10191017
self._crosses = ax.scatter(
10201018
[0.15] * len(ys), ys, marker='x', linewidth=1, s=text_size**2,
1021-
c=["k" if actives[i] else "none" for i in range(len(ys))],
1019+
c=["k" if active else "none" for active in actives],
10221020
transform=ax.transAxes
10231021
)
10241022

@@ -1030,22 +1028,19 @@ def _clicked(self, event):
10301028
if self.ignore(event) or event.button != 1 or event.inaxes != self.ax:
10311029
return
10321030
pclicked = self.ax.transAxes.inverted().transform((event.x, event.y))
1033-
_, square_inds = self._squares.contains(event)
1034-
coords = self._squares.get_offset_transform().transform(
1035-
self._squares.get_offsets()
1036-
)
10371031
distances = {}
10381032
if hasattr(self, "_rectangles"):
10391033
for i, (p, t) in enumerate(zip(self._rectangles, self.labels)):
1034+
x0, y0 = p.get_xy()
10401035
if (t.get_window_extent().contains(event.x, event.y)
1041-
or (
1042-
p.get_x() <= pclicked[0] <= p.get_x()
1043-
+ p.get_width()
1044-
and p.get_y() <= pclicked[1] <= p.get_y()
1045-
+ p.get_height()
1046-
)):
1036+
or (x0 <= pclicked[0] <= x0 + p.get_width()
1037+
and y0 <= pclicked[1] <= y0 + p.get_height())):
10471038
distances[i] = np.linalg.norm(pclicked - p.get_center())
10481039
else:
1040+
_, square_inds = self._squares.contains(event)
1041+
coords = self._squares.get_offset_transform().transform(
1042+
self._squares.get_offsets()
1043+
)
10491044
for i, t in enumerate(self.labels):
10501045
if (i in square_inds["ind"]
10511046
or t.get_window_extent().contains(event.x, event.y)):
@@ -1074,12 +1069,12 @@ def set_active(self, index):
10741069
raise ValueError(f'Invalid CheckButton index: {index}')
10751070

10761071
cross_facecolors = self._crosses.get_facecolor()
1077-
cross_facecolors[index] = (
1078-
colors.to_rgba("black")
1072+
cross_facecolors[index] = colors.to_rgba(
1073+
"black"
10791074
if colors.same_color(
1080-
cross_facecolors[index], colors.to_rgba("none")
1075+
cross_facecolors[index], colors.to_rgba("none")
10811076
)
1082-
else colors.to_rgba("none")
1077+
else "none"
10831078
)
10841079
self._crosses.set_facecolor(cross_facecolors)
10851080

@@ -1098,9 +1093,8 @@ def get_status(self):
10981093
"""
10991094
Return a tuple of the status (True/False) of all of the check buttons.
11001095
"""
1101-
return [False if colors.same_color(
1102-
self._crosses.get_facecolors()[i], colors.to_rgba("none"))
1103-
else True for i in range(len(self.labels))]
1096+
return [not colors.same_color(color, colors.to_rgba("none"))
1097+
for color in self._crosses.get_facecolors()]
11041098

11051099
def on_clicked(self, func):
11061100
"""
@@ -1124,7 +1118,7 @@ def rectangles(self):
11241118
rectangles = self._rectangles = [
11251119
Rectangle(xy=(0.05, ys[i] - h / 2), width=w, height=h,
11261120
edgecolor="black",
1127-
facecolor=self._squares.get_facecolor()[i],
1121+
facecolor="none",
11281122
transform=self.ax.transAxes
11291123
)
11301124
for i, y in enumerate(ys)

0 commit comments

Comments
 (0)
0