8000 ENH: Validate kwargs in Axis.set_ticks() · matplotlib/matplotlib@86c94c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86c94c1

Browse files
committed
ENH: Validate kwargs in Axis.set_ticks()
1 parent 25d30c2 commit 86c94c1

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

lib/matplotlib/axis.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,19 @@ def set_ticks(self, ticks, labels=None, *, minor=False, **kwargs):
20292029
other limits, you should set the limits explicitly after setting the
20302030
ticks.
20312031
"""
2032+
if labels is None and kwargs:
2033+
raise Exception('labels keyword argument cannot be None when '
2034+
'kwargs are passed')
2035+
text_properties = []
2036+
for attribute in dir(mtext.Text()):
2037+
if attribute.startswith('set_'):
2038+
if attribute not in ('set_name', 'set_fontname'):
2039+
text_properties.append(attribute[4:])
2040+
for kw in kwargs:
2041+
if kw not in text_properties:
2042+
raise Exception(f"kwarg '{kw}' not a property of "
2043+
"matplotlib.text.Text")
2044+
20322045
result = self._set_tick_locations(ticks, minor=minor)
20332046
if labels is not None:
20342047
self.set_ticklabels(labels, minor=minor, **kwargs)
Original file line numberDiff line numberDiff line change
@@ -5732,6 +5732,83 @@ def test_set_get_ticklabels():
57325732
ax[1].set_yticklabels(ax[0].get_yticklabels())
57335733

57345734

5735+
def test_set_ticks_kwarg_validation():
5736+
"""
5737+
Only matplotlib.text.Text properties are valid kwargs in axis.set_ticks.
5738+
"""
5739+
fig, ax = plt.subplots()
5740+
ticks = [1, 2, 3]
5741+
ax.set_xticks(ticks, labels=['1', '2', '3'],
5742+
agg_filter=0,
5743+
alpha=0.5,
5744+
animated=False,
5745+
backgroundcolor='blue',
5746+
bbox=None,
5747+
clip_box=None,
5748+
clip_on=None,
5749+
clip_path=None,
5750+
c='blue',
5751+
color='blue',
5752+
figure=fig,
5753+
fontfamily='serif',
5754+
family='serif',
5755+
fontproperties=None,
5756+
font=None,
5757+
font_properties=None,
5758+
fontsize='small',
5759+
size='small',
5760+
fontstretch=100,
5761+
stretch='condensed',
5762+
fontstyle='normal',
5763+
style='normal',
5764+
fontvariant='normal',
5765+
variant='normal',
5766+
fontweight='500',
5767+
weight='medium',
5768+
gid=None,
5769+
horizontalalignment='center',
5770+
ha='center',
5771+
in_layout=True,
5772+
label='label',
5773+
linespacing=1.0,
5774+
math_fontfamily='cm',
5775+
mouseover=False,
5776+
multialignment='center',
5777+
ma='center',
5778+
parse_math=False,
5779+
path_effects=None,
5780+
picker=None,
5781+
position=(1.0, 1.0),
5782+
rasterized=True,
5783+
rotation=0.0,
5784+
rotation_mode=None,
5785+
sketch_params=(1.0, 1.0, 1.0),
5786+
snap=None,
5787+
text='text',
5788+
transform=None,
5789+
transform_rotates_text=False,
5790+
url='',
5791+
usetex=False,
5792+
verticalalignment='center',
5793+
va='center',
5794+
visible=True,
5795+
wrap=True,
5796+
x=1.0,
5797+
y=1.0,
5798+
zorder=1.0)
5799+
5800+
5801+
def test_set_ticks_kwargs_raise_error_without_labels():
5802+
"""
5803+
When labels=None and any kwarg is passed, axis.set_ticks raises an
5804+
Exception.
5805+
"""
5806+
fig, ax = plt.subplots()
5807+
ticks = [1, 2, 3]
5808+
with pytest.raises(Exception):
5809+
ax.set_ticks(ticks, alpha=0.5)
5810+
5811+
57355812
@check_figures_equal(extensions=["png"])
57365813
def test_set_ticks_with_labels(fig_test, fig_ref):
57375814
"""
0