使用mayavi绘制从文件中读取的三维数据

使用mayavi绘制从文件中读取的三维数据

本文介绍了使用mayavi绘制从文件中读取的三维数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据文件,格式如下:

  xyzf(x)f(y)f(z)

我想用mayavi的contour3d绘制它:

  def fill_array(output_array,source_array,nx,ny,nz,position):
代表范围内的i(nx):
代表范围内的j(ny ):
for k in range(nz):
output_array [i] [j] [k] = source_array [i] [j] [k] [position]

nx = 8
ny = 8
nz = 8

ndim = 6

x = np.zeros((nx,ny,nz))$ bx by = np.zeros((nx,ny,nz))
z = np.zeros((nx,ny,nz))
fx = np.zeros((nx,ny,nz) )
fy = np.zeros((nx,ny,nz))
fz = np.zeros((nx,ny,nz))

data_file = np.loadtxt ('datafile')
f = np.reshape(data_file,(nx,ny,nz,ndim))
fill_array(x,f,nx,ny,nz,0))
fill_array (y,f,nx,ny,nz,1)
fill_array(z,f,nx,ny,nz,2)
fill_array(fx,f,nx,ny,nz,3)$ $ b $ fill_array(fy,f,nx,ny,nz,5)

f2 = f x ** 2 + fy ** 2 + fz ** 2
plot_data = mlab.contour3d(f2)
mlab.colorbar(plot_data,title ='f2',orientation ='vertical')
mlab.savefig('f.png',放大倍数= 5)

数据按照(x,y,z)网格数据的顺序排列。但是,如果文件不是按顺序编写的,它会创建与有序数据不匹配的情节(我知道这是正确的)。可能是什么原因/解决方案?

当然,我只想安排x,y,z,然后将函数值f(x),f(y), f(z)到它的右边位置(x,y,z)。

解决方案

b在阅读文件后添加了以下内容(其余代码保持不变):

  data_file = np.loadtxt('datafile' ,''',浮点),(''',浮点),('''',浮点),('''浮点) fz',float)])
data_file = np.sort(data_file,order = ['z','y'])
f = np.array(data_file).reshape(nx,ny,nz )


I have a data file in the following format:

x y z f(x) f(y) f(z)

I want to plot it using contour3d of mayavi:

def fill_array(output_array,source_array,nx,ny,nz,position):
    for i in range(nx):
        for j in range(ny):
            for k in range(nz):
                output_array[i][j][k] = source_array[i][j][k][position]

nx = 8
ny = 8
nz = 8

ndim = 6

x = np.zeros((nx,ny,nz))
y = np.zeros((nx,ny,nz))
z = np.zeros((nx,ny,nz))
fx = np.zeros((nx,ny,nz))
fy = np.zeros((nx,ny,nz))
fz = np.zeros((nx,ny,nz))

data_file = np.loadtxt('datafile')
f = np.reshape(data_file, (nx,ny,nz,ndim))
fill_array(x,f,nx,ny,nz,0))
fill_array(y,f,nx,ny,nz,1)
fill_array(z,f,nx,ny,nz,2)
fill_array(fx,f,nx,ny,nz,3)
fill_array(fy,f,nx,ny,nz,4)
fill_array(fz,f,nx,ny,nz,5)

f2 = fx**2 + fy**2 + fz**2
plot_data = mlab.contour3d(f2)
mlab.colorbar(plot_data,title='f2',orientation='vertical')
mlab.savefig('f.png',magnification=5)

This was working fine when data is arranged in a order (x,y,z) grid data. But with file written not in order it is creates plot which doesn't match the one with ordered data (which I know is correct). What could be the reason/solution ?

Of course I only want to arrange x,y,z and then associate functional value f(x),f(y),f(z) to its right position (x,y,z).

解决方案

It worked.
I added the following just after reading the file (rest code remaining same):

data_file = np.loadtxt('datafile',
dtype=[('x',float),('y',float),('z',float),('fx',float),('fy',float),('fz',float)])
data_file = np.sort(data_file,order=['z','y'])
f = np.array(data_file).reshape(nx,ny,nz)

这篇关于使用mayavi绘制从文件中读取的三维数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:50