如何使用 Matplotlib 绘制 3D 圣诞树

系列文章目录

前言

转自:How to draw a 3D Christmas Tree with Matplotlib | by Timur Bakibayev, Ph.D. | Analytics Vidhya | Mediumhttps://medium.com/analytics-vidhya/how-to-draw-a-3d-christmas-tree-with-matplotlib-aabb9bc27864

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


一、步骤

1.1

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

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

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

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

python 复制代码
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 化。

python 复制代码
Z = [i for i in range(k)]

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

python 复制代码
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 总是倾向于贴合图形,只需在此处添加这些限制即可:

python 复制代码
plt.xlim(-500,500)
plt.ylim(-500,500)

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

python 复制代码
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()

微调装饰

python 复制代码
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 参数中添加一些常数。

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

python 复制代码
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")

这就是结果:

不要忘记与您的朋友分享这棵树!

新年快乐

点击此处查看我的其他文章:timurbakibayev.medium.com

timurbakibayev.medium.comhttp://timurbakibayev.medium.com/

相关推荐
未来智慧谷13 分钟前
微软医疗AI诊断系统发布 多智能体协作实现疑难病例分析
人工智能·microsoft·医疗ai
野生技术架构师14 分钟前
简述MCP的原理-AI时代的USB接口
人工智能·microsoft
虾球xz20 分钟前
CppCon 2018 学习:EFFECTIVE REPLACEMENT OF DYNAMIC POLYMORPHISM WITH std::variant
开发语言·c++·学习
Allen_LVyingbo25 分钟前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
jndingxin27 分钟前
OpenCV中超分辨率(Super Resolution)模块类cv::dnn_superres::DnnSuperResImpl
人工智能·opencv·dnn
小哈龙29 分钟前
裸仓库 + Git Bash 搭建 本地 Git 服务端与客户端
开发语言·git·bash
NAGNIP30 分钟前
一文搞懂FlashAttention怎么提升速度的?
人工智能·算法
智能砖头33 分钟前
LangChain 与 LlamaIndex 深度对比与选型指南
人工智能·python
大师兄带你刨AI34 分钟前
「AI产业」| 《中国信通院&华为:智能体技术和应用研究报告》
大数据·人工智能
老周聊大模型1 小时前
《ChatGLM/Llama调优实战:从指令微调到RLHF的工业级对齐方案》
人工智能·程序员·架构