Skip to content

Commit fb4a19f

Browse files
committed
Add requested fixes
1 parent f855b31 commit fb4a19f

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

lib/matplotlib/legend.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,13 @@ def val_or_rc(val, rc_name):
484484

485485
# Figure out if self.shadow is valid
486486

487-
if not (is_color_like(self.shadow) or isinstance(self.shadow, bool)):
488-
raise ValueError('Legend shadow must be a valid color or bool')
487+
if not (is_color_like(self.shadow) or
488+
self.shadow in (0, 1, True, False)
489+
):
490+
raise ValueError(
491+
'Legend shadow must be a valid color or bool, not '
492+
f'{self.shadow!r} of type {type(self.shadow)}.'
493+
)
489494

490495
# We use FancyBboxPatch to draw a legend frame. The location
491496
# and size of the box will be updated during the drawing time.
@@ -674,7 +679,7 @@ def draw(self, renderer):
674679

675680
if is_color_like(self.shadow):
676681
Shadow(self.legendPatch, 2, -2, color=self.shadow).draw(renderer)
677-
elif self.shadow is True:
682+
elif self.shadow:
678683
Shadow(self.legendPatch, 2, -2).draw(renderer)
679684

680685
self.legendPatch.draw(renderer)

lib/matplotlib/rcsetup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def validate_color_or_bool(c):
316316
try:
317317
return validate_bool(c)
318318
except ValueError:
319-
raise ValueError(f'Could not convert "{c}" to color or bool')
319+
raise ValueError(f'Could not convert "{c!r}" to color or bool')
320320

321321

322322
def validate_color(s):

lib/matplotlib/tests/test_legend.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,11 @@ def test_empty_bar_chart_with_legend():
532532
plt.legend()
533533

534534

535+
@image_comparison(['shadow_argument_types.png'])
535536
def test_shadow_argument_types():
536537
# Test that different arguments for shadow work as expected
537538
fig, ax = plt.subplots()
538539
ax.plot([1, 2, 3], label='test')
539-
shadows = [True, False, 'red', (0.1, 0.2, 0.5), 'tab:cyan', False]
540540

541541
legs = (ax.legend(loc='upper left', shadow=True), # True
542542
ax.legend(loc='upper right', shadow=False), # False
@@ -548,11 +548,12 @@ def test_shadow_argument_types():
548548
ax.add_artist(l)
549549
ax.legend(loc='lower right') # default
550550

551-
legs = [c for c in ax.get_children() if isinstance(c, mpl.legend.Legend)]
552-
assert len(legs) == len(shadows)
553-
for i in range(len(legs)):
554-
assert legs[i].shadow == shadows[i]
555551

552+
def test_shadow_invalid_argument():
553+
# Test if invalid argument to legend shadow
554+
# (i.e. not [color|bool]) raises ValueError
555+
fig, ax = plt.subplots()
556+
ax.plot([1, 2, 3], label='test')
556557
with pytest.raises(ValueError, match="color or bool"):
557558
ax.legend(loc="upper left", shadow="aardvark") # Bad argument
558559

0 commit comments

Comments
 (0)