I drew a point P that works with Python and output it as a gif.
Let's visualize the problem of moving point P as follows.
There is a rectangular ABCD with AB = 4cm, BC = 6cm, and the point P starts from A and progresses from A to B to C to D at 1cm per second. Let the area of △ APD after $ x $ seconds from departure be $ y $ cm 2 </ sup>.
Source https://math.005net.com/yoten/doten.php (However, 2 cm per second is 1 cm per second.)
First, prepare for drawing the figure.
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
plt.show()
Make a rectangle 4 cm long and 6 cm wide. Here, there are various ways to choose where to put the coordinate origin $ O $. This time, select the center of gravity of the rectangle (2 cm in length and 3 cm in width) as the origin $ O $, and determine the coordinates of points A, B, C, and D. This will make the code easier if you later want the label to appear on the outside of the rectangle.
Use matplotlib.patches
to draw the shape. Also, I want to treat the coordinates of the point as a vector, so I also enter numpy
.
Use set_xlim
, set_ylim
to make the x-axis and y-axis ranges slightly larger than the rectangle so that the rectangle fits in the frame.
Define the coordinates of points A, B, C, D with the np.array
type.
Make a rectangle with pat.Polygon
and add it to ʻax1`.
import numpy as np #add to
import matplotlib.pyplot as plt
import matplotlib.patches as pat #add to
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlim(-4,4)
ax1.set_ylim(-3,3)
A=np.array([-3,2])
B=np.array([-3,-2])
C=np.array([3,-2])
D=np.array([3,2])
p = pat.Polygon(xy = [A,B,C,D],
edgecolor='black',
facecolor='white',
linewidth=1.6)
ax1.add_patch(p)
plt.show()
Draw the length of the side
text
can draw characters by specifying the x coordinate in the first argument, the y coordinate in the second argument, and the name in the third argument.
#Display the length of the side
ax1.text(-3.5,0.0,"4cm",horizontalalignment='center',verticalalignment='center')
ax1.text(0.0,-2.5,"6cm",horizontalalignment='center',verticalalignment='center')
Fine-tune the placement to your liking.
Next, the name of the point is displayed on the outside of the rectangle. You may decide the display position by hand as well as the length of the side.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as pat
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlim(-4,4)
ax1.set_ylim(-3,3)
A=np.array([-3,2])
B=np.array([-3,-2])
C=np.array([3,-2])
D=np.array([3,2])
pol = pat.Polygon(xy = [A,B,C,D],
edgecolor='black',
facecolor='white',
linewidth=1.6)
ax1.add_patch(pol)
#Display the length of the side
ax1.text(-3.5,0.0,"4cm",horizontalalignment='center',verticalalignment='center')
ax1.text(0.0,-2.5,"6cm",horizontalalignment='center',verticalalignment='center')
#Display the name of the vertex
scale=1.1
ax1.text(A[0]*scale,A[1]*scale,"A",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(B[0]*scale,B[1]*scale,"B",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(C[0]*scale,C[1]*scale,"C",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(D[0]*scale,D[1]*scale,"D",fontsize=15,horizontalalignment='center',verticalalignment='center')
plt.show()
The leading role is finally here. Define the coordinates of point P and display the point with ʻax1.plot () `. The way to display the name of point P is the same as that of points A, B, C and D.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as pat
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlim(-4,4)
ax1.set_ylim(-3,3)
A=np.array([-3,2])
B=np.array([-3,-2])
C=np.array([3,-2])
D=np.array([3,2])
pol = pat.Polygon(xy = [A,B,C,D],
edgecolor='black',
facecolor='white',
linewidth=1.6)
ax1.add_patch(pol)
ax1.text(-3.5,0.0,"4cm",horizontalalignment='center',verticalalignment='center')
ax1.text(0.0,-2.5,"6cm",horizontalalignment='center',verticalalignment='center')
scale=1.1
ax1.text(A[0]*scale,A[1]*scale,"A",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(B[0]*scale,B[1]*scale,"B",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(C[0]*scale,C[1]*scale,"C",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(D[0]*scale,D[1]*scale,"D",fontsize=15,horizontalalignment='center',verticalalignment='center')
P=np.array([-3.0,1.0])
#Display the moving point P
scale_P=1.2
ax1.plot(P[0],P[1],marker='o',color='black')
ax1.text(P[0]*scale_P,P[1]*scale_P,"P",fontsize=15,horizontalalignment='center',verticalalignment='center')
plt.show()
Create a triangular APD and add it to the diagram as you did when you created the rectangular ABCD, then erase the x-axis and y-axis.
%matplotlib nbagg
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as pat
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_xlim(-4,4)
ax1.set_ylim(-3,3)
A=np.array([-3,2])
B=np.array([-3,-2])
C=np.array([3,-2])
D=np.array([3,2])
pol = pat.Polygon(xy = [A,B,C,D],
edgecolor='black',
facecolor='white',
linewidth=1.6)
ax1.add_patch(pol)
ax1.text(-3.5,0.0,"4cm",horizontalalignment='center',verticalalignment='center')
ax1.text(0.0,-2.5,"6cm",horizontalalignment='center',verticalalignment='center')
scale=1.1
ax1.text(A[0]*scale,A[1]*scale,"A",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(B[0]*scale,B[1]*scale,"B",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(C[0]*scale,C[1]*scale,"C",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(D[0]*scale,D[1]*scale,"D",fontsize=15,horizontalalignment='center',verticalalignment='center')
P=np.array([-3.0,1.0])
scale_P=1.2
ax1.plot(P[0],P[1],marker='o',color='black')
ax1.text(P[0]*scale_P,P[1]*scale_P,"P",fontsize=15,horizontalalignment='center',verticalalignment='center')
#△ Added APD
S = pat.Polygon(xy = [A,P,D],
edgecolor='black',
facecolor='lightgray',
linewidth=1.6)
ax1.add_patch(S)
#Erase the frame
plt.axis('off')
plt.show()
Move the moving point P and output it as a gif animation. The coordinates of the moving point P after $ x $ seconds are updated every time, and the animation is created by drawing frame by frame.
moveP (x)
classifies where the moving point P is on the line segments AB, BC, and CD from the elapsed time, and returns the coordinates of the moving point P.velocity
) of the moving point P is 1 cm per second, and the drawing interval (time step
) is 0.1 seconds.PillowWriter
and FuncAnimation
to create animations.
ʻAnimate (i)summarizes all the operations to be performed in one drawing update, and creates an animation by taking the argument of
FuncAnimation`.%matplotlib nbagg #Required to animate on jupyter notebook
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as pat
from matplotlib.animation import PillowWriter,FuncAnimation #Added for video creation
fig = plt.figure()
ax1 = fig.add_subplot(111)
A=np.array([-3,2])
B=np.array([-3,-2])
C=np.array([3,-2])
D=np.array([3,2])
scale=1.1
scaleP=1.2
p = pat.Polygon(xy = [A,B,C,D],
edgecolor='black',
facecolor='white',
linewidth=1.6)
def initialize():
ax1.set_xlim(-4,4)
ax1.set_ylim(-3,3)
ax1.add_patch(p)
ax1.text(A[0]*scale,A[1]*scale,"A",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(B[0]*scale,B[1]*scale,"B",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(C[0]*scale,C[1]*scale,"C",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(D[0]*scale,D[1]*scale,"D",fontsize=15,horizontalalignment='center',verticalalignment='center')
ax1.text(-3.5,0.0,"4cm",horizontalalignment='center',verticalalignment='center')
ax1.text(0.0,-2.5,"6cm",horizontalalignment='center',verticalalignment='center')
def moveP(x):
if 0<= x <4:
return A+np.array([0,-1])*x*velocity
elif 4<=x <10:
return B+np.array([1,0])*(x-4)*velocity
elif 10<= x <14:
return C+np.array([0,1])*(x-10)*velocity
else:
return D
velocity=1.0
timestep=0.1
def animate(t):
plt.cla()
initialize()
x=timestep*t
P=moveP(x)
ax1.plot(P[0],P[1],marker='o',color='black')
ax1.text(P[0]*scaleP,P[1]*scaleP,"P",fontsize=15,horizontalalignment='center',verticalalignment='center')
S = pat.Polygon(xy = [A,P,D],
edgecolor='black',
facecolor='lightgray',
linewidth=1.6)
ax1.add_patch(S)
plt.axis('off')
plt.title('x=' + '{:.1f}'.format(x)+'sec')
anim = FuncAnimation(fig,animate,frames=140,repeat=True,interval=timestep*1000)
#anim.save("ugokutenP.gif", writer='pillow',fps=10)
plt.show()
Recommended Posts