用python 画3d 图举例
文本如下
C 400 200 100 400 300 200
C 400 300 0 400 400 100
C 400 300 100 400 400 200
P 0 100 0 100 0 0
P 0 400 0 100 300 0
P 300 300 200 400 400 200
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
def max(a, b):
if(a < b):
return b
else:
return a
def min(a, b):
if(a < b):
return a
else:
return b
def showbn(c1, c2, c3, file, type, ax0, color='grey'):
# ax0 = fig.gca(projection='3d')
lines = []
with open(file, 'r') as f:
for line in f.readlines():
data = line.split()
lines.append(data)
f.close()
for i in range(len(lines)):
mcolor = color
if(len(lines[i]) < 14):
continue
if(lines[i][0] != type):
continue
x1 = min(float(lines[i][2]), float(lines[i][8]))
x2 = max(float(lines[i][2]), float(lines[i][8]))
y1 = min(float(lines[i][3]), float(lines[i][9]))
y2 = max(float(lines[i][3]), float(lines[i][9]))
z1 = min(float(lines[i][4]), float(lines[i][10]))
z2 = max(float(lines[i][4]), float(lines[i][10]))
if (x1 == x2):
yy = [y1, y2]
zz = [z1, z2]
Y, Z = np.meshgrid(yy, zz)
X = Z * 0 + x1
if(c1 == 1):
ax0.plot_surface(X, Y, Z, color=mcolor)
if(y1 == y2):
xx = [x1, x2]
zz = [z1, z2]
X, Z = np.meshgrid(xx, zz)
# print(y1)
Y = X*0+y1
if(c2 == 1):
ax0.plot_surface(X, Y, Z, color=mcolor)
if (z1 == z2):
xx = [x1, x2]
yy = [y1, y2]
X, Y = np.meshgrid(xx, yy)
Z = X * 0 + z1
if(c3 == 1):
ax0.plot_surface(X, Y, Z, color=mcolor)
return
if __name__ == "__main__":
plot_file = "/home/wsk/myfile/dataset/resdata/geor.txt"
fig = plt.figure()
ax0 = fig.add_subplot(111, projection='3d')
showbn(1, 1, 1, plot_file, "conduct", ax0, "grey")
showbn(1, 1, 1, plot_file, "port", ax0, "red")
plt.show()