matplotlib绘制3D圣诞树

import matplotlib.pyplot as plt
import numpy as np
import mpl_toolkits.mplot3d.art3d as art3d
import matplotlib.patches as patches

from matplotlib.font_manager import FontProperties
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.path import Path
from matplotlib.patches import Circle

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111,projection = '3d',proj_type='ortho')
ax.view_init(elev=0, azim=0)

arr = [-0.7,0.46,1.53]

#圆柱体树干

def shugan():
    cta = np.linspace(0,2 * np.pi,20)
    h = np.linspace(0,1,10)
    r = 0.3

    xg = np.outer(r * np.cos(cta),np.ones(len(h)))
    yg = np.outer(r * np.sin(cta),np.ones(len(h)))
    zg = np.outer(np.ones(len(cta)),h) - 1 - 0.75
    
    ax.plot_surface(xg,yg,zg,cmap = plt.get_cmap('Greys_r'))

#双曲线树冠

def shuguan(n):
    u = np.linspace(0, 2 * np.pi, 20)
    v = np.linspace(0, np.pi, 20)
    
    x = 1.5**(2-n)*np.outer(np.cos(u), np.sin(v)) 
    y = 1.5**(2-n)*np.outer(np.sin(u), np.sin(v))
    z = -pow(x**2+y**2,1/3) + 1 + 0.75*n

    ax.plot_surface(x, y, z, cmap=plt.get_cmap('Greens_r'))

#树冠顶部平面伯利恒之星

def wujiaoxing():
    r = 0.5
    angles = np.linspace(0,2*np.pi,5,endpoint = False)

    x = r * np.sin(angles)
    y = r * np.cos(angles)+2.8
    
    verts = [(x[2],y[2]), (x[0],y[0]), (x[3],y[3]), (x[1],y[1]), (x[4],y[4]),(x[2],y[2]),]
     
    codes = [Path.MOVETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.LINETO,Path.CLOSEPOLY,]
     
    path = Path(verts, codes)
    patch = patches.PathPatch(path, facecolor='orange',lw=0)
     
    ax.add_patch(patch)
    art3d.pathpatch_2d_to_3d(patch,z=0, zdir="x")

#散点装饰

def mantianxing():
    np.random.seed(19680801)
    n = 20

    def randrange(n, vmin, vmax):      
        return (vmax - vmin)*np.random.rand(n) + vmin
    
    for m, zlow, zhigh in [('x', -1, 3), ('*', -1.5, 2)]:
        xs = randrange(n, -3,2)
        ys = randrange(n, -2,2)
        zs = randrange(n, zlow, zhigh)
        ax.scatter(xs, ys, zs, marker=m)

def writeTitle(s):
    font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=25)
    plt.suptitle(s,x = 0.55,y = 0.78,fontproperties=font)

def main():
    shugan()
    for i in range(3):
        shuguan(i)

    wujiaoxing()
    mantianxing()
    writeTitle("圣诞节快乐!")
    
    plt.axis('off')
    plt.show()

main()
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值