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

Skip to content

Commit 371f41d

Browse files
committed
Tighten return type of Axes.bar_label
Also, tweak tests to use canonical (and thus typed) getters.
1 parent 532d925 commit 371f41d

File tree

4 files changed

+38
-38
lines changed

4 files changed

+38
-38
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,8 +2745,8 @@ def bar_label(self, container, labels=None, *, fmt="%g", label_type="edge",
27452745
27462746
Returns
27472747
-------
2748-
list of `.Text`
2749-
A list of `.Text` instances for the labels.
2748+
list of `.Annotation`
2749+
A list of `.Annotation` instances for the labels.
27502750
"""
27512751
for key in ['horizontalalignment', 'ha', 'verticalalignment', 'va']:
27522752
if key in kwargs:

lib/matplotlib/axes/_axes.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class Axes(_AxesBase):
274274
label_type: Literal["center", "edge"] = ...,
275275
padding: float = ...,
276276
**kwargs
277-
) -> list[Text]: ...
277+
) -> list[Annotation]: ...
278278
def broken_barh(
279279
self,
280280
xranges: Sequence[tuple[float, float]],

lib/matplotlib/pyplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2728,7 +2728,7 @@ def bar_label(
27282728
label_type: Literal["center", "edge"] = "edge",
27292729
padding: float = 0,
27302730
**kwargs,
2731-
) -> list[Text]:
2731+
) -> list[Annotation]:
27322732
return gca().bar_label(
27332733
container,
27342734
labels=labels,

lib/matplotlib/tests/test_axes.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8154,11 +8154,11 @@ def test_bar_label_location_vertical():
81548154
rects = ax.bar(xs, heights)
81558155
labels = ax.bar_label(rects)
81568156
assert labels[0].xy == (xs[0], heights[0])
8157-
assert labels[0].get_ha() == 'center'
8158-
assert labels[0].get_va() == 'bottom'
8157+
assert labels[0].get_horizontalalignment() == 'center'
8158+
assert labels[0].get_verticalalignment() == 'bottom'
81598159
assert labels[1].xy == (xs[1], heights[1])
8160-
assert labels[1].get_ha() == 'center'
8161-
assert labels[1].get_va() == 'top'
8160+
assert labels[1].get_horizontalalignment() == 'center'
8161+
assert labels[1].get_verticalalignment() == 'top'
81628162

81638163

81648164
def test_bar_label_location_vertical_yinverted():
@@ -8168,11 +8168,11 @@ def test_bar_label_location_vertical_yinverted():
81688168
rects = ax.bar(xs, heights)
81698169
labels = ax.bar_label(rects)
81708170
assert labels[0].xy == (xs[0], heights[0])
8171-
assert labels[0].get_ha() == 'center'
8172-
assert labels[0].get_va() == 'top'
8171+
assert labels[0].get_horizontalalignment() == 'center'
8172+
assert labels[0].get_verticalalignment() == 'top'
81738173
assert labels[1].xy == (xs[1], heights[1])
8174-
assert labels[1].get_ha() == 'center'
8175-
assert labels[1].get_va() == 'bottom'
8174+
assert labels[1].get_horizontalalignment() == 'center'
8175+
assert labels[1].get_verticalalignment() == 'bottom'
81768176

81778177

81788178
def test_bar_label_location_horizontal():
@@ -8181,11 +8181,11 @@ def test_bar_label_location_horizontal():
81818181
rects = ax.barh(ys, widths)
81828182
labels = ax.bar_label(rects)
81838183
assert labels[0].xy == (widths[0], ys[0])
8184-
assert labels[0].get_ha() == 'left'
8185-
assert labels[0].get_va() == 'center'
8184+
assert labels[0].get_horizontalalignment() == 'left'
8185+
assert labels[0].get_verticalalignment() == 'center'
81868186
assert labels[1].xy == (widths[1], ys[1])
8187-
assert labels[1].get_ha() == 'right'
8188-
assert labels[1].get_va() == 'center'
8187+
assert labels[1].get_horizontalalignment() == 'right'
8188+
assert labels[1].get_verticalalignment() == 'center'
81898189

81908190

81918191
def test_bar_label_location_horizontal_yinverted():
@@ -8195,11 +8195,11 @@ def test_bar_label_location_horizontal_yinverted():
81958195
rects = ax.barh(ys, widths)
81968196
labels = ax.bar_label(rects)
81978197
assert labels[0].xy == (widths[0], ys[0])
8198-
assert labels[0].get_ha() == 'left'
8199-
assert labels[0].get_va() == 'center'
8198+
assert labels[0].get_horizontalalignment() == 'left'
8199+
assert labels[0].get_verticalalignment() == 'center'
82008200
assert labels[1].xy == (widths[1], ys[1])
8201-
assert labels[1].get_ha() == 'right'
8202-
assert labels[1].get_va() == 'center'
8201+
assert labels[1].get_horizontalalignment() == 'right'
8202+
assert labels[1].get_verticalalignment() == 'center'
82038203

82048204

82058205
def test_bar_label_location_horizontal_xinverted():
@@ -8209,11 +8209,11 @@ def test_bar_label_location_horizontal_xinverted():
82098209
rects = ax.barh(ys, widths)
82108210
labels = ax.bar_label(rects)
82118211
assert labels[0].xy == (widths[0], ys[0])
8212-
assert labels[0].get_ha() == 'right'
8213-
assert labels[0].get_va() == 'center'
8212+
assert labels[0].get_horizontalalignment() == 'right'
8213+
assert labels[0].get_verticalalignment() == 'center'
82148214
assert labels[1].xy == (widths[1], ys[1])
8215-
assert labels[1].get_ha() == 'left'
8216-
assert labels[1].get_va() == 'center'
8215+
assert labels[1].get_horizontalalignment() == 'left'
8216+
assert labels[1].get_verticalalignment() == 'center'
82178217

82188218

82198219
def test_bar_label_location_horizontal_xyinverted():
@@ -8224,11 +8224,11 @@ def test_bar_label_location_horizontal_xyinverted():
82248224
rects = ax.barh(ys, widths)
82258225
labels = ax.bar_label(rects)
82268226
assert labels[0].xy == (widths[0], ys[0])
8227-
assert labels[0].get_ha() == 'right'
8228-
assert labels[0].get_va() == 'center'
8227+
assert labels[0].get_horizontalalignment() == 'right'
8228+
assert labels[0].get_verticalalignment() == 'center'
82298229
assert labels[1].xy == (widths[1], ys[1])
8230-
assert labels[1].get_ha() == 'left'
8231-
assert labels[1].get_va() == 'center'
8230+
assert labels[1].get_horizontalalignment() == 'left'
8231+
assert labels[1].get_verticalalignment() == 'center'
82328232

82338233

82348234
def test_bar_label_location_center():
@@ -8237,11 +8237,11 @@ def test_bar_label_location_center():
82378237
rects = ax.barh(ys, widths)
82388238
labels = ax.bar_label(rects, label_type='center')
82398239
assert labels[0].xy == (0.5, 0.5)
8240-
assert labels[0].get_ha() == 'center'
8241-
assert labels[0].get_va() == 'center'
8240+
assert labels[0].get_horizontalalignment() == 'center'
8241+
assert labels[0].get_verticalalignment() == 'center'
82428242
assert labels[1].xy == (0.5, 0.5)
8243-
assert labels[1].get_ha() == 'center'
8244-
assert labels[1].get_va() == 'center'
8243+
assert labels[1].get_horizontalalignment() == 'center'
8244+
assert labels[1].get_verticalalignment() == 'center'
82458245

82468246

82478247
@image_comparison(['test_centered_bar_label_nonlinear.svg'])
@@ -8273,11 +8273,11 @@ def test_bar_label_location_errorbars():
82738273
rects = ax.bar(xs, heights, yerr=1)
82748274
labels = ax.bar_label(rects)
82758275
assert labels[0].xy == (xs[0], heights[0] + 1)
8276-
assert labels[0].get_ha() == 'center'
8277-
assert labels[0].get_va() == 'bottom'
8276+
assert labels[0].get_horizontalalignment() == 'center'
8277+
assert labels[0].get_verticalalignment() == 'bottom'
82788278
assert labels[1].xy == (xs[1], heights[1] - 1)
8279-
assert labels[1].get_ha() == 'center'
8280-
assert labels[1].get_va() == 'top'
8279+
assert labels[1].get_horizontalalignment() == 'center'
8280+
assert labels[1].get_verticalalignment() == 'top'
82818281

82828282

82838283
@pytest.mark.parametrize('fmt', [
@@ -8312,7 +8312,7 @@ def test_bar_label_nan_ydata():
83128312
labels = ax.bar_label(bars)
83138313
assert [l.get_text() for l in labels] == ['', '1']
83148314
assert labels[0].xy == (2, 0)
8315-
assert labels[0].get_va() == 'bottom'
8315+
assert labels[0].get_verticalalignment() == 'bottom'
83168316

83178317

83188318
def test_bar_label_nan_ydata_inverted():
@@ -8322,7 +8322,7 @@ def test_bar_label_nan_ydata_inverted():
83228322
labels = ax.bar_label(bars)
83238323
assert [l.get_text() for l in labels] == ['', '1']
83248324
assert labels[0].xy == (2, 0)
8325-
assert labels[0].get_va() == 'bottom'
8325+
assert labels[0].get_verticalalignment() == 'bottom'
83268326

83278327

83288328
def test_nan_barlabels():

0 commit comments

Comments
 (0)