def plot_sln_mayavi(sln, notebook=False): """ Plots the Solution() instance sln using Linearizer() and matplotlib. Currently only a very simple version is implemented, that takes the vertices from linearizer and interpolates them. More sophisticated version should take the triangles. """ lin = Linearizer() lin.process_solution(sln) vert = lin.get_vertices() triangles = lin.get_triangles() from numpy import zeros from enthought.mayavi import mlab x = vert[:, 0] y = vert[:, 1] z = zeros(len(y)) t = vert[:, 2] if notebook: # the off screen rendering properly works only with VTK-5.2 or above: mlab.options.offscreen = True s = mlab.triangular_mesh(x, y, z, triangles, scalars=t) mlab.view(0, 0) # Below is a code that does exactly what the "View along the +Z axis" # button does: #scene = mlab.get_engine().current_scene.scene #scene.camera.focal_point = [0, 0, 0] #scene.camera.position = [0, 0, 1] #scene.camera.view_up = [0, 1, 0] #scene.renderer.reset_camera() #scene.render() # the above looks ok, but there is still quite a large margin, so we prefer # to just call .view(0, 0), which seems to be working fine. return s
def plot_sln_mayavi(sln, offscreen=False, show_scale=True): """ Plots the Solution() instance sln using Linearizer() and matplotlib. It takes the vertices from linearizer and interpolates them. """ lin = Linearizer() lin.process_solution(sln) vert = lin.get_vertices() triangles = lin.get_triangles() from numpy import zeros from enthought.mayavi import mlab x = vert[:, 0] y = vert[:, 1] z = zeros(len(y)) t = vert[:, 2] if offscreen: # the off screen rendering properly works only with VTK-5.2 or above: mlab.options.offscreen = True mlab.clf() mlab.figure(bgcolor=(1, 1, 1), fgcolor=(0, 0, 0)) s = mlab.triangular_mesh(x, y, z, triangles, scalars=t) mlab.view(0, 0) mlab.view(distance=4) mlab.view(focalpoint=(.35,0,0)) mlab.colorbar(title="Solution", orientation="vertical") #mlab.move(right=-1.0, up=-10.0) # Below is a code that does exactly what the "View along the +Z axis" # button does: #scene = mlab.get_engine().current_scene.scene #scene.camera.focal_point = [0, 0, 0] #scene.camera.position = [0, 0, 1] #scene.camera.view_up = [0, 1, 0] #scene.renderer.reset_camera() #scene.render() # the above looks ok, but there is still quite a large margin, so we prefer # to just call .view(0, 0), which seems to be working fine. return mlab
def plot_sln_mayavi(sln, offscreen=False, show_scale=True): """ Plots the Solution() instance sln using Linearizer() and matplotlib. It takes the vertices from linearizer and interpolates them. """ lin = Linearizer() lin.process_solution(sln) vert = lin.get_vertices() triangles = lin.get_triangles() from numpy import zeros from enthought.mayavi import mlab x = vert[:, 0] y = vert[:, 1] z = zeros(len(y)) t = vert[:, 2] if offscreen: # the off screen rendering properly works only with VTK-5.2 or above: mlab.options.offscreen = True mlab.clf() mlab.figure(bgcolor=(1, 1, 1), fgcolor=(0, 0, 0)) s = mlab.triangular_mesh(x, y, z, triangles, scalars=t) mlab.view(0, 0) mlab.view(distance=4) mlab.view(focalpoint=(.35, 0, 0)) mlab.colorbar(title="Solution", orientation="vertical") #mlab.move(right=-1.0, up=-10.0) # Below is a code that does exactly what the "View along the +Z axis" # button does: #scene = mlab.get_engine().current_scene.scene #scene.camera.focal_point = [0, 0, 0] #scene.camera.position = [0, 0, 1] #scene.camera.view_up = [0, 1, 0] #scene.renderer.reset_camera() #scene.render() # the above looks ok, but there is still quite a large margin, so we prefer # to just call .view(0, 0), which seems to be working fine. return mlab
def plot_sln_mayavi(sln, notebook=False): """ Plots the Solution() instance sln using Linearizer() and matplotlib. Currently only a very simple version is implemented, that takes the vertices from linearizer and interpolates them. More sophisticated version should take the triangles. """ lin = Linearizer() lin.process_solution(sln) vert = lin.get_vertices() triangles = lin.get_triangles() from numpy import zeros from enthought.mayavi import mlab x = vert[:, 0] y = vert[:, 1] z = zeros(len(y)) t = vert[:, 2] if notebook: # the off screen rendering properly works only with VTK-5.2 or above: mlab.options.offscreen = True mlab.clf() s = mlab.triangular_mesh(x, y, z, triangles, scalars=t) mlab.view(0, 0) # Below is a code that does exactly what the "View along the +Z axis" # button does: #scene = mlab.get_engine().current_scene.scene #scene.camera.focal_point = [0, 0, 0] #scene.camera.position = [0, 0, 1] #scene.camera.view_up = [0, 1, 0] #scene.renderer.reset_camera() #scene.render() # the above looks ok, but there is still quite a large margin, so we prefer # to just call .view(0, 0), which seems to be working fine. return s
def plot_sln_mpl(sln, method="default", just_mesh=False, axes=None): """ Plots the Solution() instance sln using Linearizer() and matplotlib. method = "default" ... creates a plot using triangles (the triangles are not interpolated, so sometimes one can see small defects) method = "contour" ... takes the vertices from linearizer and interpolates them using contour and contourf (it doesn't take into account the triangulation, so one can see the defects from the convex hull approximation) just_mesh ... only shows the mesh, but not the solution """ lin = Linearizer() lin.process_solution(sln) v = lin.get_vertices() if method=="contour": from numpy import min, max, linspace from matplotlib.mlab import griddata import matplotlib.pyplot as plt x = v[:, 0] y = v[:, 1] z = v[:, 2] # define grid. xi = linspace(min(x), max(x), 100) yi = linspace(min(y), max(y), 100) # grid the data. zi = griddata(x, y, z, xi, yi) # contour the gridded data, plotting dots at the nonuniform data points. CS = plt.contour(xi, yi, zi, 15, linewidths=0.5, colors='k') CS = plt.contourf(xi, yi, zi, 15, cmap=plt.cm.jet) plt.colorbar() plt.title('Solution') elif method == "default": from numpy import array import matplotlib.collections as collections #import matplotlib.pyplot as plt if axes is None: from pylab import gca axes = gca() verts = [] vals = [] for t in lin.get_triangles(): triangle = tuple([tuple(v[n][:2]) for n in t]) val = sum([v[n][2] for n in t]) vals.append(val/3.) verts.append(triangle) verts = array(verts) vals = array(vals) if just_mesh: lw = 1 else: lw = 0 col = collections.PolyCollection(verts, linewidths=lw, antialiaseds=0) col.set_array(vals) #col.set_cmap(plt.cm.jet) ax = axes ax.add_collection(col) ax.set_xlim(verts[:, :, 0].min(), verts[:, :, 0].max()) ax.set_ylim(verts[:, :, 1].min(), verts[:, :, 1].max()) ax.set_aspect("equal") #plt.colorbar() #plt.title('Solution') else: raise ValueError("Unknown method (%s)" % method)