Fix singleton hist labels by jklymak · Pull Request #12572 · matplotlib/matplotlib · GitHub
Skip to content

Fix singleton hist labels - #12572

Merged
dstansby merged 2 commits into
matplotlib:masterfrom
jklymak:fix-singleton-hist-labels
Oct 22, 2018
Merged

dstansby merged 2 commits into
matplotlib:masterfrom
jklymak:fix-singleton-hist-labels

Conversation

@jklymak

@jklymak jklymak commented Oct 19, 2018

Copy link
Copy Markdown
Member

PR Summary

Re-closes #3869

import matplotlib.pyplot as plt

plt.hist([0, 1], label=0)  # used to error
plt.hist([0, 1], label='00')  # works fine
plt.plot([0, 1], label=[0])  # works fine
plt.hist([[0, 1], [0, 2]], label=[0, 2])  # works fine

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@jklymak jklymak added this to the v3.0.x milestone Oct 19, 2018
@jklymak
jklymak force-pushed the fix-singleton-hist-labels branch 5 times, most recently from 61d3a37 to 819b99d Compare October 20, 2018 03:29
Comment thread lib/matplotlib/axes/_axes.py Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather keep it on one level:

        if label is 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]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure - I was hoping it'd have been even simpler, but str(None) returns "None", which ruined my plans.

@jklymak
jklymak force-pushed the fix-singleton-hist-labels branch from 819b99d to f38b1c9 Compare October 20, 2018 16:12

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can combine this case with the one above it

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately strings are iterable, so I don't think I can...

In [1]: boo = 'who'

In [2]: import numpy as np

In [3]: np.iterable(boo)
Out[3]: True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly the code required to make int labels work.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake, I thought np.iterable ignored strings. np.ndim(boo) == 0 would work, but is perhaps slower.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.ndim looks useful. I quite readily get confused with the best way to test if something is a list, ndarray, or singleton, and our codebase uses a few incantations. I would be fine w/ changing this to something that is an agreed-upon standard way to check. This logic only gets called when a hist is added to the axes, so I don't think we need to worry overly much about performance.

@dstansby
dstansby merged commit 87aa310 into matplotlib:master Oct 22, 2018
@jklymak
jklymak deleted the fix-singleton-hist-labels branch October 22, 2018 17:41
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Oct 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Numeric labels do not work with plt.hist

4 participants