我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用matplotlib.pyplot.tripcolor()。
def phase_diagram_mesh(points, values, title="Phase diagram", xlabel="Pulse Duration (s)", ylabel="Pulse Amplitude (V)", shading="flat", voronoi=False, **kwargs): # fig = plt.figure() if voronoi: from scipy.spatial import Voronoi, voronoi_plot_2d points[:,0] *= 1e9 vor = Voronoi(points) cmap = mpl.cm.get_cmap('RdGy') # colorize for pr, v in zip(vor.point_region, values): region = vor.regions[pr] if not -1 in region: polygon = [vor.vertices[i] for i in region] plt.fill(*zip(*polygon), color=cmap(v)) else: mesh = scaled_Delaunay(points) xs = mesh.points[:,0] ys = mesh.points[:,1] plt.tripcolor(xs,ys,mesh.simplices.copy(),values, cmap="RdGy",shading=shading,**kwargs) plt.xlim(min(xs),max(xs)) plt.ylim(min(ys),max(ys)) plt.title(title, size=18) plt.xlabel(xlabel, size=16) plt.ylabel(ylabel, size=16) cb = plt.colorbar() cb.set_label("Probability",size=16) return mesh
def plot(f, n=100): '''Plot function over the standard quadrilateral. ''' x = numpy.linspace(-1, +1, n) y = numpy.linspace(-1, +1, n) X, Y = numpy.meshgrid(x, y) XY = numpy.stack([X, Y]) z = numpy.array(f(XY), dtype=float) triang = matplotlib.tri.Triangulation(X.flatten(), Y.flatten()) plt.tripcolor(triang, z.flatten(), shading='flat') plt.colorbar() # quad outlines X = numpy.array([[-1, 1, 1, -1, -1], [-1, -1, 1, 1, -1]]) plt.plot(X[0], X[1], '-k') plt.gca().set_aspect('equal') plt.axis('off') return
def plot(f, n=100): x0, x1 = -2, +2 y0, y1 = -2, +2 x = numpy.linspace(x0, x1, n) y = numpy.linspace(y0, y1, n) X, Y = numpy.meshgrid(x, y) XY = numpy.stack([X, Y]) z = numpy.array(f(XY), dtype=float) triang = matplotlib.tri.Triangulation(X.flatten(), Y.flatten()) plt.tripcolor(triang, z.flatten(), shading='flat') plt.colorbar() plt.gca().set_aspect('equal') plt.axis('off') return
def mplot_cellfunction(cellfn): C = cellfn.array() tri = mesh2triang(cellfn.mesh()) return plt.tripcolor(tri, facecolors=C)
def mplot_function(f, vmin, vmax, logscale): mesh = f.function_space().mesh() if (mesh.geometry().dim() != 2): raise AttributeError('Mesh must be 2D') # DG0 cellwise function if f.vector().size() == mesh.num_cells(): C = f.vector().get_local() if logscale: return plt.tripcolor(mesh2triang(mesh), C, vmin=vmin, vmax=vmax, norm=cls.LogNorm() ) else: return plt.tripcolor(mesh2triang(mesh), C, vmin=vmin, vmax=vmax) # Scalar function, interpolated to vertices elif f.value_rank() == 0: C = f.compute_vertex_values(mesh) if logscale: return plt.tripcolor(mesh2triang(mesh), C, vmin=vmin, vmax=vmax, norm=cls.LogNorm() ) else: return plt.tripcolor(mesh2triang(mesh), C, shading='gouraud', vmin=vmin, vmax=vmax) # Vector function, interpolated to vertices elif f.value_rank() == 1: w0 = f.compute_vertex_values(mesh) if (len(w0) != 2*mesh.num_vertices()): raise AttributeError('Vector field must be 2D') X = mesh.coordinates()[:, 0] Y = mesh.coordinates()[:, 1] U = w0[:mesh.num_vertices()] V = w0[mesh.num_vertices():] C = np.sqrt(U*U+V*V) return plt.quiver(X,Y,U,V, C, units='x', headaxislength=7, headwidth=7, headlength=7, scale=4, pivot='middle') # Plot a generic dolfin object (if supported)
def plot_faces(coords, faces, face_values=None, title=None, clabel=None, colorbar=True, save=None, show=True): """ Plot a mesh with values given at faces. """ if face_values is None: colors = np.arange(len(faces)) clabel = "Face number" else: colors = face_values fig = Figure(title=title, clabel=clabel, colorbar=colorbar, save=save, show=show) plt.tripcolor(coords[:, 0], coords[:, 1], faces, facecolors=colors, edgecolors='k') return fig
def plot_fancy(nodes, elems, phi, charge, u=None, charge_max=None, show=False, save=None): """ Plots fancily. """ fig = Figure(colorbar=False, tight_layout=True, show=show, xlabel="", ylabel="", save=save, ticks=False) if charge_max is None: charge_max = max(np.max(np.abs(charge)), 1e-10) cmap = plt.cm.get_cmap('Greys') cmap._init() cmap._lut[:, :] = 0. length = len(cmap._lut[:, -1]) # cmap._lut[:, -1] = np.linspace(0., 1.0, length) cmap._lut[:length/2, -1] = 0. cmap._lut[length/2:, -1] = 1. phi[phi > 1.] = 1. phi[phi < -1.] = -1. plt.tripcolor(nodes[:, 0], nodes[:, 1], elems, charge, cmap=plt.get_cmap("coolwarm"), shading="gouraud", vmin=-charge_max, vmax=charge_max) plt.tricontourf(nodes[:, 0], nodes[:, 1], elems, phi, cmap=cmap, levels=[-2.0, 0., 2.0], antialiased=True) if u is not None: u_norm = np.sqrt(u[:, 0]**2 + u[:, 1]**2) + 1e-10 colors = phi norm = plt.Normalize() norm.autoscale(colors) colormap = cmap # plt.cm.get_cmap('inferno') cmap._lut[:, -1] = 0.5 cmap._lut[length/2:, :-1] = 1. fig.ax.quiver(nodes[:, 0], nodes[:, 1], u[:, 0]/u_norm, u[:, 1]/u_norm, color=colormap(norm(colors))) return fig
def TriAndPaint(img, points, outputIMG): tri = Delaunay(points) triList = points[tri.simplices] cMap = ListedColormap( np.array([ChooseColor(img, tr) for tr in triList])) # use core rgb # center = np.sum(points[tri.simplices], axis=1) / 3 # print(center) # cMap = ListedColormap( # np.array([img.getpixel((x, y)) for x, y in center]) / 256) color = np.array(range(len(triList))) # print(color) width, height = img.size plt.figure(figsize=(width, height), dpi=1) plt.tripcolor(points[:, 0], points[:, 1], tri.simplices.copy(), facecolors=color, cmap=cMap) # plt.tick_params(labelbottom='off', labelleft='off', # left='off', right='off', bottom='off', top='off') # plt.tight_layout(pad=0) plt.axis('off') plt.subplots_adjust(left=0, right=1, top=1, bottom=0) plt.xlim(0, width) plt.ylim(0, height) plt.gca().invert_yaxis() plt.savefig(outputIMG) # uncomment show() if you want to view when it's done # plt.show()
def plot(corners, f, n=100): '''Plot function over a triangle. ''' # discretization points def partition(boxes, balls): # <https://stackoverflow.com/a/36748940/353337> def rec(boxes, balls, parent=tuple()): if boxes > 1: for i in range(balls + 1): for x in rec(boxes - 1, i, parent + (balls - i,)): yield x else: yield parent + (balls,) return list(rec(boxes, balls)) bary = numpy.array(partition(3, n)).T / n X = numpy.sum([ numpy.outer(bary[k], corners[:, k]) for k in range(3) ], axis=0).T # plot the points # plt.plot(X[0], X[1], 'xk') x = numpy.array(X[0]) y = numpy.array(X[1]) z = numpy.array(f(bary), dtype=float) triang = matplotlib.tri.Triangulation(x, y) plt.tripcolor(triang, z, shading='flat') plt.colorbar() # triangle outlines X = numpy.column_stack([corners, corners[:, 0]]) plt.plot(X[0], X[1], '-k') plt.gca().set_aspect('equal') plt.axis('off') return