Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6342bed

Browse files
committed
help
1 parenta44290f commit6342bed

File tree

5 files changed

+50
-87
lines changed

5 files changed

+50
-87
lines changed

‎lib/matplotlib/__init__.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,12 @@ def __getitem__(self, key):
765765
plt.switch_backend(rcsetup._auto_backend_sentinel)
766766

767767
elifkey=="path.effects"andselfisglobals().get("rcParams"):
768-
# to avoid circular imports
769-
returnself._load_path_effects()
768+
# defers loading of patheffects to avoid circular imports
769+
importmatplotlib.patheffectsaspath_effects
770+
771+
return [peifisinstance(pe,path_effects.AbstractPathEffect)
772+
elsegetattr(path_effects,pe[0])(**pe[1])
773+
forpeinself._get('path.effects')]
770774

771775
returnself._get(key)
772776

@@ -818,14 +822,6 @@ def copy(self):
818822
rccopy._set(k,self._get(k))
819823
returnrccopy
820824

821-
def_load_path_effects(self):
822-
"""defers loading of patheffects to avoid circular imports"""
823-
importmatplotlib.patheffectsaspath_effects
824-
825-
return [peifisinstance(pe,path_effects.AbstractPathEffect)
826-
elsegetattr(path_effects,pe.pop('name'))(**pe)
827-
forpeincopy.deepcopy(self._get('path.effects'))]
828-
829825

830826
defrc_params(fail_on_error=False):
831827
"""Construct a `RcParams` instance from the default Matplotlib rc file."""

‎lib/matplotlib/mpl-data/stylelib/xkcd.mplstyle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ figure.facecolor: white
2727

2828
# path
2929
path.sketch : 1, 100, 2
30-
path.effects:{'name': 'withStroke', 'linewidth': 4, 'foreground': 'w' }
30+
path.effects:('withStroke',{'linewidth': 4, 'foreground': 'w' })

‎lib/matplotlib/rcsetup.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,6 @@ def validate_any(s):
133133
validate_anylist=_listify_validator(validate_any)
134134

135135

136-
defvalidate_anydict(allow_none=True,required_keys=None):
137-
"""Validate dictionary, check if keys are missing"""
138-
139-
required_keys=required_keysifrequired_keyselseset()
140-
141-
def_validate_dict(d):
142-
try:
143-
d=ast.literal_eval(d)
144-
exceptValueError:
145-
pass
146-
147-
ifallow_noneanddisNone:
148-
returnd
149-
150-
ifisinstance(d,dict):
151-
ifmissing_keys:= (required_keys-d.keys()):
152-
raiseValueError(f"Missing required key:{missing_keys!r}")
153-
returnd
154-
155-
raiseValueError(f"Input{d!r} must be a dictionary {{'k': v}} "
156-
f"{'or None'ifallow_noneelse''}")
157-
158-
return_validate_dict
159-
160-
161136
def_validate_date(s):
162137
try:
163138
np.datetime64(s)
@@ -599,18 +574,39 @@ def validate_path_effects(s):
599574
ifnots:
600575
return []
601576

602-
_validate=validate_anydict(allow_none=True,required_keys={'name'})
603-
# string list of dict {k1: 1, k2:2}, {k1:2}
604-
# validate_anylist relies on , for parsing so parse here instead
605-
ifisinstance(s,str)ands.startswith("{"):
577+
ifisinstance(s,str)ands.startswith("("):
606578
s=ast.literal_eval(s)
607-
ifisinstance(s,dict):
608-
# list of one dict
609-
return_validate(s)
610579

611-
return [peifgetattr(pe,'__module__',"")=='matplotlib.patheffects'
612-
else_validate(pe)forpeinvalidate_anylist(s)]
580+
ifnotisinstance(s,list):#string tuple list mostly
581+
s= [s]
582+
583+
_validate_name=ValidateInStrings("path.effects",
584+
["Normal",
585+
"PathPatchEffect",
586+
"SimpleLineShadow",
587+
"SimplePatchShadow",
588+
"Stroke",
589+
"TickedStroke",
590+
"withSimplePatchShadow",
591+
"withStroke",
592+
"withTickedStroke"])
593+
path_effects= []
594+
595+
forpeins:
596+
#patheffects objects
597+
ifgetattr(pe,'__module__',"")=='matplotlib.patheffects':
598+
path_effects.append(pe)
599+
continue
600+
601+
ifnotisinstance(pe, (tuple)):
602+
raiseValueError("Expected a list of tuples of the form: ('function name', {**kwargs})")
603+
604+
iflen(pe)==1:
605+
pe== (pe[0], {})
606+
path_effects.append((_validate_name(pe[0]),pe[1]))
607+
613608

609+
returnpath_effects
614610

615611
def_validate_greaterthan_minushalf(s):
616612
s=validate_float(s)

‎lib/matplotlib/rcsetup.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class ValidateInStrings:
2929

