Skip to content

axes.bar fails when x is int-indexed pandas.Series #15162

@infrub

Description

@infrub

Bug report

Bug summary

The following codes fail in matplotlib 3.1.1, while work properly in 3.0.3

case 1

Code for reproduction

import pandas as pd
from matplotlib import pyplot as plt

df = pd.DataFrame({"x":[1,2,3],"width":[.2,.4,.6]},index=[1,2,3])

plt.figure()
plt.bar(df.x, 1, width=df.width)
plt.show()

Actual outcome

Traceback (most recent call last):
  File "/Desktop/fail_example.py", line 7, in <module>
    plt.bar(df.x, 1, width=df.width)
  File "/.pyenv/versions/3.7.3/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2440, in bar
    **({"data": data} if data is not None else {}), **kwargs)
  File "/.pyenv/versions/3.7.3/lib/python3.7/site-packages/matplotlib/__init__.py", line 1601, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/.pyenv/versions/3.7.3/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 2430, in bar
    label='_nolegend_',
  File "/.pyenv/versions/3.7.3/lib/python3.7/site-packages/matplotlib/patches.py", line 707, in __init__
    Patch.__init__(self, **kwargs)
  File "/.pyenv/versions/3.7.3/lib/python3.7/site-packages/matplotlib/patches.py", line 89, in __init__
    self.set_linewidth(linewidth)
  File "/.pyenv/versions/3.7.3/lib/python3.7/site-packages/matplotlib/patches.py", line 368, in set_linewidth
    self._linewidth = float(w)

Expected outcome
Outcome in 3.0.3:
Figure_1

case 2

Code for reproduction

import pandas as pd
from matplotlib import pyplot as plt

df = pd.DataFrame({"x":[1,3,10]},index=[1000,2000,3000])

plt.figure()
plt.bar(df.x, 1, width=.2)
plt.show()

Actual outcome
Figure_2

Expected outcome
Outcome in 3.0.3:
Figure_3

Matplotlib version

  • Operating system: MacOSX
  • Matplotlib version: 3.1.1 (installed from pip)
  • Matplotlib backend (print(matplotlib.get_backend())): MacOSX
  • Python version: 3.7.3
  • Jupyter version (if applicable):
  • Other libraries: pandas 0.24.2

Cause of the bug

_axes.py, line 2363 in bar:
            x0 = x
            x = np.asarray(self.convert_xunits(x))
            width = self._convert_dx(width, x0, x, self.convert_xunits)
_axes.py, line 2166 in _convert_dx: 
            try:
                x0 = x0[0]
            except (TypeError, IndexError, KeyError):
                x0 = x0

I guess x0 is expected to be a scalar after this line, However when x0 is an int-indexed (and 0 is not in its indices) pandas.Series, it is continued to be pandas.Series and causes chained disorders to end in the error above.
To evidence, they worked properly when rewritten as (ugly!):

            try:
                x0 = x0[0]
            except (TypeError, IndexError, KeyError):
                try:
                    x0 = x0.iat[0]
                except:
                    x0 = x0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy