3D图怎么画(Axes3D)

  • 时间:
  • 来源:互联网
  • 文章标签:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
# 还有一种表现形式:ax = fig.add_subplot(111,projection = '3d')
# ax = fig.gca(projection='3d')


x = np.arange(-4,4,0.25)
y = np.arange(-4 ,4,0.25)

res= X,Y=np.meshgrid(x,y) #个人博客网页有专门描述meshgrid()的版块

Z = np.sin(np.sqrt(X**2+Y**2))
ax.plot_surface(X,Y,Z,rstride = 1,cstride = 1,cmap = 'rainbow_r')#画表面图
ax.contour(X,Y,Z,zdim ='z',offset = -2,cmap = 'rainbow')#画投影

ax.set(zlim = (-2,2))  
plt.show()
# rstride :网图表面格子的大小,可以自行调节大小去观察
fig = plt.figure()
ax = fig.gca(projection='3d')

theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)

ax.plot(x,y,z)
plt.show()

3D散点图

np.random.seed(1)
def randrange(n,vmin,vmax):
    return (vmax-vmin)*np.random.rand(n)+vmin


fig = plt.figure()
ax = fig.gca(projection='3d')
n = 100
for c,m,zlow,zhigh in [('r','o',-50,-25),('b','x','-30','-5')]:
    xs = randrange(n,23,32)
    ys = randrange(n,0,100)
    zs = randrange(n,int(zlow),int(zhigh))
    ax.scatter(xs,ys,zs,c=c,marker=m)
    
ax.view_init(40,0)#可以调节视角
plt.show()

3D条形图

fig = plt.figure()  
ax = fig.add_subplot(111, projection='3d') 

for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]): 
    xs = np.arange(20)
    ys = np.random.rand(20)
    cs = [c]*len(xs) # 颜色
    ax.bar(xs,ys,zs = z,zdir='y',color = cs,alpha = 0.5)
plt.show()

本文链接http://www.taodudu.cc/news/show-1781776.html