"""Metrics of quality for the cells of a triangular mesh """ from vtkplotter import * from vtkplotter.pyplot import histogram mesh = load(datadir + "bunny.obj").scale(100).computeNormals() mesh.lineWidth(0.1) # generate an array for mesh quality arr = mesh.quality(cmap='RdYlBu') #printHistogram(arr, title='mesh quality', c='w') hist = histogram(arr, xtitle='mesh quality', bc='w') hist.scale(.2).pos(-8, 4, -9) # make it bigger and place it # add a scalar bar for the active scalars mesh.addScalarBar() # create numeric labels of active scalar on top of cells labs = mesh.labels(cells=True, precision=3).color('k') show(mesh, labs, hist, __doc__, bg='bb', zoom=2)
data.append(vals) data = np.array(data) print("Print first 5 rows:") print(names) print(data[:5]) print("Number of rows:", len(data)) ################################################## # extract the columns into separate vectors: genes = data.T g0, g1, g2, g3, g4 = genes # unpack n0, n1, n2, n3, n4 = names # now create and show histograms of the gene expressions h0 = histogram(g0, xtitle=n0, c=0) h1 = histogram(g1, xtitle=n1, c=1) h2 = histogram(g2, xtitle=n2, c=2) h3 = histogram(g3, xtitle=n3, c=3, logscale=True) show(h0, h1, h2, h3, shape=(1,4)) # this is where you choose what variables to show as 3D points pts = np.c_[g4,g2,g3] # form an array of 3d points from the columns axs = dict(xtitle='gene4', ytitle='gene2', ztitle='gene3') pts_1 = pts[g0>0] # select only points that have g0>0 print("after selection nr. of points is", len(pts_1)) p_1 = Points(pts_1, r=4, c='red') # create the vtkplotter object pts_2 = pts[(g0<0) & (g1>.5)] # select excluded points that have g1>0.5 p_2 = Points(pts_2, r=8, c='green') # create the vtkplotter object
"""Fit a plane to regions of a surface defined by N points that are closest to a given point of the surface. Green histogram is the distribution of residuals from the fitting. """ from vtkplotter import * from vtkplotter.pyplot import histogram plt = Plotter() apple = load(datadir + "apple.ply").subdivide().pointGaussNoise(1) plt += apple.alpha(0.1) variances = [] for i, p in enumerate(apple.points()): pts = apple.closestPoint(p, N=12) # find the N closest points to p plane = fitPlane(pts) # find the fitting plane variances.append(plane.variance) if i % 400: continue plt += plane plt += Points(pts) plt += Arrow(plane.center, plane.center + plane.normal / 5) plt += histogram(variances, xtitle='variance').scale(6).pos(1.2, .2, -1) plt += __doc__ + "\nNr. of fits performed: " + str(len(variances)) plt.show()
"""Create an animated logo""" from vtkplotter import * from vtkplotter.pyplot import histogram exa = Polygon().scale(4.1).pos(5.25, 4.8, 0).off() his = histogram([-1, 1], [-1, 1], mode='hex').unpack() exah, cmh = [], [] for h in his: cm = h.centerOfMass() if exa.isInside(cm): h.c('green').shrink(0.9).addShadow(z=-.4) exah.append(h) cmh.append(cm) v1 = vector(9.4, 5.2, 0) v2 = vector(9.4, 2.7, 0) t1 = Text("EMBL", v1, c="k", s=1.5, depth=0) t2 = Text("European Molecular\nBiology Laboratory", v2, c="dg", s=0.6, depth=0) show(exa, exah, t1, t2, axes=0, interactive=0, elevation=-50) for ti in reversed(range(100)): t = ti / 100. for j, h in enumerate(exah): cx, cy, _ = cmh[j] - [4,5,0] x = t*-4+(1-t)*6 g = exp(-(cx-x)**2/.5)*2 h.z(g) t1.pos([sin(t)*-10, 0, -0.41] + v1).alpha((1-t)**2) t2.pos([sin(t)*-15, 0, -0.41] + v2).alpha((1-t)**4) exah[13].c('red')
"""Fit a plane to regions of a surface defined by N points that are closest to a given point of the surface. For some of these point we show the fitting plane. Black points are the N points used for fitting. Green histogram is the distribution of residuals from the fitting. """ from vtkplotter import * from vtkplotter.pyplot import histogram vp = Plotter() vp += __doc__ s = load(datadir + "cow.vtk").subdivide().normalize().alpha(0.3) vp += s variances = [] for i, p in enumerate(s.points()): if i % 100: continue # skip most points pts = s.closestPoint(p, N=12) # find the N closest points to p plane = fitPlane(pts) # find the fitting plane vp += plane vp += Points(pts) # blue points vp += Arrow(plane.center, plane.center + plane.normal / 10, c="g") variances.append(plane.variance) vp += histogram(variances).scale(25).pos(.6, -.3, -.9) vp.show(viewup="z")
from vtkplotter import * from vtkplotter.pyplot import histogram import numpy as np N = 2000 x = np.random.randn(N) * 1.0 y = np.random.randn(N) * 1.5 # hexagonal binned histogram: histo = histogram( x, y, bins=10, mode='hexbin', xtitle="x gaussian, s=1.0", ytitle="y gaussian, s=1.5", fill=True, cmap='terrain', ) # add a formula: f = r'f(x, y)=A \exp \left(-\left(\frac{\left(x-x_{o}\right)^{2}}' f += r'{2 \sigma_{x}^{2}}+\frac{\left(y-y_{o}\right)^{2}}' f += r'{2 \sigma_{y}^{2}}\right)\right)' formula = Latex(f, c='k', s=1.5).rotateX(90).rotateZ(90).pos(1.5, -2, 1) show(histo, formula, axes=1, viewup='z')
"""Fit a plane to regions of a surface defined by N points that are closest to a given point of the surface. Green histogram is the distribution of residuals from the fitting. """ from vtkplotter import * from vtkplotter.pyplot import histogram plt = Plotter() apple = load(datadir+"apple.ply").subdivide().addGaussNoise(1) plt += apple.alpha(0.1) variances = [] for i, p in enumerate(apple.points()): pts = apple.closestPoint(p, N=12) # find the N closest points to p plane = fitPlane(pts) # find the fitting plane variances.append(plane.variance) if i % 400: continue plt += plane plt += Points(pts) plt += Arrow(plane.center, plane.center+plane.normal/5) plt += histogram(variances).scale(6).pos(1.2,.2,-1) plt += __doc__ + "\nNr. of fits performed: "+str(len(variances)) plt.show()
"""A uniform distribution on a plane is not uniform on a sphere""" import numpy as np from vtkplotter.pyplot import histogram phi = np.random.rand(1000)*6.28 the = np.random.rand(1000)*3.14 h = histogram(the, phi, mode='spheric') h.show(axes=12, viewup='z')
from vtkplotter import * from vtkplotter.pyplot import histogram vp = Plotter() s = vp.load(datadir+"cow.vtk", alpha=0.3) pts1, pts2, vals, cols = [], [], [], [] for i in range(0, s.N(), 10): p = s.points(i) pts = s.closestPoint(p, N=12) # find the N closest points to p sph = fitSphere(pts) # find the fitting sphere if sph is None: continue value = sph.radius * 10 color = colorMap(value, "jet", 0, 1) # map value to a RGB color n = versor(p - sph.center) # unit vector from sphere center to p vals.append(value) cols.append(color) pts1.append(p) pts2.append(p + n / 8) vp += Points(pts1, c=cols) vp += Lines(pts1, pts2, c="black") vp += histogram(vals, xtitle='radius', xlim=[0,2]).pos(-1,0.5,-1) vp += Text2D(__doc__, pos=1) vp.show()
"""Probe a voxel dataset at specified points and plot a histogram of the values """ from vtkplotter import * from vtkplotter.pyplot import histogram import numpy as np vol = load(datadir + 'embryo.slc') pts = np.random.rand(4000, 3) * 256 mpts = probePoints(vol, pts).pointSize(3).printInfo() scals = mpts.getPointArray() h = histogram(scals, xlim=(5, 120), xtitle='voxel value') h.scale(2.2) show(vol, mpts, h, __doc__)
import numpy as np from vtkplotter.pyplot import histogram from vtkplotter import * np.random.seed(3) data1 = np.random.randn(250) data2 = (np.random.rand(250) - 0.5) * 12 hst1 = histogram( data1, bins=30, errors=True, aspect=4 / 3, title='My distributions', c='red', marker='o', ) # pick the 16th bin and color it violet hst1.unpack(15).c('violet') hst1 += Text('Highlight a\nspecial bin', pos=(0.5, 20), c='v') # A second histogram: # make it in same format as hst1 so it can be superimposed hst2 = histogram(data2, format=hst1, alpha=0.5) # Show both: show(hst1, hst2)
from vtkplotter import Hyperboloid, show from vtkplotter.pyplot import histogram import numpy as np np.random.seed(1) ################################################################## radhisto = histogram( np.random.rand(200) * 6.28, mode='polar', title="random orientations", bins=10, c=range(10), #'orange', #uniform color alpha=0.8, labels=["label" + str(i) for i in range(10)], ) show(radhisto, at=0, N=2, axes=0, sharecam=False) ################################################################## hyp = Hyperboloid(res=20).cutWithPlane().rotateY(-90) hyp.color('grey').alpha(0.3) # select 10 random indices of points on the surface idx = np.random.randint(0, hyp.NPoints(), size=10) radhistos = [] for i in idx: #generate a random histogram rh = histogram( np.random.randn(100), mode='polar',
"""Histogram of 2 variables""" from vtkplotter import * from vtkplotter.pyplot import histogram import numpy as np n = 10000 x = np.random.normal(2, 1, n) * 2 + 3 y = np.random.normal(1, 1, n) * 1 + 7 xm, ym = np.mean(x), np.mean(y) h = histogram( x, y, bins=50, aspect=4 / 3, # cmap='Blues', cmap='PuBu', title='2D Gauss histo', ) # add some object to the plot h += Marker('*', s=0.3, c='r').pos(xm, ym, 0.1) show(h)