FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

_reshape_2D function behavior changed, breaks hist for some cases in 3.3.0 · Issue #18234 · matplotlib/matplotlib · GitHub

Repository navigation

_reshape_2D function behavior changed, breaks hist for some cases in 3.3.0 #18234

Closed

Description

Bug report

Bug summary

In version 3.2.2, plt.hist(df['class']) will produce a correct histogram where df['class'] is a series of strings. In version 3.3.0, _reshape_2D gives different output in this case, leading to slow computation and a graph with too many bars.

Code for reproduction

import seaborn as sns
import matplotlib.pyplot as plt
df = sns.load_dataset('titanic')
plt.hist(df['class'])

Actual outcome

This is under matplotlib 3.3.0:

This is _reshape_2D under 3.3.0:

Expected outcome

This is under matplotlib 3.2.2:

This is _reshape_2D under 3.2.2:

Can produce the same output for 3.3.0 by putting the series in a list but ideally I think the behavior should remain the same since this unexpectedly also breaks other function behavior relying on this (pandas.DataFrame.hist(), plt.hist())

Matplotlib version

  • Operating system: macOS Mojave 10.14.6
  • Matplotlib version: 3.3.0
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.7.4
  • Jupyter version (if applicable):
  • Other libraries: numpy: 1.18.1, pandas: 1.1.0, seaborn: 0.9.0

matplot lib installed via pip

Activity

  1. tacaswell commented on Aug 12, 2020

    Member

    Thanks for reporting this.

    In the future please always post code and text output as text rather than screen shots.

  2. added this to the v3.3.1 milestone on Aug 12, 2020
  3. tacaswell commented on Aug 12, 2020

    Member

    Do you have this problem with all pandas series or just categorical?

  4. tacaswell commented on Aug 12, 2020

    Member

    As a workaround,

    plt.hist(np.array((df['class'])))

    will get back the old plotting behavior. Something is not quite right with the way we are trying to duck-type pandas catarogical series. I suspect the right fix is for pandas to register a converter on their side that tells us how to interpret them...

  5. modified the milestones: v3.3.1, v3.3.2 on Aug 12, 2020
  6. jklymak commented on Aug 13, 2020

    Member

    This bisects to #17821 (which was fixing part of #17289) where we messed with _reshape_2D to keep bumpy happy.

  7. jklymak commented on Aug 13, 2020

    Member

    I do think that if we first-class pandas like this, we need to have far more comprehensive pandas testing to catch stuff like this.

  8. RyanBaten commented on Aug 13, 2020

    Author

    To address @tacaswell 's question:
    Found this to not just be the case for categorical.

    >>> series = pd.Series(['a', 'b', 'b', 'c', 'd'])
    >>> plt.hist(series)
    (array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
           [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
           [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
           [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
           [0., 0., 0., 0., 0., 0., 0., 0., 0., 1.]]), array([0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4, 2.7, 3. ]), <a list of 5 BarContainer objects>)
    >>> plt.show()
    >>> series.dtype
    dtype('O')
    

    The workaround

    plt.hist(np.array((df['class'])))
    

    and

    plt.hist([df['class']])
    

    both produce expected output as it was under the older version.

    For series that are 'int64' dtype, the output is as expected (although the bars are not centered in both 3.3.0 and 3.2.2).

    >>> series = pd.Series([1,2,3,4,5,4,3,2,3,4,3,2])
    >>> series.dtype
    dtype('int64')
    >>> plt.hist(series)
    (array([1., 0., 3., 0., 0., 4., 0., 3., 0., 1.]), array([1. , 1.4, 1.8, 2.2, 2.6, 3. , 3.4, 3.8, 4.2, 4.6, 5. ]), <BarContainer object of 10 artists>)
    >>> plt.show()
    

  9. jklymak commented on Aug 13, 2020

    Member

    Found this to not just be the case for categorical.

    I'm confused. Categoricals has the problems, and integers do not, right?

  10. RyanBaten commented on Aug 13, 2020

    Author

    Yes, problem exists for 'category' and 'object' but not integers.

  11. jklymak commented on Aug 13, 2020

    Member

    The problem is that if isinstance(xi, collections.abc.Iterable): returns True for a string, but we really want it to be false...

  12. jklymak commented on Aug 13, 2020

    Member

    Basically categoricals never got a test with hist at all.

    import matplotlib.pyplot as plt
    ser = ['a', 'b', 'b', 'c', 'd', 'ff', 'ff', 'a']
    plt.hist(ser)
    plt.show()

    fails... The fix is pretty easy, but we need to fix the tests as well...

  13. modified the milestones: v3.3.2, v3.3.1 on Aug 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL