(2)绘制一个红色圆点,该圆点最初位于正弦曲线的左端; (3)制作一个圆点沿曲线运动的动画,并在右上角时刻显示圆点当前的坐标位置;
时间: 2024-01-21 19:17:11 浏览: 37
以下是Python代码实现:
```python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
# 绘制正弦曲线
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
# 绘制圆点
dot, = ax.plot(x[0], y[0], 'ro')
# 设置动画函数
def animate(i):
dot.set_xdata(x[i])
dot.set_ydata(y[i])
ax.set_title(f"当前坐标:({x[i]:.2f}, {y[i]:.2f})") # 在右上角显示当前坐标
return dot,
# 创建动画
ani = FuncAnimation(fig, animate, frames=range(len(x)), interval=50, blit=True)
# 显示动画
plt.show()
```
运行代码后,会弹出一个窗口显示正弦曲线和一个红色圆点。圆点会沿着曲线运动,并且窗口的右上角会显示圆点当前的坐标位置。
相关问题
绘制一条正弦曲线和一个红色圆点,圆点最初位于正弦曲线的左端,圆点沿曲线运动,并时刻显示圆点的坐标
以下是使用Python和matplotlib库实现的代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义正弦曲线的x轴取值范围
x = np.arange(0, 6 * np.pi, 0.1)
# 计算正弦曲线上每个点的坐标
y = np.sin(x)
# 创建绘图窗口
fig, ax = plt.subplots()
# 绘制正弦曲线
ax.plot(x, y)
# 设置坐标轴标签
ax.set_xlabel('x')
ax.set_ylabel('y')
# 创建红色圆点
dot, = ax.plot(x[0], y[0], 'o', color='red')
# 更新圆点位置,并时刻显示坐标
def update_dot(i):
dot.set_data(x[i], y[i])
ax.set_title('x={:.2f}, y={:.2f}'.format(x[i], y[i]))
# 使用FuncAnimation函数更新圆点位置
from matplotlib.animation import FuncAnimation
ani = FuncAnimation(fig, update_dot, frames=len(x), interval=50)
# 显示动画
plt.show()
```
运行代码后,会显示一个窗口,其中正弦曲线会随着时间自动滚动,而圆点则会沿着曲线运动,并在窗口标题中实时显示其坐标。
绘制一条正弦曲线; (2)绘制一个红色圆点,该圆点最初位于正弦曲线的左端; (3)制作一个圆点沿曲线运动的动画,并在右上角时刻显示圆点当前的坐标位置;
以下是使用Python的Matplotlib库绘制正弦曲线和动画的代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# 生成横轴数据
x = np.linspace(0, 2*np.pi, 200)
# 生成纵轴数据
y = np.sin(x)
# 创建画布
fig, ax = plt.subplots()
# 绘制正弦曲线
line, = ax.plot(x, y)
# 创建红色圆点
dot, = ax.plot(x[0], y[0], 'ro')
# 设置动画更新函数
def animate(i):
# 更新圆点位置
dot.set_data(x[i], y[i])
# 显示圆点坐标
ax.set_title('({:.2f}, {:.2f})'.format(x[i], y[i]))
return dot,
# 创建动画对象
ani = FuncAnimation(fig, animate, frames=len(x), interval=50, blit=True)
# 显示动画
plt.show()
```
运行代码后,将会看到一个窗口弹出,展示了正弦曲线和圆点沿曲线运动的动画。右上角会显示当前圆点的坐标位置。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)