1. 程式人生 > >Python:畫出笛卡爾心形曲線

Python:畫出笛卡爾心形曲線

極座標方程: ρ = a ( 1 sin θ )

\rho = a (1-\sin\theta)

極座標畫圖:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

theta = np.linspace(0.0, 2 * np.pi, 1000)
a = 5
rho = a * (1 - np.sin(theta))
plt.subplot(polar=True)
plt.plot(theta, rho, c = 'r')
plt.show()

在這裡插入圖片描述

直角座標畫圖:
python程式:

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

theta = np.linspace(0.5*np.pi, 1.5*np.pi, 1000)
sintheta = np.sin(theta)
a = 1
y = a * sintheta * (1 - sintheta)
x = np.sqrt((1 - sintheta) ** 2 - y ** 2)
x1 = np.hstack((x,-x[::-1]))  #x[::-1]意思是反轉x
y1 = np.
hstack((y,y[::-1])) plt.plot(x1, y1, c = 'r') plt.show()

在這裡插入圖片描述