因为我是直接把jupyter notebook导出为md文件,然后传上知乎了,所以很多地方会乱掉

先来看两个例子

from mpl_toolkits.mplot3d import Axes3D

from matplotlib import cm

from matplotlib.ticker import LinearLocator

import matplotlib.pyplot as plt

import numpy as np

fig=plt.figure()

ax=fig.gca(projection=3d)
#ax = axes3d.Axes3D(fig)

[x,t]=np.meshgrid(np.array(range(25))/24.0,np.arange(0,575.5,0.5)/575*17*np.pi-2*np.pi)

p=(np.pi/2)*np.exp(-t/(8*np.pi))

u=1-(1-np.mod(3.6*t,2*np.pi)/np.pi)**4/2

y=2*(x**2-x)**2*np.sin(p)

r=u*(x*np.sin(p)+y*np.cos(p))

surf=ax.plot_surface(r*np.cos(t),r*np.sin(t),u*(x*np.cos(p)-y*np.sin(p)),rstride=1,cstride=1,cmap=cm.gist_rainbow_r,

linewidth=0,antialiased=True)

plt.show()

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

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

# Make data
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))

# Plot the surface
#ax.plot_surface(x, y, z, color=b)
ax.plot_surface(x, y, z,cmap=rainbow)

plt.show()

Python 三维绘图

1.创建三维坐标轴对象Axes3D

创建Axes3D主要有两种方式,一种是利用关键字projection=3dl来实现,另一种则是通过从mpl_toolkits.mplot3d导入对象Axes3D来实现,目的都是生成具有三维格式的对象Axes3D.

#方法一,利用关键字导入相关模块绘制
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#定义坐标轴
fig = plt.figure()
ax1 = plt.axes(projection=3d)
#ax = fig.add_subplot(111,projection=3d) #这种方法也可以画多个子图

#方法二,利用三维轴方法
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#定义图像和三维格式坐标轴
fig=plt.figure()
ax2 = Axes3D(fig)

2.三维曲线和散点

随后在定义的坐标轴上画图:

#方法一,利用关键字
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#定义坐标轴
fig = plt.figure()
ax1 = plt.axes(projection=3d)
#ax = fig.add_subplot(111,projection=3d) #这种方法也可以画多个子图

"""
#方法二,利用三维轴方法
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

#定义图像和三维格式坐标轴
fig=plt.figure()
ax2 = Axes3D(fig)
"""
##################我是分割线#####################
import numpy as np
z = np.linspace(0,13,1000)
x = 5*np.sin(z)
y = 5*np.cos(z)
zd = 13*np.random.random(100)
xd = 5*np.sin(zd)
yd = 5*np.cos(zd)
ax1.scatter3D(xd,yd,zd, cmap=b) #绘制散点图
ax1.plot3D(x,y,z,gray) #绘制空间曲线
plt.show()

3.三维曲面

下一步画三维曲面:

fig = plt.figure() #定义新的三维坐标轴
ax3 = plt.axes(projection=3d)

#定义三维数据
xx = np.arange(-5,5,0.5)
yy = np.arange(-5,5,0.5)
X, Y = np.meshgrid(xx, yy)
Z = np.sin(X)+np.cos(Y)

#作图
ax3.plot_surface(X,Y,Z,cmap=rainbow)
#ax3.contour(X,Y,Z, zdim=z,offset=-2,cmap=rainbow) #等高线图,要设置offset,为Z的最小值
plt.show()

如果加入渲染时的步长,会得到更加清晰细腻的图像: ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1,cmap=rainbow) 其中的row和cloum_stride为横竖方向的绘图采样步长,越小绘图越精细。

或者,改变xx,yy的步长,xx = np.arange(-5,5,0.1)

fig = plt.figure() #定义新的三维坐标轴
ax3 = plt.axes(projection=3d)

#定义三维数据
xx = np.arange(-5,5,0.1)
yy = np.arange(-5,5,0.1)
X, Y = np.meshgrid(xx, yy)
Z = np.sin(X)+np.cos(Y)

#作图
ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1,cmap=rainbow)
ax3.contour(X,Y,Z,offset=-2, cmap = rainbow)#绘制等高线
plt.show()

import math
fig = plt.figure(figsize=(40,30)) #定义新的三维坐标轴
ax3 = plt.axes(projection=3d)

#定义三维数据
xx = np.arange(-20,20,0.5)
yy = np.arange(-20,20,0.5)
X, Y = np.meshgrid(xx, yy)#将两个一维数组变为二维矩阵
Z = X*Y**2

#作图
#ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1)
ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1,cmap=rainbow)
plt.savefig(1.png,dpi=80)
plt.show()

np.meshgrid使用方法 链接

x = np.arange(-2,2)
x
array([-2, -1, 0, 1])

y = np.arange(-2,2)
y
array([-2, -1, 0, 1])

x1,y1 = np.meshgrid(x,y)

x1
array([[-2, -1, 0, 1],
[-2, -1, 0, 1],
[-2, -1, 0, 1],
[-2, -1, 0, 1]])

y1
array([[-2, -2, -2, -2],
[-1, -1, -1, -1],
[ 0, 0, 0, 0],
[ 1, 1, 1, 1]])

X
array([[-20. , -19.5, -19. , ..., 18.5, 19. , 19.5],
[-20. , -19.5, -19. , ..., 18.5, 19. , 19.5],
[-20. , -19.5, -19. , ..., 18.5, 19. , 19.5],
...,
[-20. , -19.5, -19. , ..., 18.5, 19. , 19.5],
[-20. , -19.5, -19. , ..., 18.5, 19. , 19.5],
[-20. , -19.5, -19. , ..., 18.5, 19. , 19.5]])

Y
array([[-20. , -20. , -20. , ..., -20. , -20. , -20. ],
[-19.5, -19.5, -19.5, ..., -19.5, -19.5, -19.5],
[-19. , -19. , -19. , ..., -19. , -19. , -19. ],
...,
[ 18.5, 18.5, 18.5, ..., 18.5, 18.5, 18.5],
[ 19. , 19. , 19. , ..., 19. , 19. , 19. ],
[ 19.5, 19.5, 19.5, ..., 19.5, 19.5, 19.5]])

Examples

fig = plt.figure(figsize=(8,6)) #定义新的三维坐标轴
ax3 = plt.axes(projection=3d)

#定义三维数据
xx = np.arange(-20,20,0.5)
yy = np.arange(-20,20,0.5)
X, Y = np.meshgrid(xx, yy)#将两个一维数组变为二维矩阵
Z = X+Y**2
plt.title(Z = X+Y**2)#添加标题

#作图
#ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1)
ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1,cmap=rainbow)
plt.show()

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(8,6))
ax3 = plt.axes(projection=3d)

xx = np.arange(-20,20,0.5)
yy = np.arange(-20,20,0.5)
X, Y = np.meshgrid(xx, yy)

Z=X**2+Y**2#Change Here

ax3.plot_surface(X,Y,Z,rstride = 1, cstride = 1,cmap=rainbow)
plt.show()

参考:

blog.csdn.net/u01463624

cnblogs.com/gengyi/p/94


推荐阅读:
相关文章