mayavi surf()如何显示colorbar?

2024-07-05 09:00:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我想不出如何让colorbar显示出来。我没有太多使用mayavi的经验,所以我不确定我需要采取哪些步骤来解决这个问题?在

其他人也有类似的问题吗 我的代码如下所示:

from tkFileDialog import askopenfilename
from StringIO import StringIO
import numpy as np 
from mayavi import mlab

#getting the data from a txt file 
filename = askopenfilename()
type(filename) 
fileAsStr =''
data = []
count= 0
atData=False
with open(filename,'r') as f:
    for line in f:
        if line.startswith("Note"):
            title = line
            title = title.strip("Note:")
            title = title.strip()
            print title
        if atData and not line.startswith('Total'):  #after the second one begin reading file
            line = line.replace(' ','')
            #data.append(line)
            fileAsStr = fileAsStr + line
        if line.startswith('-----'):
            count = count +1
        if count == 2:
            atData = True
dataStrIO = StringIO(fileAsStr)
dataArray = np.loadtxt(dataStrIO, delimiter=',')
dataDim = dataArray.shape
dx = dataArray[0:dataDim[0], 3]
dy = dataArray[0:dataDim[0], 4]
dz = dataArray[0:dataDim[0], 5]
bTotal = dataArray[0:dataDim[0],9]
firstNum = dy[0]
count = 0
while firstNum == dy[count]:
    count = count + 1
print 'count=' + str(count)

#arranging the arrays into an acceptable format
from itertools import islice
def solve(lis, n):
    it = iter(lis)
    return [list(islice(it,n)) for _ in xrange(len(lis)/n)] 
dx = np.transpose(solve(dx, count))
dy = np.transpose(solve(dy, count))
dz = solve(dz, count)
bTotal = solve(bTotal, count)
bTotal = np.log10(bTotal)


#making the plot
mlab.options.backend = 'envisage'
surf = mlab.surf(dx,dy, bTotal,warp_scale=2)

mlab.axes(surf, x_axis_visibility= True, y_axis_visibility = True, 
    z_axis_visibility = True, xlabel='x axis (mm)', ylabel='y axis (mm)', 
    zlabel ='z axis (mm)', nb_labels=10)

mlab.axes.label_text_property.font_size = 5 
mlab.title(title, line_width = .5, height = 1)
mlab.colorbar( title = "magnetic field (Tesla)")

Tags: thefromimporttitlecountnplinesolve