Skip to content

Numeric labels do not work with plt.hist #3869

Closed

Description

@mwaskom

Many (most?) matplotlib functions accept a numeric object for the label kwarg. For example, this works fine:

x = np.random.randn(50)
plt.plot(x, label=0)
plt.legend()

foo

However, the same is not true for plt.hist:

x = np.random.randn(50)
plt.hist(x, label=0)
plt.legend()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-27407c59c8b6> in <module>()
      1 x = np.random.randn(50)
----> 2 plt.hist(x, label=0)
      3 plt.legend()

/Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, **kwargs)
   2894                       histtype=histtype, align=align, orientation=orientation,
   2895                       rwidth=rwidth, log=log, color=color, label=label,
-> 2896                       stacked=stacked, **kwargs)
   2897         draw_if_interactive()
   2898     finally:

/Users/mwaskom/anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
   5858         else:
   5859             raise ValueError(
-> 5860                 'invalid label: must be string or sequence of strings')
   5861 
   5862         if len(labels) < nx:

ValueError: invalid label: must be string or sequence of strings

Is there a reason for this constraint on histogram labels?

Activity

  1. added this to the v1.4.3 milestone on Dec 1, 2014
  2. efiring commented on Dec 1, 2014

    @efiring
    Member

    Logically, shouldn't a label always be a string?

  3. mwaskom commented on Dec 1, 2014

    @mwaskom
    Author

    No, it is logically possible for labels to be numbers.

  4. jenshnielsen commented on Dec 1, 2014

    @jenshnielsen
    Member

    Why would you prefer a number over str(number)?

  5. mwaskom commented on Dec 1, 2014

    @mwaskom
    Author

    An idiosyncratic preference for generality and brevity.

  6. tacaswell commented on Dec 1, 2014

    @tacaswell
    Member

    The block of code that is raising the error is doing excessive type checking anyway.

    I would argue that a label should be anything that can be coerced to be a string.

  7. nicktimko commented on Dec 28, 2014

    @nicktimko

    It looks like the string checking is ultimately trying obj + '' to see if it's "string like". That looks a bit JavaScript-y, so maybe tweak that? str(obj) is tautological as it falls back to repr() which always works, so I don't know how to check if str() gives something "reasonable looking".

    Maybe just don't check at all as @tacaswell suggests.

  8. added a commit that references this issue on Dec 28, 2014
    a22e82e
  9. tacaswell commented on Dec 28, 2014

    @tacaswell
    Member

    I think for checking stringy-ness seeing if you can add a string is about as good as you are going to get. I put in a PR to relax the validation (along with some maintenance and doc changes).

  10. mwaskom commented on Oct 19, 2018

    @mwaskom
    Author

    Looks like didn't actually address this issue (cf mwaskom/seaborn#1593).

    Specifically:

    plt.hist([0, 1], label=0)  # errors
    plt.plot([0, 1], label=0)  # works fine
    plt.hist([[0, 1], [0, 2]], label=[0, 2])  # works fine
  11. jklymak commented on Oct 19, 2018

    @jklymak
    Member

    Re-opening, though this new report is slightly different...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions