|
12 | 12 | from matplotlib import _api, _c_internal_utils |
13 | 13 | import matplotlib.pyplot as plt |
14 | 14 | import matplotlib.colors as mcolors |
| 15 | +import matplotlib.patheffects as path_effects |
| 16 | +from matplotlib.testing.decorators import check_figures_equal |
| 17 | + |
15 | 18 | import numpy as np |
16 | 19 | from matplotlib.rcsetup import ( |
17 | 20 | validate_bool, |
|
27 | 30 | validate_int, |
28 | 31 | validate_markevery, |
29 | 32 | validate_stringlist, |
| 33 | + validate_path_effects, |
30 | 34 | _validate_linestyle, |
31 | | - _listify_validator) |
| 35 | + _listify_validator, |
| 36 | + ) |
32 | 37 |
|
33 | 38 |
|
34 | 39 | def test_rcparams(tmpdir): |
@@ -628,3 +633,62 @@ def test_rcparams_legend_loc_from_file(tmpdir, value): |
628 | 633 |
|
629 | 634 | with mpl.rc_context(fname=rc_path): |
630 | 635 | assert mpl.rcParams["legend.loc"] == value |
| 636 | + |
| 637 | +ped = [('Normal', {}), |
| 638 | + ('Stroke', {'offset': (1, 2)}), |
| 639 | + ('withStroke', {'linewidth': 4, 'foreground': 'w'})] |
| 640 | + |
| 641 | +pel = [path_effects.Normal(), |
| 642 | + path_effects.Stroke((1, 2)), |
| 643 | + path_effects.withStroke(linewidth=4, foreground='w')] |
| 644 | + |
| 645 | + |
| 646 | +@pytest.mark.parametrize("value", [pel, ped], ids=["func", "dict"]) |
| 647 | +def test_path_effects(value): |
| 648 | + assert validate_path_effects(value) == value |
| 649 | + for v in value: |
| 650 | + assert validate_path_effects(value) == value |
| 651 | + |
| 652 | + |
| 653 | +def test_path_effects_string(): |
| 654 | + """test list of dicts properly parsed""" |
| 655 | + pstr = "('Normal', ), " |
| 656 | + pstr += "('Stroke', {'offset': (1, 2)})," |
| 657 | + pstr += "('withStroke', {'linewidth': 4, 'foreground': 'w'})" |
| 658 | + assert validate_path_effects(pstr) == ped |
| 659 | + |
| 660 | + |
| 661 | +@pytest.mark.parametrize("fdict, flist", |
| 662 | + [([ped[0]], [pel[0]]), |
| 663 | + ([ped[1]], [pel[1]]), |
| 664 | + ([ped[2]], [ped[2]]), |
| 665 | + (ped, pel)], |
| 666 | + ids=['function', 'args', 'kwargs', 'all']) |
| 667 | +@check_figures_equal() |
| 668 | +def test_path_effects_picture(fig_test, fig_ref, fdict, flist): |
| 669 | + with mpl.rc_context({'path.effects': fdict}): |
| 670 | + fig_test.subplots().plot([1, 2, 3]) |
| 671 | + |
| 672 | + with mpl.rc_context({'path.effects': flist}): |
| 673 | + fig_ref.subplots().plot([1, 2, 3]) |
| 674 | + |
| 675 | + |
| 676 | +@pytest.mark.parametrize("s, msg", [ |
| 677 | + ([1, 2, 3], "Expected a list of patheffects .*"), |
| 678 | + (("Happy", ), r".* \'Happy\' is not a valid value for path\.effects\.function.*"), |
| 679 | + (("Normal", [1, 2, 3]), r"Expected a dictionary .*"),]) |
| 680 | +def test_path_effect_errors(s, msg): |
| 681 | + with pytest.raises(ValueError, match=msg): |
| 682 | + mpl.rcParams['path.effects'] = s |
| 683 | + |
| 684 | + |
| 685 | +def test_path_effects_from_file(tmpdir): |
| 686 | + # rcParams['legend.loc'] should be settable from matplotlibrc. |
| 687 | + # if any of these are not allowed, an exception will be raised. |
| 688 | + # test for gh issue #22338 |
| 689 | + rc_path = tmpdir.join("matplotlibrc") |
| 690 | + rc_path.write("path.effects: ('Normal', {}), ('withStroke', {'linewidth': 2})") |
| 691 | + |
| 692 | + with mpl.rc_context(fname=rc_path): |
| 693 | + assert isinstance(mpl.rcParams["path.effects"][0], path_effects.Normal) |
| 694 | + assert isinstance(mpl.rcParams["path.effects"][1], path_effects.withStroke) |
0 commit comments