3030
defvalidate_any(s:Any)->Any: ...
3131
defvalidate_anylist(s:Any)->list[Any]: ...
32-
defvalidate_anydict(allow_none:bool=True,required_keys:set[str]|None=None
33-
)->Callable[[dict[str,Any]|None],dict[str,Any]]: ...
3432
defvalidate_bool(b:Any)->bool: ...
3533
defvalidate_axisbelow(s:Any)->bool|Literal["line"]: ...
3634
defvalidate_dpi(s:Any)->Literal["figure"]|float: ...
@@ -143,7 +141,8 @@ def _validate_linestyle(s: Any) -> LineStyleType: ...
143141
defvalidate_markeverylist(s:Any)->list[MarkEveryType]: ...
144142
defvalidate_bbox(s:Any)->Literal["tight","standard"]|None: ...
145143
defvalidate_sketch(s:Any)->None|tuple[float,float,float]: ...
146-
defvalidate_path_effects(s:Any)->list[AbstractPathEffect]|list[dict]: ...
144+
defvalidate_path_effects(s:str|list[AbstractPathEffect,Tuple[str,dict]])
145+
->list[AbstractPathEffect| [Tuple[str,dict]]]: ...
147146
defvalidate_hatch(s:Any)->str: ...
148147
defvalidate_hatchlist(s:Any)->list[str]: ...
149148
defvalidate_dashlist(s:Any)->list[list[float]]: ...

‎lib/matplotlib/tests/test_rcparams.py

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
importnumpyasnp
1919
frommatplotlib.rcsetupimport (
20-
validate_anydict,
2120
validate_bool,
2221
validate_color,
2322
validate_colorlist,
@@ -635,35 +634,9 @@ def test_rcparams_legend_loc_from_file(tmpdir, value):
635634
withmpl.rc_context(fname=rc_path):
636635
assertmpl.rcParams["legend.loc"]==value
637636

638-
639-
@pytest.mark.parametrize("allow_none", [True,False])
640-
deftest_validate_dict(allow_none):
641-
fval=validate_anydict(allow_none)
642-
assertfval("{'a': 1, 'b': 2}")== {'a':1,'b':2}
643-
withpytest.raises(ValueError,match=r"Input \['a', 'b'\] "):
644-
fval(['a','b'])
645-
646-
fval=validate_anydict(allow_none,required_keys={'a'})
647-
assertfval({'a':1})== {'a':1}
648-
withpytest.raises(ValueError,match="Missing required key: {'a'}"):
649-
fval({'b':1})
650-
651-
652-
deftest_validate_dict_none():
653-
assertvalidate_anydict()(None)isNone
654-
assertvalidate_anydict(required_keys={'a'})(None)isNone
655-
656-
withpytest.raises(ValueError,
657-
match=r"Input None must be a dictionary "):
658-
validate_anydict(False)(None)
659-
withpytest.raises(ValueError,
660-
match=r"Input 0 must be a dictionary {'k': v} or None"):
661-
validate_anydict(True)(0)
662-
663-
664-
ped= [{'name':'Normal'},
665-
{'name':'Stroke','offset': (1,2)},
666-
{'name':'withStroke','linewidth':4,'foreground':'w'}]
637+
ped= [('Normal', {}),
638+
('Stroke', {'offset': (1,2)}),
639+
('withStroke', {'linewidth':4,'foreground':'w'})]
667640

668641
pel= [path_effects.Normal(),
669642
path_effects.Stroke((1,2)),
@@ -677,11 +650,11 @@ def test_path_effects(value):
677650
assertvalidate_path_effects(value)==value
678651

679652

680-
deftest_path_effects_string_dict():
653+
deftest_path_effects_string():
681654
"""test list of dicts properly parsed"""
682-
pstr="{'name': 'Normal'},"
683-
pstr+="{'name': 'Stroke', 'offset': (1, 2)},"
684-
pstr+="{'name': 'withStroke', 'linewidth': 4, 'foreground': 'w'}"
655+
pstr="('Normal', ),"
656+
pstr+="('Stroke',: {'offset': (1, 2)}),"
657+
pstr+="('withStroke',{'linewidth': 4, 'foreground': 'w'})"
685658
assertvalidate_path_effects(pstr)==ped
686659

687660

@@ -701,10 +674,10 @@ def test_path_effects_picture(fig_test, fig_ref, fdict, flist):
701674

702675

703676
deftest_path_effect_errors():
704-
withpytest.raises(ValueError,match="Missing required key: {'name'}"):
705-
mpl.rcParams['path.effects']= [{'kwargs':{1,2,3}}]
677+
withpytest.raises(ValueError,match=r"Key path.effects: 'Happy'"):
678+
mpl.rcParams['path.effects']= [("Happy", {'a':1})]
706679

707-
withpytest.raises(ValueError,match=r"Key path.effects: Input 1"):
680+
withpytest.raises(ValueError,match="Expected a list of tuples of the form: ('function name', {**kwargs})"):
708681
mpl.rcParams['path.effects']= [1,2,3]
709682

710683

@@ -713,8 +686,7 @@ def test_path_effects_from_file(tmpdir):
713686
# if any of these are not allowed, an exception will be raised.
714687
# test for gh issue #22338
715688
rc_path=tmpdir.join("matplotlibrc")
716-
rc_path.write("path.effects: "
717-
"{'name': 'Normal'}, {'name': 'withStroke', 'linewidth': 2}")
689+
rc_path.write("path.effects: ('Normal', {}), ('withStroke', {'linewidth': 2})")
718690

719691
withmpl.rc_context(fname=rc_path):
720692
assertisinstance(mpl.rcParams["path.effects"][0],path_effects.Normal)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp