Visualize point P that works with Python

What i did

I drew a point P that works with Python and output it as a gif.

ugokutenP02.gif

example

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

drawing

environment

1. Make an empty figure

First, prepare for drawing the figure.

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)

plt.show()

image.png

2. Display a rectangle

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. 1576855216649.jpg


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

image.png

3. Display the length of the side and the name of the vertex

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

image.png

4. Display point P

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

5. Display △ APD

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

6. Move

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.

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

ugokutenP02.gif

Recommended Posts

Visualize point P that works with Python
Works with Python and R
Visualize python package dependencies with graphviz
[Python] A program that creates stairs with #
A typed world that begins with Python
The result of making the first thing that works with Python (image recognition)
[Python] Visualize and identify slow parts with pytest
FizzBuzz with Python3
Scraping with Python
Statistics with python
Create an app that guesses students with python
Scraping with Python
Twilio with Python
Create a page that loads infinitely with python
Integrate with Python
Play with 2016-Python
AES256 with python
python starts with ()
Folium: Visualize data on a map with Python
Bingo with python
Zundokokiyoshi with python
One-liner that outputs 10000 digits of pi with Python
Sample program that outputs syslog with Python logging
Formulas that appear in Doing Math with Python
Visualize grib2 on a map with python (matplotlib)
Excel with Python
Microcomputer with Python
Cast with python
String manipulation with python & pandas that I often use
[Python] OpenCV environment construction with Docker (cv2.imshow () also works)
Visualize the range of interpolation and extrapolation with python
Parse and visualize JSON (Web application ⑤ with Python + Flask)
Python knowledge notes that can be used with AtCoder
A addictive point in "Bayesian inference experience with Python"
A server that echoes data POSTed with flask / python
A memo that I touched the Datastore with python
Visualize the appreciation status of art works with OpenCV
Articles that enable system development with Django (Python) _Introduction
Serial communication with Python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Quickly visualize with Pandas
Learning Python with ChemTHEATER 03
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python