Python使用Matplotlib绘制3D圣诞树

 更新时间:2023年12月28日 16:26:51   作者:kuan_li_lyg  
这篇文章主要为大家详细介绍了Python如何使用Matplotlib绘制3D圣诞树,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

前言

因为我们把圣诞树安装在暖气电池旁边,所以它很快就死了。所以我决定用 Matplotlib 绘制一棵圣诞树。你不需要为它遮阳避暑,它也不需要任何水。在阿瑞克斯星球,水的供应是有限的。地球上也是如此。 

步骤

要在 matplotlib 中绘图,我们需要将其包含在内。

此外,我们还要为 3D 准备所有库。

import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")

让我们先画一个 3D 圆,确保一切正常。

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
k=300
Z = [10 for i in range(k)]
X = [math.cos(i/10) for i in range(k)]
Y = [math.sin(i/10) for i in range(k)]
ax.scatter(X,Y,Z, c="green", marker="^")
plt.show()

不错!这是非常标准的。我们现在只修复 Z 坐标。

现在,应用 Z 坐标使其 3D 化。

Z = [i for i in range(k)]

让我们在顶部缩小圆的半径。

Z = [i for i in range(k)]
X = [math.cos(i/5)*(k-i) for i in range(k)]
Y = [math.sin(i/5)*(k-i) for i in range(k)]

Matplotlib 总是倾向于贴合图形,只需在此处添加这些限制即可:

plt.xlim(-500,500)
plt.ylim(-500,500)

 画一些红圈。它们的公式相同,但步长更大。我们还通过在 sin 和 cos 参数上加 2 来移动它,这样它们就不会与树本身相交。

k=300
Z = [i for i in range(k)]
X = [math.cos(i/5)*(k-i) for i in range(k)]
Y = [math.sin(i/5)*(k-i) for i in range(k)]
ax.scatter(X,Y,Z, c="green", marker="^")
k=300
step = 4
Z = [i for i in range(1,k,step)]
X = [math.cos(i/5+2)*(k-i+10) for i in range(1,k,step)]
Y = [math.sin(i/5+2)*(k-i+10) for i in range(1,k,step)]
ax.scatter(X,Y,Z, c="red", marker="o")
plt.xlim(-500,500)
plt.ylim(-500,500)
plt.show()

微调装饰

c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
ax.scatter(X,Y,Z, c=c, marker="o",s=40)

要旋转树形图,我们需要为每一帧绘制树形图,并在 sin 和 cos 参数中添加一些常数。

我们为初始图形和每一帧复制代码。

import math
import matplotlib.pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection="3d")
def init():
    k=300
    Z = [i for i in range(k)]
    X = [math.cos(i/5)*(k-i) for i in range(k)]
    Y = [math.sin(i/5)*(k-i) for i in range(k)]
    ax.scatter(X,Y,Z, c="green", marker="^")
    step = 3
    c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
    Z = [i for i in range(1,k,step)]
    X = [math.cos(i/5+2)*(k-i+10) for i in range(1,k,step)]
    Y = [math.sin(i/5+2)*(k-i+10) for i in range(1,k,step)]
    ax.scatter(X,Y,Z, c=c, marker="o",s=40)
    plt.xlim(-500,500)
    plt.ylim(-500,500)
    return fig,
def animate(f):
    fig.clear()
    ax = fig.add_subplot(111, projection="3d")
    k=300
    Z = [i for i in range(k)]
    X = [math.cos(i/5+f/10)*(k-i) for i in range(k)]
    Y = [math.sin(i/5+f/10)*(k-i) for i in range(k)]
    ax.scatter(X,Y,Z, c="green", marker="^")
    step = 3
    c = [(i/k,abs(0.5-i/k),i/k) for i in range(1,k,step)]
    Z = [i for i in range(1,k,step)]
    X = [math.cos(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
    Y = [math.sin(i/5+2+f/10)*(k-i+10) for i in range(1,k,step)]
    ax.scatter(X,Y,Z, c=c, marker="o",s=40)
    plt.xlim(-500,500)
    plt.ylim(-500,500)
    return fig,
ani=animation.FuncAnimation(fig, animate, init_func=init,
                               frames=90, interval=50, blit=True)
ani.save("christmas_tree.mp4")

这就是结果:

到此这篇关于Python使用Matplotlib绘制3D圣诞树的文章就介绍到这了,更多相关Python Matplotlib绘制3D圣诞树内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python去掉字符串中重复字符的方法

    python去掉字符串中重复字符的方法

    这篇文章主要介绍了python去掉字符串中重复字符的方法,需要的朋友可以参考下
    2014-02-02
  • Python结合OpenCV实现简单的图像拼接功能

    Python结合OpenCV实现简单的图像拼接功能

    这篇文章主要为大家详细介绍了Python如何使用OpenCV实现简单图像拼接的核心概念与技术要点,主要内容包括图像获取,处理,特征提取等关键环节,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下
    2026-05-05
  • python 偷懒技巧——使用 keyboard 录制键盘事件

    python 偷懒技巧——使用 keyboard 录制键盘事件

    这篇文章主要介绍了python如何使用 keyboard 录制键盘事件,帮助大家提高工作效率,感兴趣的朋友可以了解下
    2020-09-09
  • 超详细Python解释器新手安装教程

    超详细Python解释器新手安装教程

    这篇文章主要介绍了超详细Python解释器新手安装教程,文中有非常详细的图文示例,对不会安装python解释器的小伙伴们很有帮助哟,需要的朋友可以参考下
    2021-05-05
  • Selenium中的option使用示例

    Selenium中的option使用示例

    这篇文章主要介绍了Selenium中的option用法实例,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • Python实现删除Android工程中的冗余字符串

    Python实现删除Android工程中的冗余字符串

    这篇文章主要介绍了Python实现删除Android工程中的冗余字符串,本文实现的是删除Android资源(语言)国际化机制中的一些冗余字符串,需要的朋友可以参考下
    2015-01-01
  • 最基础的Python的socket编程入门教程

    最基础的Python的socket编程入门教程

    这篇文章主要介绍了最基础的Python的socket编程入门教程,包括最基本的发送和接受信息等内容,需要的朋友可以参考下
    2015-04-04
  • 使用python装饰器验证配置文件示例

    使用python装饰器验证配置文件示例

    项目中用到了一个WriteData的函数保存用户填写的配置,为了实现验证用户输入的需求,在不影响接口的使用的前提下,采用了python的装饰器实现,代码片段演示了如何验证WriteData函数的输入参数
    2014-02-02
  • python 如何将office文件转换为PDF

    python 如何将office文件转换为PDF

    这篇文章主要介绍了python 如何将office文件转换为PDF,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-09-09
  • TensorFlow的reshape操作 tf.reshape的实现

    TensorFlow的reshape操作 tf.reshape的实现

    这篇文章主要介绍了TensorFlow的reshape操作 tf.reshape的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04

最新评论