使用 mplot3d 绘制 3D 网格 [英] Plot 3D mesh using mplot3d

查看:36
本文介绍了使用 mplot3d 绘制 3D 网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一个包含 3D 表面点坐标的文本文件开始,它由于施加在其上的压力以及根据以下示例施加的压力值而变形:

<块引用>

 Node_label X_in Y_in Z_in X_def Y_def Z_def 按11542 15229 17734 18332 11.4645 67.7709 138.905 4.97573E-0311543 3283 3238 16784 7.73624 67.3238 138.781 13.2628E-0311540 13506 13385 17482 18.9023 67.6291 139.051 3.61705E-0311541 7637 7516 18637 15.2164 68.0038 139.031 12.7343E-0311546 16137 16651 16886 -2.98896 66.1776 138.431 19.0185E-0311547 7360 7361 16903 -6.42838 65.3547 138.177 2.74949E-03.... .... .... .... .... .... .... ....

我正在尝试使用 mplot3d 库绘制 3D 表面 + 其变形,以及变形表面上的压力的彩色轮廓图.这是我的代码:

from Tkinter import Tk从 tkFileDialog 导入 askopenfilename, asksaveasfile从 mpl_toolkits.mplot3d 导入 Axes3D导入 matplotlib.pyplot 作为 plt从 matplotlib 导入 cm从 matplotlib.ticker 导入 LinearLocator,FormatStrFormatter将 numpy 导入为 npTk().withdraw()f_in = askopenfilename(title='选择 TXT 文件')x_in = []y_in = []z_in = []x_def = []y_def = []z_def = []cpress = []使用 open(f_in,"r") 作为 f0:对于 ind,enumerate(f0) 中的行:如果 ind >2:item = line.strip()如果项目:item = item.split()x_in.append(item[0])y_in.append(item[1])z_in.append(item[2])x_def.append(项目[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)冲浪 = ax.plot_surface(x_in, y_in, z_in, cmap=cm.coolwarm,线宽=0,抗锯齿=假)plt.show()

但它没有绘制任何内容.

解决方案

来自 @ImportanceOfBeingErnest 评论,使用

starting from a text file containing the points coordinates of a 3D surface, its deformed due to a pressure applied on it and the values of the pressure applied according to the following example:

 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
       ....           ....           ....           ....           ....           ....           ....            ....

I am trying to plot a 3D surface + its deformed, together with a colored contour plot of the pressure on the deformed surface using mplot3d library. Here is my code:

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

But it does not plot anything.

解决方案

From @ImportanceOfBeingErnest comentary, using plot_trisurf:

[...]
with open(f_in,"r") as f0:
    for ind, line in enumerate(f0):
        if ind > 2:
            item = line.strip()
            if item:
                item = item.split()
                #Node_label = item[0]
                x_in.append(item[1])
                y_in.append(item[2])
                z_in.append(item[3])
                x_def.append(item[4])
                y_def.append(item[5])
                z_def.append(item[6])
                cpress.append(item[7])

# type is important to convert strings to numbers:
x_in = np.asarray(x_in, dtype=np.float64)
y_in = np.asarray(y_in, dtype=np.float64)
z_in = np.asarray(z_in, dtype=np.float64)

fig = plt.figure()
ax = fig.gca(projection='3d')

points = ax.scatter(x_in, y_in, z_in, cmap=cm.coolwarm, antialiased=False)

surface = ax.plot_trisurf(x_in, y_in, z_in, cmap=cm.coolwarm, antialiased=False)

fig.tight_layout()
fig.show()

这篇关于使用 mplot3d 绘制 3D 网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