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()