Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f09fa1

Browse files
committed
Set rectangles and lines property for backcompat
1 parent 04d8fda commit 0f09fa1

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

doc/api/prev_api_changes/api_changes_2.1.0.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ The ``shading`` kwarg to `~matplotlib.axes.Axes.pcolor` has been
422422
removed. Set ``edgecolors`` appropriately instead.
423423

424424

425-
Functions removed from the `.lines` module
426-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
425+
Functions removed from the ``lines`` module
426+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
427427

428428
The :mod:`matplotlib.lines` module no longer imports the
429429
``pts_to_prestep``, ``pts_to_midstep`` and ``pts_to_poststep``

lib/matplotlib/tests/test_widgets.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,8 +1006,13 @@ def test_check_radio_buttons_image():
10061006
rb = widgets.RadioButtons(rax1, ('Radio 1', 'Radio 2', 'Radio 3'))
10071007
with pytest.warns(DeprecationWarning):
10081008
rb.circles # Trigger the old-style elliptic radiobuttons.
1009-
widgets.CheckButtons(rax2, ('Check 1', 'Check 2', 'Check 3'),
1010-
(False, True, True))
1009+
cb = widgets.CheckButtons(rax2, ('Check 1', 'Check 2', 'Check 3'),
1010+
(False, True, True))
1011+
with pytest.warns(DeprecationWarning):
1012+
# Trigger old-style Rectangle check boxes
1013+
cb.rectangles
1014+
with pytest.warns(DeprecationWarning):
1015+
cb.lines
10111016

10121017

10131018
@check_figures_equal(extensions=["png"])
@@ -1020,6 +1025,18 @@ def test_radio_buttons(fig_test, fig_ref):
10201025
ax.text(.25, 1/3, "coffee", transform=ax.transAxes, va="center")
10211026

10221027

1028+
@check_figures_equal(extensions=["png"])
1029+
def test_check_buttons(fig_test, fig_ref):
1030+
widgets.CheckButtons(fig_test.subplots(), ["tea", "coffee"], [True, True])
1031+
ax = fig_ref.add_subplot(xticks=[], yticks=[])
1032+
ax.scatter([.15, .15], [2/3, 1/3], marker='s', transform=ax.transAxes,
1033+
s=(plt.rcParams["font.size"] / 2) ** 2, c=["none", "none"])
1034+
ax.scatter([.15, .15], [2/3, 1/3], marker='x', transform=ax.transAxes,
1035+
s=(plt.rcParams["font.size"] / 2) ** 2, c=["k", "k"])
1036+
ax.text(.25, 2/3, "tea", transform=ax.transAxes, va="center")
1037+
ax.text(.25, 1/3, "coffee", transform=ax.transAxes, va="center")
1038+
1039+
10231040
def test_slider_slidermin_slidermax_invalid():
10241041
fig, ax = plt.subplots()
10251042
# test min/max with floats

lib/matplotlib/widgets.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,8 @@ def __init__(self, ax, labels, actives=None):
10111011
for y, label in zip(ys, labels)]
10121012

10131013
self._squares = ax.scatter(
1014-
[0.15] * len(ys), ys, marker='s', c="none", s=text_size**2,
1014+
[0.15] * len(ys), ys, marker='s', s=text_size**2,
1015+
c=["none" for _ in range(len(ys))],
10151016
linewidth=1, transform=ax.transAxes, edgecolor="k"
10161017
)
10171018
mask = [not x for x in actives]
@@ -1038,8 +1039,9 @@ def _clicked(self, event):
10381039
for i, (p, t) in enumerate(zip(self._rectangles, self.labels)):
10391040
if (t.get_window_extent().contains(event.x, event.y)
10401041
or (
1041-
p.get_x() < event.x < p.get_x() + p.get_width()
1042-
and p.get_y() < event.y < p.get_y()
1042+
p.get_x() <= pclicked[0] <= p.get_x()
1043+
+ p.get_width()
1044+
and p.get_y() <= pclicked[1] <= p.get_y()
10431045
+ p.get_height()
10441046
)):
10451047
distances[i] = np.linalg.norm(pclicked - p.get_center())
@@ -1081,11 +1083,10 @@ def set_active(self, index):
10811083
)
10821084
self._crosses.set_facecolor(cross_facecolors)
10831085

1084-
if hasattr(self, "_rectangles"):
1085-
for i, p in enumerate(self._rectangles):
1086-
p.set_facecolor("k" if colors.same_color(
1087-
p.get_facecolor(), colors.to_rgba("none"))
1088-
else "none")
1086+
if hasattr(self, "_lines"):
1087+
l1, l2 = self._lines[index]
1088+
l1.set_visible(not l1.get_visible())
1089+
l2.set_visible(not l2.get_visible())
10891090

10901091
if self.drawon:
10911092
self.ax.figure.canvas.draw()
@@ -1113,24 +1114,51 @@ def disconnect(self, cid):
11131114
"""Remove the observer with connection id *cid*."""
11141115
self._observers.disconnect(cid)
11151116

1117+
@_api.deprecated("3.7")
11161118
@property
11171119
def rectangles(self):
1118-
if not hasattr(self, "rectangles"):
1120+
if not hasattr(self, "_rectangles"):
1121+
ys = np.linspace(1, 0, len(self.labels)+2)[1:-1]
11191122
dy = 1. / (len(self.labels) + 1)
11201123
w, h = dy / 2, dy / 2
11211124
rectangles = self._rectangles = [
1122-
Rectangle(xy=self._squares.get_offsets()[i], width=w, height=h,
1125+
Rectangle(xy=(0.05, ys[i] - h / 2), width=w, height=h,
11231126
edgecolor="black",
11241127
facecolor=self._squares.get_facecolor()[i],
11251128
transform=self.ax.transAxes
11261129
)
1127-
for i in range(len(self.labels))
1130+
for i, y in enumerate(ys)
11281131
]
11291132
self._squares.set_visible(False)
11301133
for rectangle in rectangles:
11311134
self.ax.add_patch(rectangle)
11321135
return self._rectangles
11331136

1137+
@_api.deprecated("3.7")
1138+
@property
1139+
def lines(self):
1140+
if not hasattr(self, "_lines"):
1141+
ys = np.linspace(1, 0, len(self.labels)+2)[1:-1]
1142+
self._crosses.set_visible(False)
1143+
dy = 1. / (len(self.labels) + 1)
1144+
w, h = dy / 2, dy / 2
1145+
self._lines = []
1146+
current_status = self.get_status()
1147+
lineparams = {'color': 'k', 'linewidth': 1.25,
1148+
'transform': self.ax.transAxes,
1149+
'solid_capstyle': 'butt'}
1150+
for i, y in enumerate(ys):
1151+
x, y = 0.05, y - h / 2
1152+
l1 = Line2D([x, x + w], [y + h, y], **lineparams)
1153+
l2 = Line2D([x, x + w], [y, y + h], **lineparams)
1154+
1155+
l1.set_visible(current_status[i])
1156+
l2.set_visible(current_status[i])
1157+
self._lines.append((l1, l2))
1158+
self.ax.add_patch(l1)
1159+
self.ax.add_patch(l2)
1160+
return self._lines
1161+
11341162

11351163
class TextBox(AxesWidget):
11361164
"""

0 commit comments

Comments
 (0)