前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >🤠 Merry Xmas | 用Matplotlib画个3D圣诞树送给你!~

🤠 Merry Xmas | 用Matplotlib画个3D圣诞树送给你!~

作者头像
生信漫卷
发布2023-02-24 14:20:37
3010
发布2023-02-24 14:20:37
举报
文章被收录于专栏:R语言及实用科研软件

1写在前面

Merry Christmas ! 🥳

过完圣诞就要跨年了,希望2023年自己可以一扫霉运,顺顺利利!!!😘

从网上抄了个英文的祝福语送给大家(主要是懒): 👇

I hope Santa is good to you this year because you only deserve the best🥰.


今天就用pythonmatplotlib给大家画个圣诞树吧🎄。

2用到的包

代码语言:javascript
复制
import math
import matplotlib.pyplot as plt
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D

3画个圣诞树🎄

代码语言:javascript
复制
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)

Note! 是有动画的哦,这里就不展示了,大家有兴趣试一下。🤪


最后祝大家圣诞快乐!~


本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-12-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 生信漫卷 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1写在前面
  • 2用到的包
  • 3画个圣诞树🎄
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档