-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Improve bar_label annotation #22447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve bar_label annotation #22447
Changes from all commits
09fa965
2b77283
9bc4c41
4560691
fae56a0
a64db70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2598,13 +2598,25 @@ def bar_label(self, container, labels=None, *, fmt="%g", label_type="edge", | |
|
||
**kwargs | ||
Any remaining keyword arguments are passed through to | ||
`.Axes.annotate`. | ||
`.Axes.annotate`. The alignment parameters ( | ||
*horizontalalignment* / *ha*, *verticalalignment* / *va*) are | ||
not supported because the labels are automatically aligned to | ||
the bars. | ||
|
||
Returns | ||
------- | ||
list of `.Text` | ||
A list of `.Text` instances for the labels. | ||
""" | ||
for key in ['horizontalalignment', 'ha', 'verticalalignment', 'va']: | ||
if key in kwargs: | ||
raise ValueError( | ||
f"Passing {key!r} to bar_label() is not supported.") | ||
|
||
a, b = self.yaxis.get_view_interval() | ||
y_inverted = a > b | ||
c, d = self.xaxis.get_view_interval() | ||
x_inverted = c > d | ||
|
||
# want to know whether to put label on positive or negative direction | ||
# cannot use np.sign here because it will return 0 if x == 0 | ||
|
@@ -2633,7 +2645,7 @@ def sign(x): | |
annotations = [] | ||
|
||
for bar, err, dat, lbl in itertools.zip_longest( | ||
bars, errs, datavalues, labels | ||
bars, errs, datavalues, labels | ||
): | ||
(x0, y0), (x1, y1) = bar.get_bbox().get_points() | ||
xc, yc = (x0 + x1) / 2, (y0 + y1) / 2 | ||
|
@@ -2665,18 +2677,26 @@ def sign(x): | |
xy = endpt, yc | ||
|
||
if orientation == "vertical": | ||
xytext = 0, sign(dat) * padding | ||
y_direction = -1 if y_inverted else 1 | ||
xytext = 0, y_direction * sign(dat) * padding | ||
else: | ||
xytext = sign(dat) * padding, 0 | ||
x_direction = -1 if x_inverted else 1 | ||
xytext = x_direction * sign(dat) * padding, 0 | ||
|
||
if label_type == "center": | ||
ha, va = "center", "center" | ||
elif label_type == "edge": | ||
if orientation == "vertical": | ||
ha = 'center' | ||
va = 'top' if dat < 0 else 'bottom' # also handles NaN | ||
if y_inverted: | ||
va = 'top' if dat > 0 else 'bottom' # also handles NaN | ||
else: | ||
va = 'top' if dat < 0 else 'bottom' # also handles NaN | ||
Comment on lines
+2691
to
+2694
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not entirely certain this is correct, because there are no tests with NaN on an inverted axis. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Valdes-Tresanco-MS di this get addressed? If so, please move back out of draft. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a new test added, but the problem is that the result that is being checked is the same as what occurs when you run the code on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what I guess I'm saying is it would be better if the test were paramterized across all the 4 cases here. |
||
elif orientation == "horizontal": | ||
ha = 'right' if dat < 0 else 'left' # also handles NaN | ||
if x_inverted: | ||
ha = 'right' if dat > 0 else 'left' # also handles NaN | ||
else: | ||
ha = 'right' if dat < 0 else 'left' # also handles NaN | ||
va = 'center' | ||
|
||
if np.isnan(dat): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might alternatively written as
But no strong preference. I'll leave this up to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That should be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merged as is since the existing code style is good enough.