0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

matplotlibのarrowで'vertices' must be a 2D list ... と怒られる場合の対処法

Posted at

#問題設定

pythonのmatplotlibには矢印を表示する関数が用意されています.

import matplotlib.pyplot as plt

dx = 0.3
dy = 0.3
params = {
    'width':0.01,
    }
plt.arrow(0, 0, dx, dy, **params)
plt.xlim(-0.5, 0.5)
plt.ylim(-0.5, 0.5)
plt.show()

Figure.png

矢印の始点と終点を設定する際,矢印に鏃を含むか含まないかをlength_includes_headで設定できます.
デフォルトだと含まない設定です.

import matplotlib.pyplot as plt

dx = 0.0
dy = 0.2
params = {
    'width':0.01,
    }
plt.arrow(-0.1, 0, dx, dy, **params)

params = {
    'width':0.01,
    'length_includes_head':True,
    }
plt.arrow(0.1, 0, dx, dy, **params)

plt.grid()

plt.xlim(-0.5, 0.5)
plt.ylim(-0.5, 0.5)
plt.show()

Figure_.png

長さ0の矢印を描画する場合,鏃を含むか含まないかで結果が変わり,鏃を含むとエラーが出ます.

鏃を含まない場合:

test1.py
import matplotlib.pyplot as plt

dx = 0.0
dy = 0.0
params = {
    'width':0.01,
    }
plt.arrow(0, 0, dx, dy, **params)
plt.xlim(-0.5, 0.5)
plt.ylim(-0.5, 0.5)
plt.show()

Figure_1.png

鏃を含む場合:

test2.py
import matplotlib.pyplot as plt

dx = 0.0
dy = 0.0
params = {
    'width':0.01,
    'length_includes_head':True,
    }
plt.arrow(0, 0, dx, dy, **params)
plt.xlim(-0.5, 0.5)
plt.ylim(-0.5, 0.5)
plt.show()
Traceback (most recent call last):
  File "test2.py", line 9, in <module>
    plt.arrow(0, 0, dx, dy, **params)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/pyplot.py", line 2411, in arrow
    return gca().arrow(x, y, dx, dy, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 4822, in arrow
    a = mpatches.FancyArrow(x, y, dx, dy, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/patches.py", line 1269, in __init__
    super().__init__(verts, closed=True, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/patches.py", line 938, in __init__
    self.set_xy(xy)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/patches.py", line 1005, in set_xy
    self._path = Path(xy, closed=self._closed)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/path.py", line 130, in __init__
    "'vertices' must be a 2D list or array with shape Nx2")
ValueError: 'vertices' must be a 2D list or array with shape Nx2

#対処法

##対処法1

矢印の長さと矢印の幅等をいい感じに変えればエラーが消えます.

import matplotlib.pyplot as plt

dx = 1.0e-8
dy = 0.0
params = {
    'width':1.0e-8,
    'length_includes_head':True,
    }
plt.arrow(0, 0, dx, dy, **params)
plt.xlim(-0.5, 0.5)
plt.ylim(-0.5, 0.5)
plt.show()

(何も表示されないので表示結果は略)

##対処法2

元凶はmatplotlib/patches.py (私の環境では /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/patches.py ) の1227行目付近の以下の文

        if not length:
            verts = []  # display nothing if empty
        else:
            ...

で,verts = [] が型違いで怒られています.
もっとも簡単な修正方法は,その直前でlength等を適当に設定してしまうことです.

        if not length:
          length = distance = 1.0E-8

        if not length:
            verts = []  # display nothing if empty
        else:
            ...

(何も表示されないので表示結果は略)

#おまけ

矢印の長さが鏃の大きさより小さいと表示が変になるので注意してください.

import matplotlib.pyplot as plt

dx = 0.001
dy = 0.0
params = {
    'width':0.01,
    'length_includes_head':True,
    }
plt.arrow(0, 0, dx, dy, **params)
plt.xlim(-0.5, 0.5)
plt.ylim(-0.5, 0.5)
plt.show()

Figure_2_1.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?