Exemple #1
0
    def grid_plot(self):
        """
        plotting the grid
        """
        maxfaces = self.cells.shape[1]

        pointdata = self.readTXT('%s/points.dat' % self.dire)
        xp1 = (pointdata[:, 0] - self.originx) / 1.
        yp1 = (pointdata[:, 1] - self.originy) / 1.
        #########################################################
        xp = np.zeros((self.Nc, maxfaces + 1))
        yp = np.zeros((self.Nc, maxfaces + 1))

        cells = self.cells.copy()
        #cells[cells.mask]=0
        xp[:, :maxfaces] = xp1[cells]
        xp[range(self.Nc), self.nfaces] = xp1[cells[:, 0]]
        yp[:, :maxfaces] = yp1[cells]
        yp[range(self.Nc), self.nfaces] = yp1[cells[:, 0]]
        ##########################################################
        xy = np.zeros((maxfaces + 1, 2))

        def _closepoly(ii):
            nf = self.nfaces[ii] + 1
            xy[:nf, 0] = xp[ii, :nf]
            xy[:nf, 1] = yp[ii, :nf]
            return xy[:nf, :].copy()

        cellxy = [_closepoly(ii) for ii in range(self.Nc)]

        #xlims = np.asarray([self.xv.min(), self.xv.max()])
        #ylims = np.asarray([self.yv.min(), self.yv.max()])

        xlims = np.asarray([616832, 709049]) - self.originx
        ylims = np.asarray([2854748, 3050710]) - self.originy

        fig = plt.figure(figsize=(12, 10))
        ax = fig.add_subplot(111)
        #cmap = plt.set_cmap('bwr')
        collection = PolyCollection(cellxy, facecolors='none')
        #collection.set_array(np.array(self.L[:]))
        collection.set_edgecolors('k')
        collection.set_linewidths(0.3)
        ax.add_collection(collection)
        lc = self.outline()  #line collection of outline
        ax.add_collection(lc)
        ax.set_xlim(xlims)
        ax.set_ylim(ylims)
        ax.set_aspect('equal')
        ax.tick_params(labelsize=22)
        ax.set_xlabel('Easting (m)', fontsize=22)
        ax.set_ylabel('Northing (m)', fontsize=22)
        fig.tight_layout()

        plt.show()
Exemple #2
0
class Mesh():
    """
    Mesh described by vertices and faces
    """
    def __init__(self,
                 ax,
                 transform,
                 vertices,
                 faces,
                 cmap=None,
                 facecolors="white",
                 edgecolors="black",
                 linewidths=0.5,
                 mode="front"):
        """
        """

        self.collection = PolyCollection([], clip_on=False, snap=False)
        self.vertices = vertices
        self.faces = faces
        self.cmap = cmap
        self.facecolors = mpl.colors.to_rgba_array(facecolors)
        self.edgecolors = mpl.colors.to_rgba_array(edgecolors)
        self.linewidths = linewidths
        self.mode = mode
        self.update(transform)
        ax.add_collection(self.collection, autolim=False)

    def update(self, transform):
        """
        Update mesh according to transform (4x4 array)
        """

        T = glm.transform(self.vertices, transform)[self.faces]
        Z = -T[:, :, 2].mean(axis=1)

        if self.cmap is not None:
            # Facecolors using depth buffer
            norm = mpl.colors.Normalize(vmin=Z.min(), vmax=Z.max())
            facecolors = self.cmap(norm(Z))

        else:
            facecolors = self.facecolors
        edgecolors = self.edgecolors
        linewidths = self.linewidths

        # Back face culling
        if self.mode == "front":
            front, back = glm.frontback(T)
            T, Z = T[front], Z[front]
            if len(facecolors) == len(self.faces):
                facecolors = facecolors[front]
            if len(edgecolors) == len(self.faces):
                edgecolors = edgecolors[front]

        # Front face culling
        elif self.mode == "back":
            front, back = glm.frontback(T)
            T, Z = T[back], Z[back]
            if len(facecolors) == len(faces):
                facecolors = facecolors[back]
            if len(edgecolor) == len(faces):
                edgecolors = edgecolors[back]

        # Separate 2d triangles from zbuffer
        triangles = T[:, :, :2]
        antialiased = linewidths > 0

        # Sort triangles according to z buffer
        I = np.argsort(Z)
        triangles = triangles[I, :]
        if len(facecolors) == len(I):
            facecolors = facecolors[I, :]
        if len(edgecolors) == len(I):
            edgecolors = edgecolors[I, :]

        self.collection.set_verts(triangles)
        self.collection.set_linewidths(linewidths)
        self.collection.set_facecolors(facecolors)
        self.collection.set_edgecolors(edgecolors)
        self.collection.set_antialiased(antialiased)
