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

Skip to content

Backport PR #12572 on branch v3.0.x (Fix singleton hist labels) #12594

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

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
2 changes: 2 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6759,6 +6759,8 @@ def hist(self, x, bins=None, range=None, density=None, weights=None,
labels = [None]
elif isinstance(label, str):
labels = [label]
elif not np.iterable(label):
labels = [str(label)]
else:
labels = [str(lab) for lab in label]

Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3065,6 +3065,21 @@ def test_hist_emptydata():
ax.hist([[], range(10), range(10)], histtype="step")


def test_hist_labels():
# test singleton labels OK
fig, ax = plt.subplots()
l = ax.hist([0, 1], label=0)
assert l[2][0].get_label() == '0'
l = ax.hist([0, 1], label=[0])
assert l[2][0].get_label() == '0'
l = ax.hist([0, 1], label=None)
assert l[2][0].get_label() == '_nolegend_'
l = ax.hist([0, 1], label='0')
assert l[2][0].get_label() == '0'
l = ax.hist([0, 1], label='00')
assert l[2][0].get_label() == '00'


@image_comparison(baseline_images=['transparent_markers'], remove_text=True)
def test_transparent_markers():
np.random.seed(0)
Expand Down