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.ColorB yArrayComponent ("data", 2)
myMapper.SetSca larRange(0,maxS num)
win.Render()
else:
myMapper.ColorB yArrayComponent ("data", 8)
myMapper.SetSca larRange(0,maxO ccu)
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='visualiz ation of wage'
# -*- coding: cp1252 -*-
import tkFont
from vtk import *
from vtk.tk.vtkTkRen derWindowIntera ctor import *
from Tkinter import *
def RawDataExample( ):
""" Creates 2D data inside a vtkTkRenderWind owInteractor. """
def ColorGroup():
if v==1:
myMapper.ColorB yArrayComponent ("data", 2)
myMapper.SetSca larRange(0,maxS num)
win.Render()
else:
myMapper.ColorB yArrayComponent ("data", 8)
myMapper.SetSca larRange(0,maxO ccu)
win.Render()
# create root window
root = Tk()
root.title("Inf ormation Visualization of Wage")
f=Frame(root)
f.pack()
heading=Label(f ,text="Create a Visualization Tool to analyse large datasets",font= tkFont.Font ( family="Helveti ca",
size=20, weight="bold") )
heading.pack()
colorGroup=Fram e(root,relief=" sunken",border= 1)
colorGroup.pack (side="right")
lblColor=Label( colorGroup,reli ef="groove", text="Group by Colour",font=tk Font.Font ( family="New",
size=12))
lblColor.pack()
v=IntVar()
radSex=Radiobut ton(colorGroup, width=10,text=" Sex",anchor=W,v ariable=v,value =1,command=Colo rGroup)
radOccupation=R adiobutton(colo rGroup,width=10 ,text="Occupati on",anchor=W,va riable=v,value= 6,command=Color Group)
v.set(1)
radSex.pack()
radOccupation.p ack()
quit=Button(roo t,text="Exit",c ommand=root.des troy,font=tkFon t.Font ( family="Helveti ca",
size=12, weight="bold"), cursor="heart")
quit.pack(side= "bottom")
#set up a structured points and two arrays to hold sets of data
myGrid=vtkPolyD ata()
myPoint=vtkPoin ts()
## create 11 arrays for 11 variables
EducationArray = vtkIntArray()
EducationArray. SetName("educat ion")
SouthArray = vtkIntArray()
SouthArray.SetN ame("south")
SexArray=vtkInt Array()
SexArray.SetNam e("sex")
ExperienceArray =vtkIntArray()
ExperienceArray .SetName("exper ience")
UnionArray=vtkI ntArray()
UnionArray.SetN ame("union")
WageArray=vtkFl oatArray()
WageArray.SetNa me("wage")
AgeArray=vtkInt Array()
AgeArray.SetNam e("age")
RaceArray=vtkIn tArray()
RaceArray.SetNa me("race")
OccupationArray =vtkIntArray()
OccupationArray .SetName("occup ation")
SectorArray=vtk IntArray()
SectorArray.Set Name("sector")
MarriageArray=v tkIntArray()
MarriageArray.S etName("marriag e")
## create window, glyph etc
win = vtkTkRenderWind owInteractor(ro ot, width=300, height=300)
win.Initialize( )
win.GlobalWarni ngDisplayOff()
def quit(obj=root):
obj.destroy()
ren = vtk.vtkRenderer ()
win.GetRenderWi ndow().AddRende rer(ren)
## reading the data from the file and add data into 11 arrays
f=open('textdat a.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.InsertN extPoint((float )(linelist[0]),(float)(linel ist[3]),0)
EducationArray. InsertNextValue ((int)(linelist[0]))
SouthArray.Inse rtNextValue((in t)(linelist[1]))
SexArray.Insert NextValue((int) (linelist[2]))
ExperienceArray .InsertNextValu e((int)(linelis t[3]))
UnionArray.Inse rtNextValue((in t)(linelist[4]))
WageArray.Inser tNextValue((flo at)(linelist[5]))
AgeArray.Insert NextValue((int) (linelist[6]))
RaceArray.Inser tNextValue((int )(linelist[7]))
OccupationArray .InsertNextValu e((int)(linelis t[8]))
SectorArray.Ins ertNextValue((i nt)(linelist[9]))
MarriageArray.I nsertNextValue( (int)(linelist[10]))
## add 11 arrays into one array in order to retrive the data.
data = vtk.vtkFloatArr ay()
data.SetNumberO fComponents(11)
data.SetNumberO fTuples(10)
data.CopyCompon ent(0, WageArray, 0)
data.CopyCompon ent(1, SouthArray, 0)
data.CopyCompon ent(2, SexArray,0)
data.CopyCompon ent(3, ExperienceArray ,0)
data.CopyCompon ent(4, UnionArray,0)
data.CopyCompon ent(5, EducationArray, 0)
data.CopyCompon ent(6, AgeArray,0)
data.CopyCompon ent(7, RaceArray,0)
data.CopyCompon ent(8, OccupationArray ,0)
data.CopyCompon ent(9, SectorArray,0)
data.CopyCompon ent(10, MarriageArray,0 )
data.SetName("d ata")
## 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.GetCompone nt(i,5)]
listExp[:0]=[data.GetCompone nt(i,3)]
listSex[:0]=[data.GetCompone nt(i,2)]
listOcc[:0]=[data.GetCompone nt(i,8)]
maxXnum=max(lis tEdu)
minXnum=min(lis tEdu)
maxYnum=max(lis tExp)
minYnum=min(lis tExp)
maxSnum=max(lis tSex)
minSnum=min(lis tSex)
maxOccu=max(lis tOcc)
print maxXnum,minXnum ,maxYnum,minYnu m,maxSnum,minSn um
## set the points positions and add array into dataset
myGrid.SetPoint s(myPoint)
myGrid.GetPoint Data().AddArray (data)
myGrid.GetPoint Data().SetActiv eScalars("data" )
# create x, y axes and scalar bar
MyXAxis=vtk.vtk AxisActor2D()
MyXAxis.SetFont Factor(0.5)
MyXAxis.SetLabe lFactor(1.0)
MyXAxis.SetLabe lFormat("%6.4g" )
MyXAxis.GetProp erty().SetColor (0,2,0)
MyXAxis.SetNumb erOfLabels(10)
MyXAxis.SetRang e(minXnum,maxXn um)
MyXAxis.SetPoin t1(minXnum,minY num)
MyXAxis.SetPoin t2(maxXnum,minY num)
MyXAxis.SetTitl e("Education" )
MyYAxis=vtk.vtk AxisActor2D()
MyYAxis.SetFont Factor(0.5)
MyYAxis.SetLabe lFactor(1.0)
MyYAxis.SetLabe lFormat("%6g")
MyYAxis.GetProp erty().SetColor (0,2,0)
MyYAxis.SetNumb erOfLabels(10)
MyYAxis.SetRang e(maxYnum,minYn um)
MyYAxis.SetPoin t1(minXnum,maxY num)
MyYAxis.SetPoin t2(minXnum,minY num)
MyYAxis.SetTitl e("Experience ")
myTrans=vtk.vtk Transform()
myTransFilter=v tk.vtkTransform Filter()
Xrange=maxXnum-minXnum
Yrange=maxYnum-minYnum
if Xrange>Yrange:
myTrans.Scale(1 ,Xrange/Yrange,1)
MyYAxis.SetPoin t1(minXnum,maxY num*Xrange/Yrange)
else:
myTrans.Scale(Y range/Xrange,1,1)
MyXAxis.SetPoin t2(maxXnum*Yran ge/Xrange,minYnum)
MyXAxis.GetPosi tionCoordinate( ).SetCoordinate SystemToWorld()
MyXAxis.GetPoin t1Coordinate(). SetCoordinateSy stemToWorld()
MyXAxis.GetPoin t2Coordinate(). SetCoordinateSy stemToWorld()
MyYAxis.GetPosi tionCoordinate( ).SetCoordinate SystemToWorld()
MyYAxis.GetPoin t1Coordinate(). SetCoordinateSy stemToWorld()
MyYAxis.GetPoin t2Coordinate(). SetCoordinateSy stemToWorld()
myTransFilter.S etInput(myGrid)
myTransFilter.S etTransform(myT rans)
## set source as sphere
sphere=vtk.vtkS phereSource()
sphere.SetRadiu s(.9)
sphere.SetPhiRe solution(30)
sphere.SetTheta Resolution(30)
# display points as glyphs
myGlyph=vtk.vtk Glyph3D()
myGlyph.SetInpu t(myTransFilter .GetOutput())
myGlyph.SetSour ce(sphere.GetOu tput())
myGlyph.Clampin gOff()
myGlyph.SetScal eModeToScaleByS calar()
myGlyph.SetScal eFactor(0.1)
myGlyph.SetColo rModeToColorByS calar()
myMapper = vtk.vtkPolyData Mapper()
myMapper.SetInp ut(myGlyph.GetO utput())
myMapper.Scalar VisibilityOn()
myMapper.ColorB yArrayComponent ("data", 1)
myMapper.SetSca larRange(0,maxS num)
myActor = vtk.vtkActor()
myActor.SetMapp er(myMapper)
print myMapper.GetLoo kupTable
MyLegend=vtk.vt kScalarBarActor ()
MyLegend.SetLoo kupTable(myMapp er.GetLookupTab le())
MyLegend.SetTit le('Scalar Bar')
MyLegend.SetOri entationToVerti cal()
MyLegend.SetLab elFormat("%6.4g ")
MyLegend.SetNum berOfLabels(6)
MyLegend.SetHei ght(1)
ren.AddActor(my Actor)
ren.AddActor(My Legend)
ren.AddActor(My XAxis)
ren.AddActor(My YAxis)
# pack the win into the tk root
win.pack(side=" left",fill='bot h', expand=1)
win.Start()
# start the tk mainloop
root.mainloop()
RawDataExample( )
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.ColorB yArrayComponent ("data", 2)
myMapper.SetSca larRange(0,maxS num)
win.Render()
else:
myMapper.ColorB yArrayComponent ("data", 8)
myMapper.SetSca larRange(0,maxO ccu)
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='visualiz ation of wage'
# -*- coding: cp1252 -*-
import tkFont
from vtk import *
from vtk.tk.vtkTkRen derWindowIntera ctor import *
from Tkinter import *
def RawDataExample( ):
""" Creates 2D data inside a vtkTkRenderWind owInteractor. """
def ColorGroup():
if v==1:
myMapper.ColorB yArrayComponent ("data", 2)
myMapper.SetSca larRange(0,maxS num)
win.Render()
else:
myMapper.ColorB yArrayComponent ("data", 8)
myMapper.SetSca larRange(0,maxO ccu)
win.Render()
# create root window
root = Tk()
root.title("Inf ormation Visualization of Wage")
f=Frame(root)
f.pack()
heading=Label(f ,text="Create a Visualization Tool to analyse large datasets",font= tkFont.Font ( family="Helveti ca",
size=20, weight="bold") )
heading.pack()
colorGroup=Fram e(root,relief=" sunken",border= 1)
colorGroup.pack (side="right")
lblColor=Label( colorGroup,reli ef="groove", text="Group by Colour",font=tk Font.Font ( family="New",
size=12))
lblColor.pack()
v=IntVar()
radSex=Radiobut ton(colorGroup, width=10,text=" Sex",anchor=W,v ariable=v,value =1,command=Colo rGroup)
radOccupation=R adiobutton(colo rGroup,width=10 ,text="Occupati on",anchor=W,va riable=v,value= 6,command=Color Group)
v.set(1)
radSex.pack()
radOccupation.p ack()
quit=Button(roo t,text="Exit",c ommand=root.des troy,font=tkFon t.Font ( family="Helveti ca",
size=12, weight="bold"), cursor="heart")
quit.pack(side= "bottom")
#set up a structured points and two arrays to hold sets of data
myGrid=vtkPolyD ata()
myPoint=vtkPoin ts()
## create 11 arrays for 11 variables
EducationArray = vtkIntArray()
EducationArray. SetName("educat ion")
SouthArray = vtkIntArray()
SouthArray.SetN ame("south")
SexArray=vtkInt Array()
SexArray.SetNam e("sex")
ExperienceArray =vtkIntArray()
ExperienceArray .SetName("exper ience")
UnionArray=vtkI ntArray()
UnionArray.SetN ame("union")
WageArray=vtkFl oatArray()
WageArray.SetNa me("wage")
AgeArray=vtkInt Array()
AgeArray.SetNam e("age")
RaceArray=vtkIn tArray()
RaceArray.SetNa me("race")
OccupationArray =vtkIntArray()
OccupationArray .SetName("occup ation")
SectorArray=vtk IntArray()
SectorArray.Set Name("sector")
MarriageArray=v tkIntArray()
MarriageArray.S etName("marriag e")
## create window, glyph etc
win = vtkTkRenderWind owInteractor(ro ot, width=300, height=300)
win.Initialize( )
win.GlobalWarni ngDisplayOff()
def quit(obj=root):
obj.destroy()
ren = vtk.vtkRenderer ()
win.GetRenderWi ndow().AddRende rer(ren)
## reading the data from the file and add data into 11 arrays
f=open('textdat a.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.InsertN extPoint((float )(linelist[0]),(float)(linel ist[3]),0)
EducationArray. InsertNextValue ((int)(linelist[0]))
SouthArray.Inse rtNextValue((in t)(linelist[1]))
SexArray.Insert NextValue((int) (linelist[2]))
ExperienceArray .InsertNextValu e((int)(linelis t[3]))
UnionArray.Inse rtNextValue((in t)(linelist[4]))
WageArray.Inser tNextValue((flo at)(linelist[5]))
AgeArray.Insert NextValue((int) (linelist[6]))
RaceArray.Inser tNextValue((int )(linelist[7]))
OccupationArray .InsertNextValu e((int)(linelis t[8]))
SectorArray.Ins ertNextValue((i nt)(linelist[9]))
MarriageArray.I nsertNextValue( (int)(linelist[10]))
## add 11 arrays into one array in order to retrive the data.
data = vtk.vtkFloatArr ay()
data.SetNumberO fComponents(11)
data.SetNumberO fTuples(10)
data.CopyCompon ent(0, WageArray, 0)
data.CopyCompon ent(1, SouthArray, 0)
data.CopyCompon ent(2, SexArray,0)
data.CopyCompon ent(3, ExperienceArray ,0)
data.CopyCompon ent(4, UnionArray,0)
data.CopyCompon ent(5, EducationArray, 0)
data.CopyCompon ent(6, AgeArray,0)
data.CopyCompon ent(7, RaceArray,0)
data.CopyCompon ent(8, OccupationArray ,0)
data.CopyCompon ent(9, SectorArray,0)
data.CopyCompon ent(10, MarriageArray,0 )
data.SetName("d ata")
## 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.GetCompone nt(i,5)]
listExp[:0]=[data.GetCompone nt(i,3)]
listSex[:0]=[data.GetCompone nt(i,2)]
listOcc[:0]=[data.GetCompone nt(i,8)]
maxXnum=max(lis tEdu)
minXnum=min(lis tEdu)
maxYnum=max(lis tExp)
minYnum=min(lis tExp)
maxSnum=max(lis tSex)
minSnum=min(lis tSex)
maxOccu=max(lis tOcc)
print maxXnum,minXnum ,maxYnum,minYnu m,maxSnum,minSn um
## set the points positions and add array into dataset
myGrid.SetPoint s(myPoint)
myGrid.GetPoint Data().AddArray (data)
myGrid.GetPoint Data().SetActiv eScalars("data" )
# create x, y axes and scalar bar
MyXAxis=vtk.vtk AxisActor2D()
MyXAxis.SetFont Factor(0.5)
MyXAxis.SetLabe lFactor(1.0)
MyXAxis.SetLabe lFormat("%6.4g" )
MyXAxis.GetProp erty().SetColor (0,2,0)
MyXAxis.SetNumb erOfLabels(10)
MyXAxis.SetRang e(minXnum,maxXn um)
MyXAxis.SetPoin t1(minXnum,minY num)
MyXAxis.SetPoin t2(maxXnum,minY num)
MyXAxis.SetTitl e("Education" )
MyYAxis=vtk.vtk AxisActor2D()
MyYAxis.SetFont Factor(0.5)
MyYAxis.SetLabe lFactor(1.0)
MyYAxis.SetLabe lFormat("%6g")
MyYAxis.GetProp erty().SetColor (0,2,0)
MyYAxis.SetNumb erOfLabels(10)
MyYAxis.SetRang e(maxYnum,minYn um)
MyYAxis.SetPoin t1(minXnum,maxY num)
MyYAxis.SetPoin t2(minXnum,minY num)
MyYAxis.SetTitl e("Experience ")
myTrans=vtk.vtk Transform()
myTransFilter=v tk.vtkTransform Filter()
Xrange=maxXnum-minXnum
Yrange=maxYnum-minYnum
if Xrange>Yrange:
myTrans.Scale(1 ,Xrange/Yrange,1)
MyYAxis.SetPoin t1(minXnum,maxY num*Xrange/Yrange)
else:
myTrans.Scale(Y range/Xrange,1,1)
MyXAxis.SetPoin t2(maxXnum*Yran ge/Xrange,minYnum)
MyXAxis.GetPosi tionCoordinate( ).SetCoordinate SystemToWorld()
MyXAxis.GetPoin t1Coordinate(). SetCoordinateSy stemToWorld()
MyXAxis.GetPoin t2Coordinate(). SetCoordinateSy stemToWorld()
MyYAxis.GetPosi tionCoordinate( ).SetCoordinate SystemToWorld()
MyYAxis.GetPoin t1Coordinate(). SetCoordinateSy stemToWorld()
MyYAxis.GetPoin t2Coordinate(). SetCoordinateSy stemToWorld()
myTransFilter.S etInput(myGrid)
myTransFilter.S etTransform(myT rans)
## set source as sphere
sphere=vtk.vtkS phereSource()
sphere.SetRadiu s(.9)
sphere.SetPhiRe solution(30)
sphere.SetTheta Resolution(30)
# display points as glyphs
myGlyph=vtk.vtk Glyph3D()
myGlyph.SetInpu t(myTransFilter .GetOutput())
myGlyph.SetSour ce(sphere.GetOu tput())
myGlyph.Clampin gOff()
myGlyph.SetScal eModeToScaleByS calar()
myGlyph.SetScal eFactor(0.1)
myGlyph.SetColo rModeToColorByS calar()
myMapper = vtk.vtkPolyData Mapper()
myMapper.SetInp ut(myGlyph.GetO utput())
myMapper.Scalar VisibilityOn()
myMapper.ColorB yArrayComponent ("data", 1)
myMapper.SetSca larRange(0,maxS num)
myActor = vtk.vtkActor()
myActor.SetMapp er(myMapper)
print myMapper.GetLoo kupTable
MyLegend=vtk.vt kScalarBarActor ()
MyLegend.SetLoo kupTable(myMapp er.GetLookupTab le())
MyLegend.SetTit le('Scalar Bar')
MyLegend.SetOri entationToVerti cal()
MyLegend.SetLab elFormat("%6.4g ")
MyLegend.SetNum berOfLabels(6)
MyLegend.SetHei ght(1)
ren.AddActor(my Actor)
ren.AddActor(My Legend)
ren.AddActor(My XAxis)
ren.AddActor(My YAxis)
# pack the win into the tk root
win.pack(side=" left",fill='bot h', expand=1)
win.Start()
# start the tk mainloop
root.mainloop()
RawDataExample( )
Comment