def GetSample(obj, ev): # Get the observed plane and bind it planeX = GetSample.planeX imageActor2.SetInputData(planeX.GetResliceOutput()) ren2.AddActor(imageActor2) renWin.AddRenderer(ren2) # Set the number of scalar components to 3, # otherwise vtkImageAccumulate can't process it. # planeX.GetResliceOutput().AllocateScalars(vtk.VTK_INT, 3) # Set the input data for histogram histogram.SetInputData(planeX.GetResliceOutput()) histogram.Update() # Construct an array to store the histogram red = [1, 0, 0] frequencies = vtk.vtkIntArray() frequencies.SetNumberOfComponents(1) frequencies.SetNumberOfTuples(256) o = histogram.GetOutput() output = o.GetPointData().GetScalars() j = 0 while j < 256: frequencies.SetTuple1(j, output.GetValue(j)) j = j + 1 # Convert the array to vtkDataObject dataObject = vtk.vtkDataObject() dataObject.GetFieldData().AddArray(frequencies) # Create a barChart to display the histogram barChart = vtk.vtkBarChartActor() barChart.SetInput(dataObject) barChart.SetTitle("Histogram") barChart.GetPositionCoordinate().SetValue(0.05, 0.05, 0.0) barChart.GetPosition2Coordinate().SetValue(0.95, 0.85, 0.0) barChart.GetProperty().SetColor(1, 1, 1) barChart.GetLegendActor().SetNumberOfEntries( dataObject.GetFieldData().GetArray(0).GetNumberOfTuples()) barChart.LegendVisibilityOff() barChart.LabelVisibilityOff() count = 0 while count < 256: barChart.SetBarColor(count, red) count += 1 # Visualize the histogram ren3 = vtk.vtkRenderer() ren3.SetViewport(0.5, 0, 1.0, 0.5) ren3.AddActor(barChart) renWin.AddRenderer(ren3)
def plotHistogram(self, renderer): barChart = vtk.vtkBarChartActor() dataObject = vtk.vtkDataObject() dataObject.GetFieldData().AddArray(self.FreqArray) for i in range(0, self.BinsCount): barChart.SetBarLabel(i, "{0:.2f}".format(self.getBinCenter(i))) barChart.SetInput(dataObject) barChart.GetLegendActor().SetNumberOfEntries(dataObject.GetFieldData().GetArray(0).GetNumberOfTuples()) barChart.LegendVisibilityOff() barChart.LabelVisibilityOn() barChart.GetProperty().SetColor(1, 1, 1) renderer.AddActor(barChart)
bitter = numpy_to_vtk(colors_numpy_array, deep=True, array_type=vtk.VTK_FLOAT) #print vtk_data_array #bitter = vtk.vtkFloatArray #bitter.SetNumberOfTuples(numTuples); #for i in range(0, numTuples):#(int i=0; i<numTuples; i++) # bitter.SetTuple1(i, vtk.vtkMath::Random(7,100)) dobj = vtk.vtkDataObject() dobj.GetFieldData().AddArray(bitter) actor = vtk.vtkBarChartActor() actor.SetInput(dobj); actor.SetTitle("Bar Chart"); actor.GetPositionCoordinate().SetValue(0.05,0.05,0.0); actor.GetPosition2Coordinate().SetValue(0.95,0.85,0.0); actor.GetProperty().SetColor(1,1,1); actor.GetLegendActor().SetNumberOfEntries(numTuples); ''' for (int i=0; i<numTuples; i++) { double red=vtkMath::Random(0,1); double green=vtkMath::Random(0,1); double blue=vtkMath::Random(0,1); actor.SetBarColor(i,red,green,blue); }