Exemple #3
0
lLCollection=PolyCollection(vivll[whichVertsToPlot][w],zorder=10)
col1=(c-cMin)/(cMax-cMin)
col1=num.clip(col1,0.,1.)
if showBinaryColours:
    ww=num.where(c>0.3)
    www=num.where(c<=0.3)
    col1[ww]=1.
    col1[www]=0.1
#wc=num.where(c>0.1)
#col1[:]=1.
collectionColours=cmap(col1)
collectionColours[:,3]=alpha
#collectionColours[w][:,3]=0.0
lLCollection.set_facecolors(collectionColours[whichVertsToPlot][w])
lLCollection.set_linewidths(0.0)

slLCollection=PolyCollection(vivll[whichVertsToPlot][w])
scollectionColours=cmap(s/num.max(s))
scollectionColours[:,3]=alpha
#collectionColours[w][:,3]=0.0
slLCollection.set_facecolors(scollectionColours[whichVertsToPlot][w])
slLCollection.set_linewidths(0.0)





meanWDs=num.array(meanWDs)
stdWDs=num.array(stdWDs)#/meanWDs
x=num.array(x)
Exemple #4
0
class Bar:
    """ Bar (histogram) """
    def __init__(self,
                 ax,
                 transform,
                 Z,
                 facecolors="white",
                 edgecolors="black",
                 linewidth=0,
                 clip=False):
        """ """

        self.Z = Z
        if isinstance(facecolors, np.ndarray):
            shape = facecolors.shape
            facecolors = facecolors.reshape(-1, shape[-1])
            facecolors = mpl.colors.to_rgba_array(facecolors)
            self.facecolors = facecolors.reshape(shape[0], shape[1], 4)
        else:
            shape = Z.shape
            self.facecolors = np.zeros((shape[0], shape[1], 4))
            self.facecolors[...] = mpl.colors.to_rgba(facecolors)

        if isinstance(edgecolors, np.ndarray):
            shape = edgecolors.shape
            edgecolors = edgecolors.reshape(-1, shape[-1])
            edgecolors = mpl.colors.to_rgba_array(edgecolors)
            self.edgecolors = edgecolors.reshape(shape[0], shape[1], 4)
        else:
            shape = Z.shape
            self.edgecolors = np.zeros((shape[0], shape[1], 4))
            self.edgecolors[...] = mpl.colors.to_rgba(edgecolors)

        self.linewidth = linewidth
        self.xlim = -0.5, +0.50
        self.ylim = -0.5, +0.50
        self.zlim = -0.5, +0.50
        self.clip = clip

        # Because all the bars have the same orientation, we can use a hack to
        # shade each face at once instead of computing individual face lighting.
        self.shade = np.array([[1.00, 1.00, 0.75, 1.00, 0.50, 1.00]])

        self.collection = PolyCollection([], clip_on=self.clip, snap=False)
        self.update(transform)
        ax.add_collection(self.collection, autolim=False)

    def update(self, transform):
        """ """

        Z = self.Z
        xmin, xmax = self.xlim
        ymin, ymax = self.ylim
        zmin, zmax = self.zlim
        dx, dy = 0.5 * 1 / Z.shape[0], 0.5 * 1 / Z.shape[1]

        # Each bar is described by 8 vertices and 6 faces
        V = np.zeros((Z.shape[0], Z.shape[1], 8, 3))
        F = np.zeros((Z.shape[0], Z.shape[1], 6, 4), dtype=int)

        # Face and edge colors for the six faces
        FC = np.zeros((Z.shape[0], Z.shape[1], 6, 4))
        FC[:, :] = self.facecolors.reshape(Z.shape[0], Z.shape[1], 1, 4)
        FC *= self.shade.T
        FC[:, :, :, 3] = 1

        EC = np.zeros((Z.shape[0], Z.shape[1], 6, 4))
        EC[:, :] = self.edgecolors.reshape(Z.shape[0], Z.shape[1], 1, 4)

        # Build vertices
        X, Y = np.meshgrid(np.linspace(xmin, xmax, Z.shape[0]),
                           np.linspace(ymin, ymax, Z.shape[1]))
        V[..., 0] = X.reshape(Z.shape[0], Z.shape[1], 1)
        V[..., 1] = Y.reshape(Z.shape[0], Z.shape[1], 1)

        V[:, :, 0] += [+dx, +dy, zmin]
        V[:, :, 1] += [+dx, -dy, zmin]
        V[:, :, 2] += [-dx, -dy, zmin]
        V[:, :, 3] += [-dx, +dy, zmin]

        V[:, :, 4] += [+dx, +dy, zmin]
        V[:, :, 5] += [+dx, -dy, zmin]
        V[:, :, 6] += [-dx, -dy, zmin]
        V[:, :, 7] += [-dx, +dy, zmin]
        V[:, :, 4:, 2] += Z.reshape(Z.shape[0], Z.shape[1], 1)

        # Build faces
        I = 8 * np.arange(Z.shape[0] * Z.shape[1])
        F[:, :] = I.reshape(Z.shape[0], Z.shape[1], 1, 1)
        F[:, :] += [
            [0, 1, 2, 3],  # -Z
            [0, 1, 5, 4],  # +X
            [2, 3, 7, 6],  # -X
            [1, 2, 6, 5],  # -Y
            [0, 3, 7, 4],  # +Y
            [4, 5, 6, 7]
        ]  # +Z

        # Actual transformation
        V = V.reshape(-1, 3)
        V = glm.transform(V[F], transform)  #[...,:2]

        # Depth computation
        # We combine the global "depth" of the bar (depth of the bottom face)
        # and the local depth of each face. This trick avoids problems when
        # sorting all the different faces.
        Z1 = (V[:, :, 0, :, 2].mean(axis=2)).reshape(Z.shape[0], Z.shape[1], 1)
        Z2 = (V[..., 2].mean(axis=3) + 10 * Z1).ravel()

        # Sorting
        I = np.argsort(-Z2)
        V = (V[..., :2].reshape(Z.shape[0] * Z.shape[1] * 6, 4, 2))

        self.collection.set_verts(V[I])
        self.collection.set_facecolors(FC.reshape(-1, 4)[I])
        self.collection.set_edgecolors(EC.reshape(-1, 4)[I])
        self.collection.set_linewidths(self.linewidth)
        if self.linewidth == 0.0:
            self.collection.set_antialiased(False)
        else:
            self.collection.set_antialiased(True)
                    ax.legend(loc='upper right')
                    plt.savefig(case + '_' + str(n) + '.png',
                                bbox_inches='tight')
                    plt.close('all')
                elif 'U-chan' in case and n % 10 == 0:
                    fig = plt.figure()
                    ax = fig.add_subplot(111)
                    collection = PolyCollection(
                        xy,
                        cmap=plt.get_cmap('jet'))  # 'Blues' 'jet' 'ocean' mcm
                    ui = self.get_center_vel(uj)
                    uimag = np.sqrt(
                        np.multiply(ui[:, 0], ui[:, 0]) +
                        np.multiply(ui[:, 1], ui[:, 1]))
                    collection.set_array(uimag)
                    collection.set_linewidths(0.2)
                    collection.set_edgecolors('none')
                    collection.set_clim(vmin=0.15, vmax=0.45)
                    ax.add_collection(collection)
                    ax.set_xlim([-2.2, 2.5])
                    ax.set_ylim([-0.1, 2.6])
                    ax.set_aspect('equal')
                    plt.savefig(case + '_' + str(n) + '.png',
                                bbox_inches='tight')
                    plt.close('all')

        return hi, uj, ei

    def get_edge_x_vel(self, uj):
        """
        Return x direction velocity and location at cell edge midpoints