Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

plt.bar() fails on 2D arrays with singleton dimensions. error message is unhelpful. #10733

Closed as not planned
@HoniSanders

Description

@HoniSanders

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp