Python实现绘制3D地球旋转效果
作者:微小冷 时间:2021-04-17 22:25:37
画一个地球
想画一个转动的地球,那么首先要有一个球,或者说要有一个球面,用参数方程可以表示为
x=rcosϕcosθ
y=rcosϕsinθ
z=rsinϕ
然后要有一个地球,或者说要有一个地图,用来作为贴图,映射到球面上。
import numpy as np
import matplotlib.pyplot as plt
path = "earth1.jpg"
img = plt.imread(path)
h, w, c = img.shape
ys, xs = np.indices([h, w])
th = xs/w*np.pi*2
phi = np.pi/2 - ys/h*np.pi
x = np.cos(phi)*np.cos(th)
y = np.cos(phi)*np.sin(th)
z = np.sin(phi)
cs = [tuple(c/255) for c in img.reshape(-1,3)]
ax = plt.subplot(projection='3d')
ax.scatter(x, y, z, marker='.', c=cs)
plt.axis('off')
plt.show()
其中scatter
画的是散点图,c=cs
为颜色映射参数,所以温馨提示,选取的地球图片不宜过大,否则点太多会让电脑爆掉。
最后得到的效果如下
让地球转起来
三维空间中的旋转矩阵如下表所示,具体讲解可参考这两篇博客:旋转坐标轴💎旋转正方体
有了旋转矩阵,接下来就是让地球转起来。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
cos = lambda th : np.cos(np.deg2rad(th))
sin = lambda th : np.sin(np.deg2rad(th))
Rz = lambda th : np.array([
[cos(th) , -sin(th), 0],
[sin(th), cos(th), 0],
[0 , 0, 1]])
xyz = np.array([x,y,z]).reshape(3,-1)
fig = plt.figure(figsize=(5,5))
ax = fig.add_subplot(projection='3d')
ax.grid()
lines = ax.scatter(x, y, z, marker='.', c=cs)
def animate(n):
# 按照xyz顺序旋转
axis = [2,1,0]
shape = xyz.shape
lines._offsets3d = Rz(n)@xyz
return lines,
ani = animation.FuncAnimation(fig, animate,
range(0, 360, 2), interval=25, blit=True)
#plt.show()
ani.save("zyx.gif")
效果如下
来源:https://tinycool.blog.csdn.net/article/details/128914226
标签:Python,地球,旋转
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
tensorflow之自定义神经网络层实例
2022-05-26 07:05:27
Pytorch实现将label变成one hot编码的两种方式
2021-08-26 08:45:00
php处理抢购类功能的高并发请求
2023-11-21 01:30:54
![](https://img.aspxhome.com/file/2023/6/94496_0s.jpg)
asp版FCKEditor编辑器的用法
2008-07-05 12:15:00
Oracle 自增(auto increment) 或 标识字段的建立方法
2009-03-06 11:15:00
PHP中error_reporting()函数的用法(修改PHP屏蔽错误)
2023-11-20 01:08:17
在Python中使用sort()方法进行排序的简单教程
2022-08-21 07:05:47
Mac下python包管理工具pip的安装
2023-11-19 11:09:46
![](https://img.aspxhome.com/file/2023/1/87701_0s.png)
如何在Win下mysql备份恢复命令
2010-03-03 17:23:00
女装类视觉设计分享
2009-10-30 18:36:00
![](https://img.aspxhome.com/file/UploadPic/200910/30/1256637795938-73s.jpg)
利用Pycharm断点调试Python程序的方法
2023-07-05 03:47:08
![](https://img.aspxhome.com/file/2023/2/97242_0s.jpg)
Python socket聊天脚本代码实例
2023-02-02 21:33:16
Django使用消息提示简单的弹出个对话框实例
2023-02-08 06:23:07
![](https://img.aspxhome.com/file/2023/1/66911_0s.jpg)
python 实现超级玛丽游戏
2023-10-10 09:38:38
![](https://img.aspxhome.com/file/2023/9/70049_0s.gif)
PHP常用字符串操作函数实例总结(trim、nl2br、addcslashes、uudecode、md5等)
2023-10-02 13:10:01
Python使用pymongo模块操作MongoDB的方法示例
2023-03-26 08:58:46
python正则表达式去除两个特殊字符间的内容方法
2023-08-24 16:22:10
举例讲解Python面向对象编程中类的继承
2022-02-09 02:59:14
Python numpy大矩阵运算内存不足如何解决
2022-08-06 22:38:21
![](https://img.aspxhome.com/file/2023/9/92509_0s.png)
python 判断一个进程是否存在
2021-06-21 02:19:24