python - mayavi mapping a discrete colorbar on a surface -
I want to change the color of the surface on the basis of another value (an ID) that is considered discrete (hence I In the simplified example given below, I have prepared a field with 3 different IDs:
0 / red on left
2 / blue
1 / green left
But with the code below, I have some strange behavior on the border between red and blue (green Point) I think this is probably due to an interpolation!
Code:
import mlab np # np # my dataset -simplified-x, y, z = np.mgrid [-3: 3: 100] H, -3: 3: 100h, -3: 3: 100 h] value = NP. SQLT (x ** 2 + y ** 2 + z ** 2) # My color value: Volume is divided into 3 sub-versions taking colorvalues with X = np.empty (values.shape) colorvalues [0:33,:,:] = 0 colorvalues [33:66,:,:] = 2. Look [66:,:,:] = 1 src = mlab.pipeline.scalar_field (value) src.image_data.point_data.add_array (colorvalues.T.ravel ()) (1) .name = 'MyID' src.image_data. point_data.update src.image_data.point_data.get_array () # surface, contour = mlab.pipeline.contour (src) contour.filter.contours = [2.8,] # ID's interest to recognize. Contour2 = mlab.pipeline.set_active_attribute (contour, point_scalars = 'myID') # and we display the surface is the current attribute of the colormap: ID. RGB mySurf.module_manager.scalar_lut_manager.lut.table = np.array ([[255,0,0,255] [0,255,0,255] ,,: mySurf = mlab.pipeline.surface (contour2) # I change my color map is a discrete [0,0,255,255]]) mlab.colorbar (title = 'ID' orientation = "vertical ', nb_labels = 3) mlab.show ()
I have also tried with this line before mlab.show ():
MySurf.actor .mapper.interpolate_scalars_before_mapping = true
Rendering is better but green points become green strips.
I got my reply using a casual closest neighbor, using a color by the interpolation and the cell of my object. Import from the elusive import as mlab import number NP import scipy.interpolate # my dataset -simplified-x, y, z = np.mgrid [3: 3: 100h, -3: 3: 100h, -3: 3: 100 h] Value = NP SQLT (x ** 2 + y ** 2 + Z ** 2) # My color values: Volume 3 is divided into sub-versions x color formats = np.empty (values.shape) colorvalues Taking [0:33,:,::] = 0 colorvalues [33:66,:,:] = 2 Lookup [66:,:,:] = 1 Src = mlab.pipeline.scalar_field (x, y, z, value) # surface i contour = mlab.pipeline.contour (src) contour.filter.contours = [2.8,] I am interested at # Surface PtsCoord = Contour. Output [0] .points.to_array () # Then the variable in which the triangle form is indicative of the digit. PolyAndTriIDs = contour.outputs [0] .polys.to_array () PolyAndTriIDs = PolyAndTriIDs.reshape (PolyAndTriIDs.size / 4,4) # Each triangle x1, coordinates y1, z1 = PtsCoord [PolyAndTriIDs [:, 1]] T. X2, y2, z2 = PtsCoord [PolyAndTriIDs [, 2]]. T x 3, y3, z3 = PtsCoord [PolyAndTriIDs [:, 3]] T # I nearest generated color value in the triangle center with neighbor interpolation method interp0 = scipy.interpolate.NeararestNDInterpolator ((x.ravel (), y .ravel (), z.ravel ()), colorvalues.ravel ()) result0 = interp0 ((np.mean ((x1, x2, x3), 0), npmen (y1, y2, y3) 0), NPMen ((z1, z2, z3), 0))) # cell scalar value displayed with triangular_ames and colors given by mesh = mlab.triangular_mesh (PtsCoord [,, 0], PtsCoord [:, 1], PtsCoord [,, 2], PolyAndTriIDs [:, 1:]) cell_data = mesh.mlab_source.dataset.cell_data cell_data.scalars = result0 cell_data.scalars .name = 'cell data' cell_data.update () mesh.actor .mapper.scal Ar_mode = 'use_cell_data' mesh.module_manager.scalar_lut_manager.lut.table = np.array ([[255,0,0,255], [0255, 0255], [0,0,255,255]] mesh.module_manager.scalar_lut_manager.use_default_range = false Mesh.module_manager.scalar_lut_manager.data_range = [0, 2.] mlab.colorbar (title = 'id', orientation = 'vertical', nb_labels = 3)
The result is less smooth but more is relevant.
Comments
Post a Comment