简单的例子

参考:https://matplotlib.org/、https://blog.csdn.net/u011630575/article/details/79409202

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

fig = plt.figure()
#‘111’表示放置一行一列的图,图放置值1处;图的形式的3D
ax1 = fig.add_subplot(111, projection='3d')

x = [1,2,3,4,5,6,7,8,9,10]
y = [5,6,7,8,2,5,6,3,7,2]

x,y=np.meshgrid(x,y) #返回的是x,y的所有点的横纵坐标集合
print(x)
print(y)
'''x的返回值
[[ 1  2  3  4  5  6  7  8  9 10]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 1  2  3  4  5  6  7  8  9 10]
 [ 1  2  3  4  5  6  7  8  9 10]]
'''
'''y的返回值
[[5 5 5 5 5 5 5 5 5 5]
 [6 6 6 6 6 6 6 6 6 6]
 [7 7 7 7 7 7 7 7 7 7]
 [8 8 8 8 8 8 8 8 8 8]
 [2 2 2 2 2 2 2 2 2 2]
 [5 5 5 5 5 5 5 5 5 5]
 [6 6 6 6 6 6 6 6 6 6]
 [3 3 3 3 3 3 3 3 3 3]
 [7 7 7 7 7 7 7 7 7 7]
 [2 2 2 2 2 2 2 2 2 2]]
'''
z = np.array([
		[1,2,6,3,2,7,3,3,7,2],
		[1,2,6,3,2,7,3,3,7,2],
		[1,2,6,3,2,7,3,3,7,2],
		[1,2,6,3,2,7,3,3,7,2],
		[1,2,6,3,2,7,3,3,7,2],
		[1,2,6,3,2,7,3,3,7,2],
		[1,2,6,3,2,7,3,3,7,2],
		[1,2,6,3,2,7,3,3,7,2],
		[1,2,6,3,2,7,3,3,7,2],
		[1,2,6,3,2,7,3,3,7,2],
	])

ax1.plot_wireframe(x,y,z) #三个参数都是二维数据,画的是网线图

ax1.set_xlabel('x axis')
ax1.set_ylabel('y axis')
ax1.set_zlabel('z axis')

plt.show()

在这里插入图片描述

画图的整个过程

导入必要模块

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np   #得到、处理一些数据

设置图像显示的位置、形式

fig = plt.figure()   #得到作图变量
#‘111’表示放置一行一列的图,图放置值1处;图的形式的3D
ax1 = fig.add_subplot(111, projection='3d') # 图像显示的坐标

准备数据和处理成合适的格式

x = [1,2,3,4,5,6,7,8,9,10]
y = [5,6,7,8,2,5,6,3,7,2]

x,y=np.meshgrid(x,y) #返回的是x,y的所有点的横纵坐标集合

下图为x,y处理后的二维数组,x被横向复制,y被转置后横向复制,这样可以组成对应的(x,y)形式的所有点的位置
在这里插入图片描述
z方向的数据可以是和x,y的值有关的数据,例中直接给了固定的值,但也需要是二维的数组,因为对于(x,y)的每一个点,都有一个z值

选择合适的图像格式

上例中使用的是线框图,还可以使用饼图(pie),散点图(scatter),条形图(bar)等

ax1.scatter(x, y, z, c='g', marker='o')
ax1.bar3d(x3, y3, z3, dx, dy, dz)
ax1.plot_wireframe(x,y,z, rstride = 3, cstride = 3)
ax1.plot_surface(X, Y, Z, cmap='rainbow')

其他修饰性的参数

ax1.set_xlabel('x axis') #x轴名称
ax1.set_ylabel('y axis') #y轴名称
ax1.set_zlabel('z axis') #z轴名称

图的显示

使用plt.show()

画一个3d球形

对应关系分析,代码中数据的 t 和 s 有两倍的关系,且发现x^2 + y ^2 = (sin s)^2, z=cos(s)所以x^2+ y^ 2+z^2=1,的球体,

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

t = np.linspace(0, np.pi * 2, 100)
s = np.linspace(0, np.pi, 100)

t, s = np.meshgrid(t, s)
x = np.cos(t) * np.sin(s)
y = np.sin(t) * np.sin(s)
z = np.cos(s)
# z[>0]=0  # 截取球体的下半部分
ax = plt.subplot(111, projection='3d')
# ax = plt.subplot(121, projection='3d')
# ax.plot_wireframe(x, y, z)
# ax = plt.subplot(122, projection='3d')
# ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
# ax = plt.subplot(122, projection='3d')
ax.set_xlabel('x axis') #x轴名称
ax.set_ylabel('y axis') #y轴名称
ax.set_zlabel('z axis') 
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
# plt.gca().set_box_aspect((2,2,1)) #设置坐标比例时2:2:1
plt.show()

在这里插入图片描述

画一个3d锥形

对应关系分析,代码中数据的 t 和 s 有两倍的关系,且发现x^2 + y ^2 = (sin s)^2,使得z=2- sin(s),最终,x ^ 2+y ^ 2 + z =2,也就是锥形

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

t = np.linspace(0, np.pi * 2, 100)
s = np.linspace(0, np.pi, 100)
t, s = np.meshgrid(t, s)

x = np.cos(t) * np.sin(s)
y = np.sin(t) * np.sin(s)
z = 2-np.sin(s)


ax.set_xlabel('x axis') #x轴名称
ax.set_ylabel('y axis') #y轴名称
ax.set_zlabel('z axis') 
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
plt.show()

在这里插入图片描述

画一个3d漏斗

这和上面锥形的代码只是x,y,z后边的sin s,换成了cos s。

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

t = np.linspace(0, np.pi * 2, 100)
s = np.linspace(0, np.pi, 100)
t, s = np.meshgrid(t, s)

x = np.cos(t) * np.cos(s)
y = np.sin(t) * np.cos(s)
z = 2-np.cos(s)


ax.set_xlabel('x axis') #x轴名称
ax.set_ylabel('y axis') #y轴名称
ax.set_zlabel('z axis') 
ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap='rainbow')
plt.show()

在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