Skip to content

axes.bar supports values given as dict_values yet axes.barh throws an error #14693

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

Closed
AoifeHughes opened this issue Jul 4, 2019 · 4 comments
Closed
Milestone

Comments

@AoifeHughes
Copy link

Bug report

Bug summary
Calling axes.barh will throw this error if the height of the bars are given as a dictonary values type
TypeError: unsupported operand type(s) for +: 'int' and 'dict_values'

Code for reproduction

import matplotlib.pyplot as plt

data = {'cats': 10, 'dogs': 20}

# Works as expected
fig, ax = plt.subplots(1)
ax.bar(range(len(data)), data.values())

# Doesn't work
fig, ax = plt.subplots(1)
ax.barh(range(len(data)), data.values())

# Works as expected
fig, ax = plt.subplots(1)
ax.barh(range(len(data)), list(data.values()))

plt.show()
@timhoffm
Copy link
Member

timhoffm commented Jul 4, 2019

Works on master with Python 3.7.

Please provide your python and matplotlib versions.

@timhoffm timhoffm added the status: needs clarification Issues that need more information to resolve. label Jul 4, 2019
@AoifeHughes
Copy link
Author

  • matplotlib version 3.0.2
  • python 3.6.7
  • OSX


(playground)  nathan@x86_64-apple-darwin13  ~ ipython
Python 3.6.7 |Anaconda, Inc.| (default, Oct 23 2018, 14:01:38)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import matplotlib

In [2]: matplotlib.__version__
Out[2]: '3.0.2'

In [3]: import matplotlib.pyplot as plt
   ...:
   ...: data = {'cats': 10, 'dogs': 20}
   ...:
   ...: # Works as expected
   ...: fig, ax = plt.subplots(1)
   ...: ax.bar(range(len(data)), data.values())
   ...:
   ...: # Doesn't work
   ...: fig, ax = plt.subplots(1)
   ...: ax.barh(range(len(data)), data.values())
   ...:
   ...: # Works as expected
   ...: fig, ax = plt.subplots(1)
   ...: ax.barh(range(len(data)), list(data.values()))
   ...:
   ...: plt.show()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-79a8e56f0f43> in <module>
      9 # Doesn't work
     10 fig, ax = plt.subplots(1)
---> 11 ax.barh(range(len(data)), data.values())
     12
     13 # Works as expected

~/anaconda3/envs/playground/lib/python3.6/site-packages/matplotlib/axes/_axes.py in barh(self, y, width, height, left, align, **kwargs)
   2461         kwargs.setdefault('orientation', 'horizontal')
   2462         patches = self.bar(x=left, height=height, width=width, bottom=y,
-> 2463                            align=align, **kwargs)
   2464         return patches
   2465

~/anaconda3/envs/playground/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, data, *args, **kwargs)
   1808                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1809                         RuntimeWarning, stacklevel=2)
-> 1810             return func(ax, *args, **kwargs)
   1811
   1812         inner.__doc__ = _add_data_doc(inner.__doc__,

~/anaconda3/envs/playground/lib/python3.6/site-packages/matplotlib/axes/_axes.py in bar(self, x, height, width, bottom, align, **kwargs)
   2294                 edgecolor=e,
   2295                 linewidth=lw,
-> 2296                 label='_nolegend_',
   2297                 )
   2298             r.update(kwargs)

~/anaconda3/envs/playground/lib/python3.6/site-packages/matplotlib/patches.py in __init__(self, xy, width, height, angle, **kwargs)
    664         self._height = height
    665
--> 666         self._x1 = self._x0 + self._width
    667         self._y1 = self._y0 + self._height
    668

TypeError: unsupported operand type(s) for +: 'int' and 'dict_values'

In [4]:

@dstansby
Copy link
Member

It looks like you're using an old version of Matplotlib; the most recent version is 3.1.1. Please could you upgrade to Matplotlib 3.1.1 and let us know if there is still a problem?

@jklymak
Copy link
Member

jklymak commented Jul 15, 2019

Yeah this was fixed in 3.1. #13187 I'm going to close...

@jklymak jklymak closed this as completed Jul 15, 2019
@dstansby dstansby added this to the v3.1.0 milestone Jul 15, 2019
@dstansby dstansby removed the status: needs clarification Issues that need more information to resolve. label Jul 15, 2019
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

No branches or pull requests

4 participants