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

Skip to content

Commit ba5d92e

Browse files
committed
Move {get,set}_{x,y}label to _AxesBase.
This lets SecondaryAxes also use it rather than having to reimplement it (and do so only partially: previously SecondaryAxes did not support the `loc` kwarg).
1 parent 80eabbc commit ba5d92e

3 files changed

Lines changed: 108 additions & 156 deletions

File tree

lib/matplotlib/axes/_axes.py

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -192,112 +192,6 @@ def set_title(self, label, fontdict=None, loc=None, pad=None, *, y=None,
192192
title.update(kwargs)
193193
return title
194194

195-
def get_xlabel(self):
196-
"""
197-
Get the xlabel text string.
198-
"""
199-
label = self.xaxis.get_label()
200-
return label.get_text()
201-
202-
def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *,
203-
loc=None, **kwargs):
204-
"""
205-
Set the label for the x-axis.
206-
207-
Parameters
208-
----------
209-
xlabel : str
210-
The label text.
211-
212-
labelpad : float, default: None
213-
Spacing in points from the axes bounding box including ticks
214-
and tick labels.
215-
216-
loc : {'left', 'center', 'right'}, default: :rc:`xaxis.labellocation`
217-
The label position. This is a high-level alternative for passing
218-
parameters *x* and *horizontalalignment*.
219-
220-
Other Parameters
221-
----------------
222-
**kwargs : `.Text` properties
223-
`.Text` properties control the appearance of the label.
224-
225-
See Also
226-
--------
227-
text : Documents the properties supported by `.Text`.
228-
"""
229-
if labelpad is not None:
230-
self.xaxis.labelpad = labelpad
231-
protected_kw = ['x', 'horizontalalignment', 'ha']
232-
if {*kwargs} & {*protected_kw}:
233-
if loc is not None:
234-
raise TypeError(f"Specifying 'loc' is disallowed when any of "
235-
f"its corresponding low level keyword "
236-
f"arguments ({protected_kw}) are also "
237-
f"supplied")
238-
loc = 'center'
239-
else:
240-
loc = loc if loc is not None else rcParams['xaxis.labellocation']
241-
cbook._check_in_list(('left', 'center', 'right'), loc=loc)
242-
if loc == 'left':
243-
kwargs.update(x=0, horizontalalignment='left')
244-
elif loc == 'right':
245-
kwargs.update(x=1, horizontalalignment='right')
246-
return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
247-
248-
def get_ylabel(self):
249-
"""
250-
Get the ylabel text string.
251-
"""
252-
label = self.yaxis.get_label()
253-
return label.get_text()
254-
255-
def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *,
256-
loc=None, **kwargs):
257-
"""
258-
Set the label for the y-axis.
259-
260-
Parameters
261-
----------
262-
ylabel : str
263-
The label text.
264-
265-
labelpad : float, default: None
266-
Spacing in points from the axes bounding box including ticks
267-
and tick labels.
268-
269-
loc : {'bottom', 'center', 'top'}, default: :rc:`yaxis.labellocation`
270-
The label position. This is a high-level alternative for passing
271-
parameters *y* and *horizontalalignment*.
272-
273-
Other Parameters
274-
----------------
275-
**kwargs : `.Text` properties
276-
`.Text` properties control the appearance of the label.
277-
278-
See Also
279-
--------
280-
text : Documents the properties supported by `.Text`.
281-
"""
282-
if labelpad is not None:
283-
self.yaxis.labelpad = labelpad
284-
protected_kw = ['y', 'horizontalalignment', 'ha']
285-
if {*kwargs} & {*protected_kw}:
286-
if loc is not None:
287-
raise TypeError(f"Specifying 'loc' is disallowed when any of "
288-
f"its corresponding low level keyword "
289-
f"arguments ({protected_kw}) are also "
290-
f"supplied")
291-
loc = 'center'
292-
else:
293-
loc = loc if loc is not None else rcParams['yaxis.labellocation']
294-
cbook._check_in_list(('bottom', 'center', 'top'), loc=loc)
295-
if loc == 'bottom':
296-
kwargs.update(y=0, horizontalalignment='left')
297-
elif loc == 'top':
298-
kwargs.update(y=1, horizontalalignment='right')
299-
return self.yaxis.set_label_text(ylabel, fontdict, **kwargs)
300-
301195
def get_legend_handles_labels(self, legend_handler_map=None):
302196
"""
303197
Return handles and labels for legend

lib/matplotlib/axes/_base.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,6 +3127,60 @@ def set_axis_on(self):
31273127

31283128
# data limits, ticks, tick labels, and formatting
31293129

3130+
def get_xlabel(self):
3131+
"""
3132+
Get the xlabel text string.
3133+
"""
3134+
label = self.xaxis.get_label()
3135+
return label.get_text()
3136+
3137+
def set_xlabel(self, xlabel, fontdict=None, labelpad=None, *,
3138+
loc=None, **kwargs):
3139+
"""
3140+
Set the label for the x-axis.
3141+
3142+
Parameters
3143+
----------
3144+
xlabel : str
3145+
The label text.
3146+
3147+
labelpad : float, default: None
3148+
Spacing in points from the axes bounding box including ticks
3149+
and tick labels.
3150+
3151+
loc : {'left', 'center', 'right'}, default: :rc:`xaxis.labellocation`
3152+
The label position. This is a high-level alternative for passing
3153+
parameters *x* and *horizontalalignment*.
3154+
3155+
Other Parameters
3156+
----------------
3157+
**kwargs : `.Text` properties
3158+
`.Text` properties control the appearance of the label.
3159+
3160+
See Also
3161+
--------
3162+
text : Documents the properties supported by `.Text`.
3163+
"""
3164+
if labelpad is not None:
3165+
self.xaxis.labelpad = labelpad
3166+
protected_kw = ['x', 'horizontalalignment', 'ha']
3167+
if {*kwargs} & {*protected_kw}:
3168+
if loc is not None:
3169+
raise TypeError(f"Specifying 'loc' is disallowed when any of "
3170+
f"its corresponding low level keyword "
3171+
f"arguments ({protected_kw}) are also "
3172+
f"supplied")
3173+
loc = 'center'
3174+
else:
3175+
loc = (loc if loc is not None
3176+
else mpl.rcParams['xaxis.labellocation'])
3177+
cbook._check_in_list(('left', 'center', 'right'), loc=loc)
3178+
if loc == 'left':
3179+
kwargs.update(x=0, horizontalalignment='left')
3180+
elif loc == 'right':
3181+
kwargs.update(x=1, horizontalalignment='right')
3182+
return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
3183+
31303184
def invert_xaxis(self):
31313185
"""
31323186
Invert the x-axis.
@@ -3416,6 +3470,60 @@ def set_xscale(self, value, **kwargs):
34163470
"xaxis", "_set_ticklabels",
34173471
doc_sub={"Axis.set_ticks": "Axes.set_xticks"})
34183472

3473+
def get_ylabel(self):
3474+
"""
3475+
Get the ylabel text string.
3476+
"""
3477+
label = self.yaxis.get_label()
3478+
return label.get_text()
3479+
3480+
def set_ylabel(self, ylabel, fontdict=None, labelpad=None, *,
3481+
loc=None, **kwargs):
3482+
"""
3483+
Set the label for the y-axis.
3484+
3485+
Parameters
3486+
----------
3487+
ylabel : str
3488+
The label text.
3489+
3490+
labelpad : float, default: None
3491+
Spacing in points from the axes bounding box including ticks
3492+
and tick labels.
3493+
3494+
loc : {'bottom', 'center', 'top'}, default: :rc:`yaxis.labellocation`
3495+
The label position. This is a high-level alternative for passing
3496+
parameters *y* and *horizontalalignment*.
3497+
3498+
Other Parameters
3499+
----------------
3500+
**kwargs : `.Text` properties
3501+
`.Text` properties control the appearance of the label.
3502+
3503+
See Also
3504+
--------
3505+
text : Documents the properties supported by `.Text`.
3506+
"""
3507+
if labelpad is not None:
3508+
self.yaxis.labelpad = labelpad
3509+
protected_kw = ['y', 'horizontalalignment', 'ha']
3510+
if {*kwargs} & {*protected_kw}:
3511+
if loc is not None:
3512+
raise TypeError(f"Specifying 'loc' is disallowed when any of "
3513+
f"its corresponding low level keyword "
3514+
f"arguments ({protected_kw}) are also "
3515+
f"supplied")
3516+
loc = 'center'
3517+
else:
3518+
loc = (loc if loc is not None
3519+
else mpl.rcParams['yaxis.labellocation'])
3520+
cbook._check_in_list(('bottom', 'center', 'top'), loc=loc)
3521+
if loc == 'bottom':
3522+
kwargs.update(y=0, horizontalalignment='left')
3523+
elif loc == 'top':
3524+
kwargs.update(y=1, horizontalalignment='right')
3525+
return self.yaxis.set_label_text(ylabel, fontdict, **kwargs)
3526+
34193527
def invert_yaxis(self):
34203528
"""
34213529
Invert the y-axis.

lib/matplotlib/axes/_secondary_axes.py

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -250,56 +250,6 @@ def set_aspect(self, *args, **kwargs):
250250
"""
251251
cbook._warn_external("Secondary axes can't set the aspect ratio")
252252

253-
def set_xlabel(self, xlabel, fontdict=None, labelpad=None, **kwargs):
254-
"""
255-
Set the label for the x-axis.
256-
257-
Parameters
258-
----------
259-
xlabel : str
260-
The label text.
261-
262-
labelpad : float, default: ``self.xaxis.labelpad``
263-
Spacing in points between the label and the x-axis.
264-
265-
Other Parameters
266-
----------------
267-
**kwargs : `.Text` properties
268-
`.Text` properties control the appearance of the label.
269-
270-
See Also
271-
--------
272-
text : Documents the properties supported by `.Text`.
273-
"""
274-
if labelpad is not None:
275-
self.xaxis.labelpad = labelpad
276-
return self.xaxis.set_label_text(xlabel, fontdict, **kwargs)
277-
278-
def set_ylabel(self, ylabel, fontdict=None, labelpad=None, **kwargs):
279-
"""
280-
Set the label for the y-axis.
281-
282-
Parameters
283-
----------
284-
ylabel : str
285-
The label text.
286-
287-
labelpad : float, default: ``self.yaxis.labelpad``
288-
Spacing in points between the label and the y-axis.
289-
290-
Other Parameters
291-
----------------
292-
**kwargs : `.Text` properties
293-
`.Text` properties control the appearance of the label.
294-
295-
See Also
296-
--------
297-
text : Documents the properties supported by `.Text`.
298-
"""
299-
if labelpad is not None:
300-
self.yaxis.labelpad = labelpad
301-
return self.yaxis.set_label_text(ylabel, fontdict, **kwargs)
302-
303253
def set_color(self, color):
304254
"""
305255
Change the color of the secondary axes and all decorators.

0 commit comments

Comments
 (0)