python三维散点图动图代码( #要将三维散点图变成动画可使用 Matplotlib 库中的 `FuncAnimation` 类,将每组数据的前 i 个数据点逐步添加到图形中,从而创建一个动画效果: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib.animation import FuncAnimation # 三组数据 data1 = [(2000,40,30),(2001,78,58),(2002,124,83),(2003,211,145),(2004,218,159),(2005,290,214),(2006,323,207),(2007,359,199),(2008,283,181),(2009,272,181),(2010,300,193),(2011,257,144),(2012,340,205),(2013,364,225),(2014,357,217),(2015,394,248),(2016,472,200),(2017,803,245),(2018,1187,158),(2019,1526,118)] data2 = [(2000,345,153),(2001,488,253),(2002,678,423),(2003,737,374),(2004,1127,534),(2005,1252,602),(2006,1199,646),(2007,1249,688),(2008,1050,565),(2009,854,484),(2010,615,390),(2011,578,425),(2012,569,413),(2013,488,39),(2014,584,495),(2015,436,321),(2016,579,320),(2017,591,118),(2018,470,43),(2019,27,1)] data3 = [(2000,139,118),(2001,158,112),(2002,252,179),(2003,349,188),(2004,427,219),(2005,530,210),(2006,530,210),(2007,520,191),(2008,456,220),(2009,304,180),(2010,324,203),(2011,293,180),(2012,264,166),(2013,233,157),(2014,203,124),(2015,221,136),(2016,227,107),(2017,224,77),(2018,144,33),(2019,84,3)] # 创建三维坐标系 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # 定义动画函数 def update(i): # 绘制散点图 ax.scatter([d[0] for d in data1[:i]], [d[1] for d in data1[:i]], [d[2] for d in data1[:i]], c='r', marker='o') ax.scatter([d[0] for d in data2[:i]], [d[1] for d in data2[:i]], [d[2] for d in data2[:i]], c='g', marker='o') ax.scatter([d[0] for d in data3[:i]], [d[1] for d in data3[:i]], [d[2] for d in data3[:i]], c='b', marker='o') # 设置坐标轴标签 ax.set_xlabel('年份') # 添加x轴标签 ax.set_ylabel('发明专利数量') # 添加y轴标签 ax.set_zlabel('降低成本专利数量') # 添加z轴标签 # 解决中文显示问题 plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文字体 plt.rcParams['axes.unicode_minus'] = False # 字符显示正确,该语句解决图像中的“-”负号的乱码问题 # 创建动画对象 ani = FuncAnimation(fig, update, frames=range(1, 21), interval=500) # 显示动画 plt.show())怎么保存为动画呢
时间: 2023-06-08 19:07:45 浏览: 180
下面是Python的三维散点图动图代码:
```python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.animation as animation
# 创建一个三维坐标系
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 生成随机数据
x = np.random.normal(loc=0, scale=1, size=100)
y = np.random.normal(loc=0, scale=1, size=100)
z = np.random.normal(loc=0, scale=1, size=100)
# 绘制散点图
sc = ax.scatter(x, y, z, c=z, cmap='coolwarm')
# 定义动画函数
def animate(i):
# 生成新的随机数据
x = np.random.normal(loc=0, scale=1, size=100)
y = np.random.normal(loc=0, scale=1, size=100)
z = np.random.normal(loc=0, scale=1, size=100)
# 更新散点图数据
sc.set_offsets(np.c_[x, y])
sc.set_3d_properties(z)
# 每次更新后返回散点图对象
return sc,
# 创建动画
ani = animation.FuncAnimation(fig, animate, frames=100, interval=50, blit=True)
# 显示动画
plt.show()
```
这段代码生成了一个三维坐标系,并在其中绘制了随机生成的三维散点图。animate函数则定义了每个动画帧如何更新散点图数据,这里每次生成新的随机数据。最后,利用FuncAnimation函数生成动画,并通过plt.show()函数显示动画。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)