在三维空间绘制点,线,面
1.绘制点
用scatter()散点绘制三维坐标点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
dot1 = [[ 0 , 0 , 0 ], [ 1 , 1 , 1 ], [
2 , 2 , 2 ], [ 2 , 2 , 3 ], [ 2 , 2 , 4 ]] # 得到五个点
plt.figure() # 得到画面
ax1 = plt.axes(projection = '3d' )
ax1.set_xlim( 0 , 5 ) # X轴,横向向右方向
ax1.set_ylim( 5 , 0 ) # Y轴,左向与X,Z轴互为垂直
ax1.set_zlim( 0 , 5 ) # 竖向为Z轴
color1 = [ 'r' , 'g' , 'b' , 'k' , 'm' ]
marker1 = [ 'o' , 'v' , '1' , 's' , 'H' ]
i = 0
for x in dot1:
ax1.scatter(x[ 0 ], x[ 1 ], x[ 2 ], c = color1[i],
marker = marker1[i], linewidths = 4 ) # 用散点函数画点
i + = 1
plt.show()
|
2.绘制线
函数plot3D(xs, ys, *args, zdir=‘z', **kwargs),用于绘制三维坐标的线,其参数使用说明如下.
(1)xs,ys,zdir=‘z': 设置(x,y,z)坐标值,为集合对象,是该函数与plot()的唯一区别.
(2) kwargs:接受键值对参数,使用方法同plot()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
plt.figure()
ax = plt.subplot( 111 , projection = '3d' )
ax.set_xlim( 0 , 20 ) # X轴,横向向右方向
ax.set_ylim( 20 , 0 ) # Y轴,左向与X,Z轴互为垂直
ax.set_zlim( 0 , 20 ) # 竖向为Z轴
z = np.linspace( 0 , 4 * np.pi, 500 )
x = 10 * np.sin(z)
y = 10 * np.cos(z)
ax.plot3D(x, y, z, 'black' ) # 绘制黑色空间曲线
# ----------------------------------------------------------
z1 = np.linspace( 0 , 4 * np.pi, 500 )
x1 = 5 * np.sin(z1)
y1 = 5 * np.cos(z1)
ax.plot3D(x1,y1,z1, 'g--' ) #绘制绿色空间虚曲线
#------------------------------------------------------------
ax.plot3D([ 0 , 18 , 0 ],[ 5 , 18 , 10 ],[ 0 , 5 , 0 ], 'om-' ) #绘制带o折线
plt.show()
|
3.绘制面
3D 图形需要的数据与等高线图基本相同:X、Y 数据决定坐标点,Z 轴数据决定 X、Y 坐标点对应的高度。与等高线图使用等高线来代表高度不同,3D 图形将会以更直观的形式来表示高度。
为了绘制 3D 图形,需要调用 Axes3D 对象的 plot_surface()方法来完成。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
fig = plt.figure(figsize = ( 15 , 5 ))
ax = fig.add_subplot( 131 , projection = '3d' ) # 第一个绘图区
x = np.arange( 1 , 50 , 1 )
y = np.arange( 1 , 50 , 1 )
X, Y = np.meshgrid(x, y) # 将坐标向量(x,y)变为坐标矩阵(X,Y)
def Z(X, Y): # 自定义求Z向量的函数
return X * 0.2 + Y * 0.3 + 20
s1 = ax.plot_surface(X, Y, Z(X, Y), rstride = 10 ,
cstride = 10 , cmap = cm.jet, linewidth = 1 ,
antialiased = True ) #绘制面
ax.set_xlim3d( 0 , 50 ) #指定x轴坐标值范围
ax.set_ylim3d( 0 , 50 ) #指定y轴坐标值范围
ax.set_zlim3d( 0 , 50 ) #指定z轴坐标值范围
fig.colorbar(s1,shrink = 1 ,aspect = 5 )
#------------------------------------------
ax1 = fig.add_subplot( 132 ,projection = '3d' ) #第二个绘图区
s2 = ax1.plot_surface(X,Y,Z(X,Y),rstride = 1 ,
cstride = 1 , cmap = cm.jet, linewidth = 1 ,
antialiased = False ) #绘制面
fig.colorbar(s2,shrink = 0.5 ,aspect = 5 )
#--------------------------------------------
d = 0.05
x1 = np.arange( - 4 , 4 ,d)
y1 = np.arange( - 3 , 3 ,d)
X1,Y1 = np.meshgrid(x1,y1)
def Z1(X,Y): #自定义求z向量的函数
z1 = np.exp( - X * * 2 - Y * * 2 )
z2 = np.exp( - (X - 1 ) * * 2 - (Y - 1 ) * * 2 )
return (z2 - z1) * 2 #返回Z坐标值
ax2 = fig.add_subplot( 133 ,projection = '3d' )
s3 = ax2.plot_surface(X,Y,Z(X,Y),rstride = 1 ,
cstride = 1 , cmap = cm.jet, linewidth = 1 ,
antialiased = False )
fig.colorbar(s3,shrink = 0.5 ,aspect = 5 )
plt.show()
|
到此这篇关于Python中三维坐标空间绘制的实现的文章就介绍到这了,更多相关Python 三维坐标空间内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/changshupx/article/details/108708714