使用matplotlib移动圆

2024-04-29 14:03:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我绕了一个圈,然后在里面又绕了一个圈。我必须在大圆圈内移动小圆圈

我有np数组中θ的所有值。我想让圆围绕Rheta的每个值移动。我不知道如何“动画化”这个圆

def hypotrochoide(R, r):
   
   theta = np.linspace(0, 2*np.pi, 100)
   
   R= R/2
   
   
   x = R*np.cos(theta)
   y = R*np.sin(theta)
   
   
   r= r/2
   
   rTheta= np.linspace(0, 2*np.pi, 100)
   
   iCircleX = ((R- r) * np.cos(0)) + r*np.cos(theta)
   iCircleY = ((R- r) * np.sin(0)) + r*np.sin(theta)

   
   

   plt.axis('equal')
   
   
   plt.plot(x,y)
   plt.plot(iCircleX,iCircleY)
   


    
hypotrochoide(10, 4)


plt.show()

Tags: plotnppiplt数组sincos圆圈
1条回答
网友
1楼 · 发布于 2024-04-29 14:03:12

这是我使用Pandas和Plotly Express的解决方案,它有一个动画功能:

以下是您需要的软件包:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

import plotly.tools as tls
import plotly.graph_objs as go
import plotly
import plotly.figure_factory as ff
import plotly.express as px
from plotly.subplots import make_subplots
from plotly.offline import init_notebook_mode, plot, iplot, download_plotlyjs
init_notebook_mode(connected=True)
plotly.offline.init_notebook_mode(connected=True)

首先,我编写了两个辅助函数:一个用于计算半径为r和圆心(centerX,centerY)的圆的所有点,另一个用于查找外圆和内圆的所有点

# Create functions
def circle_pts(centerX, centerY, r):
    allTheta = np.linspace(0, 2*np.pi, 100)
    x = r*np.cos(allTheta) + centerX
    y = r*np.sin(allTheta) + centerY
    
    return x, y

def hypotrochoide_calculations(R, r, theta):
    # Find points of outer circle
    outerX, outerY = circle_pts(0, 0, R)
    
    # Find points of inner circle
    centerInnerX = (R - r)*np.cos(theta)
    centerInnerY = (R - r)*np.sin(theta)
    innerX, innerY = circle_pts(centerInnerX, centerInnerY, r)
    
    return outerX, outerY, innerX, innerY

然后我计算0到2pirad之间所有θ的所有点数据(外部和内部x和y),并将其加载到一个数据帧中

# Get data
stepSize = 100
R = 5
r = 1

data = np.zeros((stepSize*stepSize, 5))          # Create empty array

# Create full stepSize^2 list of thetas (each one repeated 100 times)
allTheta = np.linspace(0, 2*np.pi, stepSize)
fullThetas = []
for i in range (allTheta.shape[0]):
    for j in range(stepSize):
        fullThetas.append(allTheta[i])
data[:,0] = fullThetas

# For 0 to 2pi thetas 
for i in range(allTheta.shape[0]):
    # Calculate outer and inner circle points
    outerX, outerY, innerX, innerY = hypotrochoide_calculations(R, r, allTheta[i])
    
    # Calculate row indices
    idxStart = 100*i
    idxEnd = idxStart + 100
    
    # Set outer/inner x/y coordinates to the proper rows in the array
    data[idxStart:idxEnd, 1] = outerX
    data[idxStart:idxEnd, 2] = outerY
    data[idxStart:idxEnd, 3] = innerX
    data[idxStart:idxEnd, 4] = innerY
    
# Create Pandas DataFrame out of array
df = pd.DataFrame(data)
df.columns = ['Theta', 'Outer_X','Outer_Y','Inner_X','Inner_Y']
df

然后,我将该数据帧赋予Plotly Expression函数,并将其指定为跨越所有θ的动画

# Create plotly animation
fig = px.scatter(df, x='Inner_X', y='Inner_Y', animation_frame='Theta')
fig.add_trace(px.scatter(df, x='Outer_X', y='Outer_Y').data[0])

fig.update_layout(title='Hypotrochoide Animation', height=600, width=600)
fig.update_xaxes(range=[-6,6])
fig.update_yaxes(range=[-6,6])

这是来自动画的示例帧Animation

要运行动画,可以拖动滑块或单击播放按钮

相关问题 更多 >