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