Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2712,7 +2712,13 @@ def sign(x):
value = extrema

if label_type == "center":
xy = xc, yc
xy = (0.5, 0.5)
kwargs["xycoords"] = (
lambda r, b=bar:
mtransforms.Bbox.intersection(
b.get_window_extent(r), b.get_clip_box()
)
)
else: # edge
if orientation == "vertical":
xy = xc, endpt
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 12 additions & 2 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7741,14 +7741,24 @@ def test_bar_label_location_center():
ys, widths = [1, 2], [3, -4]
rects = ax.barh(ys, widths)
labels = ax.bar_label(rects, label_type='center')
assert labels[0].xy == (widths[0] / 2, ys[0])
assert labels[0].xy == (0.5, 0.5)
assert labels[0].get_ha() == 'center'
assert labels[0].get_va() == 'center'
assert labels[1].xy == (widths[1] / 2, ys[1])
assert labels[1].xy == (0.5, 0.5)
assert labels[1].get_ha() == 'center'
assert labels[1].get_va() == 'center'


@image_comparison(['test_centered_bar_label_nonlinear.svg'])
def test_centered_bar_label_nonlinear():
_, ax = plt.subplots()
bar_container = ax.barh(['c', 'b', 'a'], [1_000, 5_000, 7_000])
ax.set_xscale('log')
ax.set_xlim(1, None)
ax.bar_label(bar_container, label_type='center')
ax.set_axis_off()


def test_bar_label_location_errorbars():
ax = plt.gca()
xs, heights = [1, 2], [3, -4]
Expand Down