By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

I am back for help:-)

P: 13
Hi folks:

I have figured out how to sacle glyph by one scalar array, and color by another scalar array. but when I want to change color by third scalar array, I met some problems. At the beginning, I color the glyph by sex, and then I want to color by occupation. I thought I just need to the following code in order to change color. However, if I click occupation radiobutton, color can be changed according to the data given. but I can not change back by clicking sex radiobutton.(ie glyph still color by occupation once I click any radiobuttons)
def ColorGroup():
if v==1:

myMapper.ColorByArrayComponent("data", 2)
myMapper.SetScalarRange(0,maxSnum)
win.Render()

else:

myMapper.ColorByArrayComponent("data", 8)
myMapper.SetScalarRange(0,maxOccu)
win.Render()

Can anone tell me where I am wrong??Thanks:-)

The following is my whole code and data file, Can you play with this to find where I am wrong?
8 0 1 21 0 5.1 35 2 6 1 1
9 1 1 42 1 4.95 57 3 0 1 1
12 0 0 1 0 6.67 19 3 2 1 0
12 0 0 4 0 4 22 3 3 0 0
12 0 0 17 0 7.5 35 2 6 0 1
13 0 0 9 1 13.07 28 3 0 0 0
10 1 0 27 0 4.45 43 3 6 0 0
12 0 0 9 0 19.47 27 2 5 0 0
16 0 0 11 0 13.28 33 3 6 1 1
12 0 1 9 1 8.75 27 3 0 0 0

title='visualization of wage'
# -*- coding: cp1252 -*-
import tkFont
from vtk import *
from vtk.tk.vtkTkRenderWindowInteractor import *
from Tkinter import *

def RawDataExample():
""" Creates 2D data inside a vtkTkRenderWindowInteractor. """

def ColorGroup():
if v==1:

myMapper.ColorByArrayComponent("data", 2)
myMapper.SetScalarRange(0,maxSnum)
win.Render()

else:

myMapper.ColorByArrayComponent("data", 8)
myMapper.SetScalarRange(0,maxOccu)
win.Render()

# create root window
root = Tk()
root.title("Information Visualization of Wage")
f=Frame(root)
f.pack()

heading=Label(f,text="Create a Visualization Tool to analyse large datasets",font=tkFont.Font ( family="Helvetica",
size=20, weight="bold") )
heading.pack()

colorGroup=Frame(root,relief="sunken",border=1)
colorGroup.pack(side="right")

lblColor=Label(colorGroup,relief="groove", text="Group by Colour",font=tkFont.Font ( family="New",
size=12))
lblColor.pack()

v=IntVar()
radSex=Radiobutton(colorGroup,width=10,text="Sex", anchor=W,variable=v,value=1,command=ColorGroup)
radOccupation=Radiobutton(colorGroup,width=10,text ="Occupation",anchor=W,variable=v,value=6,command= ColorGroup)
v.set(1)

radSex.pack()
radOccupation.pack()

quit=Button(root,text="Exit",command=root.destroy, font=tkFont.Font ( family="Helvetica",
size=12, weight="bold"),cursor="heart")
quit.pack(side="bottom")

#set up a structured points and two arrays to hold sets of data
myGrid=vtkPolyData()
myPoint=vtkPoints()

## create 11 arrays for 11 variables
EducationArray = vtkIntArray()
EducationArray.SetName("education")
SouthArray = vtkIntArray()
SouthArray.SetName("south")
SexArray=vtkIntArray()
SexArray.SetName("sex")
ExperienceArray=vtkIntArray()
ExperienceArray.SetName("experience")
UnionArray=vtkIntArray()
UnionArray.SetName("union")
WageArray=vtkFloatArray()
WageArray.SetName("wage")
AgeArray=vtkIntArray()
AgeArray.SetName("age")
RaceArray=vtkIntArray()
RaceArray.SetName("race")
OccupationArray=vtkIntArray()
OccupationArray.SetName("occupation")
SectorArray=vtkIntArray()
SectorArray.SetName("sector")
MarriageArray=vtkIntArray()
MarriageArray.SetName("marriage")

## create window, glyph etc
win = vtkTkRenderWindowInteractor(root, width=300, height=300)
win.Initialize()
win.GlobalWarningDisplayOff()
def quit(obj=root):
obj.destroy()

ren = vtk.vtkRenderer()
win.GetRenderWindow().AddRenderer(ren)

## reading the data from the file and add data into 11 arrays
f=open('textdata.txt', 'r')
for line in f:
#read in a line and split up into a list delimited by ,
linelist = line.split("\t")
print linelist
#put two numbers into two integer arrays
myPoint.InsertNextPoint((float)(linelist[0]),(float)(linelist[3]),0)
EducationArray.InsertNextValue((int)(linelist[0]))
SouthArray.InsertNextValue((int)(linelist[1]))
SexArray.InsertNextValue((int)(linelist[2]))
ExperienceArray.InsertNextValue((int)(linelist[3]))
UnionArray.InsertNextValue((int)(linelist[4]))
WageArray.InsertNextValue((float)(linelist[5]))
AgeArray.InsertNextValue((int)(linelist[6]))
RaceArray.InsertNextValue((int)(linelist[7]))
OccupationArray.InsertNextValue((int)(linelist[8]))
SectorArray.InsertNextValue((int)(linelist[9]))
MarriageArray.InsertNextValue((int)(linelist[10]))


