一.学习内容
1.绘制3D图表和统计表(mplot3d)
2.绘制3D线框图
3…绘制3D曲面图
4.实例1:三维空间的星星
5.实例2:三维空间闪烁的星星
6.代码及运行结果
(1)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
#获取测试数据
X,Y,z= axes3d.get_test_data(0.05)
#绘制3d线框图
ax.plot_wireframe(X,Y,z,rstride=10,cstride=10)
plt.title('2020080603043')
plt.show()
#绘制3D曲面图
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
import numpy as np
x1 = np.arange(-5,5,0.25)
y1 = np.arange(-5,5,0.25)
x1,y1 =np.meshgrid(x1,y1)
r1 = np.sqrt(x1**2+y1**2)
z1 = np.sin(r1)
fig =plt.figure()
ax = fig.add_subplot(111,projection='3d')
#绘制曲面图
ax.plot_surface(x1,y1,z1,cmap=cm.coolwarm,linewidth=0,antialiased=False)
ax.set_zlim(-1.01,1.01)
plt.title('2020080603043')
plt.show()
#实例1:三维空间的星星
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
plt.rcParams['font.sans-serif']=["SimHei"]
plt.rcParams['axes.unicode_minus'] = False
#测试数据
x = np.random.randint(0,40,30)
y = np.random.randint(0,40,30)
z = np.random.randint(0,40,30)
#创建三维坐标系
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
for xx,yy,zz,in zip(x,y,z):
color ='y'
if 10 < zz < 20:
color = '#c71585'
elif zz >= 20:
color= '#008B8B'
ax.scatter(xx,yy,zz,c=color,marker='*',s=160,linewidth=1,edgecolor='black')
ax.set_xlabel('x轴')
ax.set_xlabel('y轴')
ax.set_xlabel('z轴')
ax.set_title('3D散点图 2020080603043',fontproperties='simhei',fontsize=14)
plt.tight_layout()
plt.show()
#实例2三维空间星星
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from matplotlib.animation import FuncAnimation
plt.rcParams['font.sans-serif']=["SimHei"]
plt.rcParams['axes.unicode_minus'] = False
#生成测试数据
xx = np.array([13,5,25,13,9,19,3,39,13,27])
yy = np.array([4,38,16,26,7,19,28,10,17,18])
zz = np.array([7,19,6,12,25,19,23,25,10,15])
fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
#绘制初始的3D散点图
star = ax.scatter(xx,yy,zz,c='#C71585',marker='*',s=160,linewidth=1,edgecolor='black')
#每帧动画调用的函数
def animate(i):
if i % 2:
color = '#C71585'
else:
color = 'while'
next_star=ax.scatter(xx,yy,zz,c=color,marker='*',s=160,linewidth=1,edgecolor='black')
return next_star
def init():
return star
ain = FuncAnimation(fig=fig,func=animate,frames=None,init_func=init,interval=1000,blit=False)
ax.set_xlabel('x轴')
ax.set_ylabel('y轴')
ax.set_zlabel('z轴')
ax.set_title('3D散点图 2020080603043')
plt.tight_layout()
plt.show()