"""A simple scatter plot""" from vtkplotter import show from vtkplotter.pyplot import plot import numpy as np x = np.random.randn(100)+10 y = np.random.randn(100)*20 plt = plot( x, y, lw=0, xtitle="variable x", ytitle="variable y", aspect=4/3, # aspect ratio marker="*", # marker style mc="dr", # marker color axes=True, ) # show Assembly object and lock interaction to 2d: # (can zoom in a region w/ mouse, press r to reset) show(plt, __doc__, viewup='2d')
from vtkplotter.pyplot import plot # Make up same data x = np.arange(0, 6, 0.1) y = 2+2*np.sin(2*x)/(x+1) ye= y**2 / 10 miny = np.min(y-ye) idx = np.argmax(y) # Plot the two variables, return a Plot(Assembly) object: plt = plot(x,y, yerrors=ye, xtitle='time in seconds', ytitle='y oscillation [a.u.]', ylim=(0.5, 5), aspect=4/3, # aspect ratio (any float = x_size/y_size) errorBand=True, # join errors on y into an error band lc="k", # line color ec="r", # error band color la=0.6, # error and line alphas pad=0.0, # tight margins, no padding ) # Add a grey transparent rectangle to represent an exclusion region: plt += Rectangle([1,0.5], [2.7,5], alpha=0.2, c='k') # Add some text and latex formula plt += Text("excluded", s=0.2, c='k').rotateZ(20).pos(1.3, 3.7) plt += Latex(r"y(t)=2+2\cdot\frac{\sin(2t)}{(t+1)}", pos=(4.7, 4.7), s=.8, c='db') # Add a star marker at maximum of function (at z=0.1, so it stays on top): plt += Marker('*', pos=(x[idx], y[idx], 0.1), c='blue')
n = 1000 x = np.random.randn(n) y = np.random.randn(n) # define what size must have each marker: marker_sizes = np.sin(2*x)/8 # define a (r,g,b) list of colors for each marker: marker_cols = np.c_[np.cos(2*x), np.zeros(n), np.zeros(n)] txt0 = Text2D("A scatter plot of a\n 2D gaussian distribution") plt0 = plot(x, y, ma=0.3, lw=0, # ma = marker alpha marker="*", # marker style xtitle="variable A", ytitle="variable B", ) txt1 = Text2D(" marker size = sin(2x) ") plt1 = plot(x, y, ma=0.3, lw=0, marker="*", # marker style ms=marker_sizes, # VARIABLE marker sizes mc='red', # same color for markers ) txt2 = Text2D(" marker size = sin(2x)\n red level = cos(2x)") plt2 = plot(x, y, ma=0.3, lw=0, marker=">", # marker style ms=marker_sizes, # VARIABLE marker sizes mc=marker_cols, # VARIABLE marker colors
"""Probe a Volume with a line and plot the intensity values""" from vtkplotter import * from vtkplotter.pyplot import plot vol = load(datadir + 'vase.vti') vol.addScalarBar3D(title='vase', c='k', italic=1) p1, p2 = (10, 10, 10), (90, 90, 90) pl = probeLine(vol, p1, p2, res=50).lineWidth(4) xvals = pl.points()[:, 0] yvals = pl.getPointArray() plt = plot( xvals, yvals, spline=True, lc="r", # line color marker="*", # marker style mc="dr", # marker color ms=0.6, # marker size ) show([(vol, pl, __doc__), plt], N=2, sharecam=False)
"""Surface plotting in spherical coordinates spherical harmonic function is: Y(l=2, m=0) = 3*cos(theta)**2 - 1 (red points are made NaN on purpose) """ from vtkplotter import * from vtkplotter.pyplot import plot import numpy as np def rhofunc(theta, phi): if theta < 0.2: return np.nan # make some points invalid #return cos(theta)**2 # Y(l=1 m=0) return (3*cos(theta)**2 - 1)**2 # Y(l=2 m=0) #return (5*cos(theta)**3 - 3*cos(theta))**2 # Y(l=3 m=0) # Build the plot, # return an Assembly of 3 meshes, the unit # grid sphere, the surface rho(theta, phi) and # the red Points where rho is a complex number: spl = plot(rhofunc, mode='spheric', cmap='viridis') show(spl, __doc__, axes=12, viewup='z')
"""Use of plot() function analogous to matplotlib""" import numpy as np, vtk from vtkplotter import * from vtkplotter.pyplot import plot x = np.linspace(0, 5, 10) plt1 = plot(x, x * x, 'sg-', title='Plot1: y=x*x') plt2 = plot(x, cos(x), 'pr--', title='Plot2: y=cos(x)') plt3 = plot(x, sqrt(x), 'Db-', title='Plot3: y=sqrt(x)') plt4 = plot(x, sin(x), '*t--', title='Plot4: y=sin(x)') # window shape can be expressed as "n/m" or "n|m" show(plt1, plt2, plt3, plt4, shape="3|1", sharecam=False, size=(1300, 900)) printc('plt1 is vtkAssembly?', isinstance(plt1, vtk.vtkAssembly))
z = (x - 1)**2 + (y - 1)**2 + 9 * sin(y - 1)**2 + 1 return z / 12 def func(v): return f(v[0], v[1]) def callbk(optimizer, v, value): global minv if value < minv: pts.append([v.value[0], v.value[1], value]) minv = value optimizer = ng.optimizers.OnePlusOne(parametrization=2, budget=100) pts, minv = [], 1e30 optimizer.register_callback("tell", callbk) # define a constraint on first variable of x: #optimizer.parametrization.register_cheap_constraint(lambda v: v[0]>-3) res = optimizer.minimize(func) # best value printc('Minimum at:', res.value) ln = Line(pts, lw=3) fu = plot(f, xlim=[-3, 4], ylim=[-3, 4], alpha=0.5) show(fu, ln, __doc__)
from vtkplotter import * from vtkplotter.pyplot import plot ########################################################### REAL #Draw a surface representing a 2-var function specified #as a string or as a reference to an external existing function. #Red points indicate where the function does not exist. # an existing function z(x,y) can be passed: def my_z(x, y): return sin(2 * x * y) * cos(3 * y) / 2 f1 = plot(my_z) # red dots are shown where the function does not exist (y>x): f2 = plot("sin(3*x)*log(x-y)/3") # specify x and y ranges and z vertical limits: f3 = plot("log(x**2+y**2-1)", xlim=[-2, 2], ylim=[-1, 8], zlim=[-1, None]) show(f1, f2, f3, N=3, sharecam=False) ########################################################## COMPLEX comment = """Vertical axis shows the real part of complex z: z = sin(log(x*y)) Color map the value of the imaginary part (green=positive, purple=negative)""" plt = plot(lambda x, y: sin(log(x * y)) / 25, mode='complex')
from vtkplotter.pyplot import plot import numpy as np x = np.linspace(0, 10, num=21) y = 3 * np.sin(x) errs = np.ones_like(x) / 2 ################# first plot plt = plot( x, y, "*r-", # markers: *,o,p,h,D,d,v,^,s,x,a xtitle="x variable (mm)", ytitle="y(x)", aspect=16 / 9, # aspect ratio x/y of plot # xlim=(-1, 14), # specify x range # ylim=(-4, 5), # specify y range ) ################# plot on top of plt plt.plot( x + 3, y, "sb--", xerrors=errs, # set error bars on x yerrors=errs, # set error bars on y spline=True, # continous line through points lw=1.5, ) ################## plot again on top of plt