## add 11 arrays into one array in order to retrive the data.
data = vtk.vtkFloatArray()
data.SetNumberOfComponents(11)
data.SetNumberOfTuples(10)
data.CopyComponent(0, WageArray, 0)
data.CopyComponent(1, SouthArray, 0)
data.CopyComponent(2, SexArray,0)
data.CopyComponent(3, ExperienceArray,0)
data.CopyComponent(4, UnionArray,0)
data.CopyComponent(5, EducationArray,0)
data.CopyComponent(6, AgeArray,0)
data.CopyComponent(7, RaceArray,0)
data.CopyComponent(8, OccupationArray,0)
data.CopyComponent(9, SectorArray,0)
data.CopyComponent(10, MarriageArray,0)
data.SetName("data")

## find the max, and min number for the education and experience which
## are going to use in X,Y Axes.
listEdu=[]
listExp=[]
listSex=[]
listOcc=[]
for i in range(10): ######## this may be 9 !!!!!!!!!
listEdu[:0]=[data.GetComponent(i,5)]
listExp[:0]=[data.GetComponent(i,3)]
listSex[:0]=[data.GetComponent(i,2)]
listOcc[:0]=[data.GetComponent(i,8)]
maxXnum=max(listEdu)
minXnum=min(listEdu)
maxYnum=max(listExp)
minYnum=min(listExp)
maxSnum=max(listSex)
minSnum=min(listSex)
maxOccu=max(listOcc)
print maxXnum,minXnum,maxYnum,minYnum,maxSnum,minSnum

## set the points positions and add array into dataset
myGrid.SetPoints(myPoint)
myGrid.GetPointData().AddArray(data)
myGrid.GetPointData().SetActiveScalars("data")


# create x, y axes and scalar bar
MyXAxis=vtk.vtkAxisActor2D()
MyXAxis.SetFontFactor(0.5)
MyXAxis.SetLabelFactor(1.0)
MyXAxis.SetLabelFormat("%6.4g")
MyXAxis.GetProperty().SetColor(0,2,0)
MyXAxis.SetNumberOfLabels(10)
MyXAxis.SetRange(minXnum,maxXnum)
MyXAxis.SetPoint1(minXnum,minYnum)
MyXAxis.SetPoint2(maxXnum,minYnum)
MyXAxis.SetTitle("Education")


MyYAxis=vtk.vtkAxisActor2D()
MyYAxis.SetFontFactor(0.5)
MyYAxis.SetLabelFactor(1.0)
MyYAxis.SetLabelFormat("%6g")
MyYAxis.GetProperty().SetColor(0,2,0)
MyYAxis.SetNumberOfLabels(10)
MyYAxis.SetRange(maxYnum,minYnum)
MyYAxis.SetPoint1(minXnum,maxYnum)
MyYAxis.SetPoint2(minXnum,minYnum)
MyYAxis.SetTitle("Experience")


myTrans=vtk.vtkTransform()
myTransFilter=vtk.vtkTransformFilter()

Xrange=maxXnum-minXnum
Yrange=maxYnum-minYnum


if Xrange>Yrange:
myTrans.Scale(1,Xrange/Yrange,1)
MyYAxis.SetPoint1(minXnum,maxYnum*Xrange/Yrange)
else:
myTrans.Scale(Yrange/Xrange,1,1)
MyXAxis.SetPoint2(maxXnum*Yrange/Xrange,minYnum)

MyXAxis.GetPositionCoordinate().SetCoordinateSyste mToWorld()
MyXAxis.GetPoint1Coordinate().SetCoordinateSystemT oWorld()
MyXAxis.GetPoint2Coordinate().SetCoordinateSystemT oWorld()

MyYAxis.GetPositionCoordinate().SetCoordinateSyste mToWorld()
MyYAxis.GetPoint1Coordinate().SetCoordinateSystemT oWorld()
MyYAxis.GetPoint2Coordinate().SetCoordinateSystemT oWorld()

myTransFilter.SetInput(myGrid)
myTransFilter.SetTransform(myTrans)


## set source as sphere
sphere=vtk.vtkSphereSource()
sphere.SetRadius(.9)
sphere.SetPhiResolution(30)
sphere.SetThetaResolution(30)

# display points as glyphs
myGlyph=vtk.vtkGlyph3D()
myGlyph.SetInput(myTransFilter.GetOutput())
myGlyph.SetSource(sphere.GetOutput())
myGlyph.ClampingOff()
myGlyph.SetScaleModeToScaleByScalar()
myGlyph.SetScaleFactor(0.1)
myGlyph.SetColorModeToColorByScalar()

myMapper = vtk.vtkPolyDataMapper()
myMapper.SetInput(myGlyph.GetOutput())
myMapper.ScalarVisibilityOn()
myMapper.ColorByArrayComponent("data", 1)
myMapper.SetScalarRange(0,maxSnum)

myActor = vtk.vtkActor()
myActor.SetMapper(myMapper)

print myMapper.GetLookupTable

MyLegend=vtk.vtkScalarBarActor()
MyLegend.SetLookupTable(myMapper.GetLookupTable())
MyLegend.SetTitle('Scalar Bar')
MyLegend.SetOrientationToVertical()
MyLegend.SetLabelFormat("%6.4g")
MyLegend.SetNumberOfLabels(6)
MyLegend.SetHeight(1)

ren.AddActor(myActor)
ren.AddActor(MyLegend)
ren.AddActor(MyXAxis)
ren.AddActor(MyYAxis)

# pack the win into the tk root
win.pack(side="left",fill='both', expand=1)
win.Start()

# start the tk mainloop
root.mainloop()

RawDataExample()
Sep 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: 13
hi folks:

I fixed my problem now, I made a silly mistake. I should use v.get()==1, not v==1. :-)

have a nice day, everybody!!!!!
Sep 30 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.