python画椭圆的逻辑_使用matplotlib.pyplot绘制椭圆(Python)

如果不想使用补丁,可以使用椭圆的参数方程:

x=u+a.cos(t);y=v+b.sin(t)import numpy as np

from matplotlib import pyplot as plt

from math import pi

u=1. #x-position of the center

v=0.5 #y-position of the center

a=2. #radius on the x-axis

b=1.5 #radius on the y-axis

t = np.linspace(0, 2*pi, 100)

plt.plot( u+a*np.cos(t) , v+b*np.sin(t) )

plt.grid(color='lightgray',linestyle='--')

plt.show()

它给出:

x5IcR.png

由于二维旋转矩阵,椭圆可以旋转:import numpy as np

from matplotlib import pyplot as plt

from math import pi, cos, sin

u=1. #x-position of the center

v=0.5 #y-position of the center

a=2. #radius on the x-axis

b=1.5 #radius on the y-axis

t_rot=pi/4 #rotation angle

t = np.linspace(0, 2*pi, 100)

Ell = np.array([a*np.cos(t) , b*np.sin(t)])

#u,v removed to keep the same center location

R_rot = np.array([[cos(t_rot) , -sin(t_rot)],[sin(t_rot) , cos(t_rot)]])

#2-D rotation matrix

Ell_rot = np.zeros((2,Ell.shape[1]))

for i in range(Ell.shape[1]):

Ell_rot[:,i] = np.dot(R_rot,Ell[:,i])

plt.plot( u+Ell[0,:] , v+Ell[1,:] ) #initial ellipse

plt.plot( u+Ell_rot[0,:] , v+Ell_rot[1,:],'darkorange' ) #rotated ellipse

plt.grid(color='lightgray',linestyle='--')

plt.show()

返回:

0MJId.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值