【发布时间】:2017-03-28 13:53:11
【问题描述】:
从包含 3D 表面的点坐标的文本文件开始,由于施加在其上的压力以及根据以下示例施加的压力值而发生变形:
Node_label X_in Y_in Z_in X_def Y_def Z_def Press 11542 15229 17734 18332 11.4645 67.7709 138.905 4.97573E-03 11543 3283 3238 16784 7.73624 67.3238 138.781 13.2628E-03 11540 13506 13385 17482 18.9023 67.6291 139.051 3.61705E-03 11541 7637 7516 18637 15.2164 68.0038 139.031 12.7343E-03 11546 16137 16651 16886 -2.98896 66.1776 138.431 19.0185E-03 11547 7360 7361 16903 -6.42838 65.3547 138.177 2.74949E-03 .... .... .... .... .... .... .... ....
我正在尝试使用 mplot3d 库绘制 3D 表面 + 其变形,以及变形表面上压力的彩色等高线图。这是我的代码:
from Tkinter import Tk
from tkFileDialog import askopenfilename, asksaveasfile
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
Tk().withdraw()
f_in = askopenfilename(title='Choose the TXT file')
x_in = []
y_in = []
z_in = []
x_def = []
y_def = []
z_def = []
cpress = []
with open(f_in,"r") as f0:
for ind, line in enumerate(f0):
if ind > 2:
item = line.strip()
if item:
item = item.split()
x_in.append(item[0])
y_in.append(item[1])
z_in.append(item[2])
x_def.append(item[3])
y_def.append(item[4])
z_def.append(item[5])
cpress.append(item[6])
fig = plt.figure()
ax = fig.gca(projection='3d')
x_in = np.asarray(x_in)
y_in = np.asarray(y_in)
z_in = np.asarray(z_in)
surf = ax.plot_surface(x_in, y_in, z_in, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
plt.show()
但它没有绘制任何东西。
【问题讨论】:
-
插入
plot_surface的数组需要是二维的而不是一维的。 -
即使将 np.meshgrid(x_in, y_in) 添加到代码中也不起作用
-
列中的值是否真的形成了一个直线网格?如果没有,您需要先在网格上插入它们,或者使用
plot_trisurface。
标签: python matplotlib plot 3d