Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug report
Bug summary
plt.bar() only accept 1D arrays, and does not accept 2D arrays with a dimension that has length 1. E.g., does not accept arrays with shape (3,1). Unfortunately, the traceback is unhelpful.
Code for reproduction
frommatplotlibimportpyplotaspltimportnumpyasnpplt.bar(np.zeros((2,)),np.zeros((2,1)))
Actual outcome
File "<stdin>", line 1, in <module> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2648, in bar ret = ax.bar(*args, **kwargs) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.py", line 1717, in inner return func(ax, *args, **kwargs) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 2087, in bar label='_nolegend_', File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/patches.py", line 659, in __init__ Patch.__init__(self, **kwargs) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/patches.py", line 103, in __init__ self.set_linewidth(linewidth) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/patches.py", line 351, in set_linewidth self._linewidth = float(w)TypeError: only length-1 arrays can be converted to Python scalars
Expected outcome
plt.bar() should be able to figure out that a 1D vector and a 2D array with a singleton dimension are equivalent. This can be done by applying squeeze if necessary. In any case if plt.bar() is going to fail unintuitively on this, it should test to see if the input is a 2D array, and provide an informative error message.
Matplotlib version
- Operating system: windows, linux, and mac
- Matplotlib version: most recent
- Matplotlib backend (
print(matplotlib.get_backend())
): - Python version: 2.7 and 3.6
- Jupyter version (if applicable):
- Other libraries:
This code works with matplotlib version 1.4.2 and does not work with 2.1.1. Other pyplot functions do not exhibit this behavior. E.g., plt.plot(np.zeros((2,1)),np.zeros((2,1))) works fine.