Skip to content

Commit 21da4c1

Browse files
committed
resolve suggested improvements
1 parent 3b70738 commit 21da4c1

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,7 +2381,12 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
23812381

23822382
self._request_autoscale_view()
23832383

2384-
bar_container = BarContainer(patches, errorbar,
2384+
if orientation == 'vertical':
2385+
datavalues = height
2386+
elif orientation == 'horizontal':
2387+
datavalues = width
2388+
2389+
bar_container = BarContainer(patches, errorbar, datavalues=datavalues,
23852390
orientation=orientation, label=label)
23862391
self.add_container(bar_container)
23872392

@@ -2509,7 +2514,8 @@ def bar_label(self, container, labels=None, *, fmt="%g", label_type="edge",
25092514
Parameters
25102515
----------
25112516
container : `.BarContainer`
2512-
Container with all the bars and optionally errorbars.
2517+
Container with all the bars and optionally errorbars, likely
2518+
returned from `.bar` or `.barh`.
25132519
25142520
labels : array-like, optional
25152521
A list of label texts, that should be displayed. If not given, the
@@ -2519,7 +2525,7 @@ def bar_label(self, container, labels=None, *, fmt="%g", label_type="edge",
25192525
A format string for the label.
25202526
25212527
label_type : {'edge', 'center'}, default: 'edge'
2522-
The label type, Possible values:
2528+
The label type. Possible values:
25232529
25242530
- 'edge': label placed at the end-point of the bar segment, and the
25252531
value displayed will be the position of that end-point.
@@ -2535,7 +2541,7 @@ def bar_label(self, container, labels=None, *, fmt="%g", label_type="edge",
25352541
25362542
Returns
25372543
-------
2538-
annotations
2544+
list of `.Text`
25392545
A list of `.Text` instances for the labels.
25402546
"""
25412547

@@ -2548,28 +2554,34 @@ def sign(x):
25482554

25492555
bars = container.patches
25502556
errorbar = container.errorbar
2557+
datavalues = container.datavalues
25512558
orientation = container.orientation
25522559

25532560
if errorbar:
2554-
lines = errorbar.lines
2555-
barlinecols = lines[2]
2556-
barlinecol = barlinecols[0]
2561+
# check "ErrorbarContainer" for the definition of these elements
2562+
lines = errorbar.lines # attribute of "ErrorbarContainer" (tuple)
2563+
barlinecols = lines[2] # 0: data_line, 1: caplines, 2: barlinecols
2564+
barlinecol = barlinecols[0] # the "LineCollection" of error bars
25572565
errs = barlinecol.get_segments()
25582566
else:
25592567
errs = []
2560-
lbls = [] if labels is None else labels
2568+
2569+
if labels is None:
2570+
labels = []
25612571

25622572
annotations = []
2563-
for bar, err, lbl in itertools.zip_longest(bars, errs, lbls):
25642573

2574+
for bar, err, dat, lbl in itertools.zip_longest(
2575+
bars, errs, datavalues, labels
2576+
):
25652577
(x0, y0), (x1, y1) = bar.get_bbox().get_points()
25662578
xc, yc = (x0 + x1) / 2, (y0 + y1) / 2
25672579

25682580
if orientation == "vertical":
2569-
extrema = max(y0, y1) if yc >= 0 else min(y0, y1)
2581+
extrema = max(y0, y1) if dat >= 0 else min(y0, y1)
25702582
length = abs(y0 - y1)
25712583
elif orientation == "horizontal":
2572-
extrema = max(x0, x1) if xc >= 0 else min(x0, x1)
2584+
extrema = max(x0, x1) if dat >= 0 else min(x0, x1)
25732585
length = abs(x0 - x1)
25742586

25752587
if err is None:

lib/matplotlib/container.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,11 @@ class BarContainer(Container):
6262
6363
"""
6464

65-
def __init__(self, patches, errorbar=None, *, orientation=None, **kwargs):
65+
def __init__(self, patches, errorbar=None, *, datavalues=None,
66+
orientation=None, **kwargs):
6667
self.patches = patches
6768
self.errorbar = errorbar
69+
self.datavalues = datavalues
6870
self.orientation = orientation
6971
super().__init__(patches, **kwargs)
7072

0 commit comments

Comments
 (0)