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

Skip to content

Fix singleton hist labels #12572

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
merged 2 commits into from
Oct 22, 2018
Merged

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Oct 19, 2018

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
else:
if isinstance(label, str):
Copy link
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
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
@@ -6758,6 +6758,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)]
Copy link
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
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
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
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
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