[PYTHON] What to do if you get angry with'vertices' must be a 2D list ... in matplotlib arrow

Problem setting

python's matplotlib has a function to display an arrow.

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

When setting the start and end points of an arrow, you can set whether the arrow includes or does not include it with length_includes_head. It is a setting that is not included by default.

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

When drawing an arrow of length 0, the result will change depending on whether or not the arrow is included, and an error will occur if the arrow is included.

If it does not include iron:

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

When including iron:

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

approach

Remedy 1

If you change the length of the arrow and the width of the arrow to a good feeling, the error disappears.

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()

(Since nothing is displayed, the display result is omitted)

Remedy 2

The cause is the following statement near line 1227 of matplotlib / patches.py (/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/matplotlib/patches.py in my environment)

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

So, verts = [] is angry because of the wrong type. The easiest way to fix it is to set the length etc. just before that.

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

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

(Since nothing is displayed, the display result is omitted)

bonus

Please note that the display will be strange if the length of the arrow is smaller than the size of the iron.

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

Recommended Posts

What to do if you get angry with'vertices' must be a 2D list ... in matplotlib arrow
What to do if you get a minus zero in Python
What to do if you get angry in TensorFlow v2 without attribute'app'
What to do if you get a must override `get_config` error when trying to model.save in Keras
What to do if you get a "No versions found" error in pipenv
What to do if you get angry with "Gtk * backend requires pygtke to be installed" even though pygtk is included in matplotlib
What to do if you get "coverage unknown" in Coveralls
What to do if you get an error when importing matplotlib in Python (Mac)
What to do if you get a UnicodeDecodeError with pip install
What to do if you get a Cannot retrieve metalink for repository error in yum
What to do if you get lost in file reference with FileNotFoundError
What to do if you get a TypeError with numpy min, max
What to do if you get angry with "Value Error: unknown local: UTF-8" in python manage.py syncdb
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda
What to do if you get angry with swapon failed: Operation not permitted
What to do if you get "Python not configured." Using PyDev in Eclipse
What to do if a UnicodeDecodeError occurs in pip
What to do if you get an error when trying to send a message in tasks.loop () immediately after startup
What to do if you get `No kernel for language python found` in Hydrogen
What to do if you get a memory error when converting from PySparkDataFrame to PandasDataFrame
What to do if you get "(35,'SSL connect error')" in pycurl (one of them)
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if you can't hit the arrow keys in the Python interactive console
What to do if pip gives a DistributionError in Homebrew
What to do if a 0xC0000005 error occurs in tf.train.start_queue_runners ()
What to do if you can't log in as root
What to do if you get a Call with too many input arguments error at DoAndReturn in a golang test
What to do if you get Swagger-codegen in python and Import Error: No module named
What to do if you get an error when running "certbot renew" in CakePHP environment
If you remove the list to be looped, you will get terrible.
What to do if you can't use the trash in Lubuntu 18.04.
What to do if you get an error saying c compiler cannot create executables in configure
What to do if you get angry if you don't have libxml / xmlversion.h when installing lxml on CentOS
What to do if you get `locale.Error: unsupported locale setting` when getting the day of the week from a date in Python
What to do if there is a decimal in python json .dumps
What to do if you can't find PDO in Laravel or CakePHP
What to do if you can't use scikit grid search in Python
What to do if you get stuck during Anaconda installation on Linux
What to do if you get an error when trying to load mnist
What to do if you can't install with pip in babun environment
What to do if you get Could not fetch URL 443 with pip
What happens if you do "import A, B as C" in Python?
What to do if you get an error when installing Dlib (Ubuntu)
What to do when a warning message is displayed in pip list
What to do if you get "The session could not be opened" when installing CentOS on VirtualBox
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
What to do if you get a Permission denied (public key) error when trying to pull on Github
What to do if you get the error Target WSGI script'/var/www/xxx/xxx.wsgi' cannot be loaded as python module
What to do if pipreqs results in UnicodeDecodeError
What to do if you can't pipenv shell
What to do if pip cannot be installed
What to do to get google spreadsheet in python
What to do if a Unicode Encode Error occurs in Sublime Text Python
What to do if you get an error when installing python with pyenv
[Python] What to check when you get a Unicode Decode Error in Django
What to do if NotADirectoryError: [Errno 20] Not a directory:'xdg-settings' appears in jupyter notebook
What to do if a version error occurs in the selenium Chrome driver
What do you like about how to convert an array (list) to a string?
What to do if you get the error Input contains NaN, infinity or a value too large for dtype ('float64'). In machine learning
What to do if you get the error RuntimeError: Python is not installed as a framework when trying to use matplitlib and pylab in Python 3.3
What to do if you get an "unknown service" error from your gRPC server