Exemplo n.º 1
0
def plotmesh(Th, **kwargs):
    assert issiMesh(Th), "First argument must be a siMesh object"
    d = kwargs.pop('d', Th.d)
    latex = kwargs.pop('latex', True)
    all_labels = Th.sThlab[Th.find(d)]
    labels = np.intersect1d(kwargs.pop('labels', all_labels), all_labels)
    if np.isscalar(labels):
        labels = [labels]
    legend = kwargs.pop('legend', False)
    if len(labels) == 0:
        print('No submesh found!')
        return
    #plt.rc('text', usetex=True, text.latex.unicode)
    #fig = plt.gcf()
    Legend_handle = []
    Labels = []
    LabName = LabelBaseName(Th, d)
    for l in labels:
        k = Th.find(d, labels=[l])
        if len(k) == 1:
            lh = sME.plotmesh(Th.sTh[k[0]], **kwargs)
            if legend:
                if isinstance(lh, list):
                    lh = lh[0]
                if isinstance(
                        lh, Poly3DCollection
                ):  # BUG: plt.legend does not accept Poly3DCollection as input
                    lkwargs = kwargs
                    color = check_color(
                        lkwargs.pop('color', Th.sTh[k[0]].color))
                    edgecolor = check_color(lkwargs.pop('edgecolor', 'White'))
                    facecolor = check_color(lkwargs.pop('facecolor', color))
                    Legend_handle.append(
                        Patch(facecolor=facecolor,
                              edgecolor=edgecolor,
                              **kwargs))
                else:
                    Legend_handle.append(lh)
                if latex:
                    Labels.append(r'$%s_{%d}$' % (LabName, l))
                else:
                    Labels.append(mesh_label(Th.sTh[k[0]]))

    #ax=fig.axes[0]
    #ax.set_axis_off()
    #ax.set_axis_bgcolor('None')
    if legend:
        plt.legend(Legend_handle,
                   Labels,
                   loc='best',
                   ncol=int(len(Legend_handle) / 10) + 1).draggable()
    #coef=1.0
    #m=min(Th.bbox);M=max(Th.bbox)
    #if Th.dim==3:
    #ax.set_zlim(m*coef,M*coef)
    #ax.set_xlim(m*coef,M*coef)
    #ax.set_ylim(m*coef,M*coef)
    #ax.set_aspect('equal')
    set_axes(plt.gca(), Th.bbox)
    return Legend_handle
Exemplo n.º 2
0
def plotmesh_SubTh2simp3D(q, me, **kwargs):
    lim = kwargs.pop('lim', True)
    color = check_color(kwargs.pop('color', 'Blue'))
    kwargs['edgecolor'] = check_color(kwargs.pop('edgecolor', color))
    kwargs['facecolor'] = check_color(kwargs.pop('facecolor', color))
    #if facecolor is None:
    #facecolor='none'
    cut_planes = kwargs.pop('cut_planes', [])
    ME = cutMeshElements(q, me, cut_planes)
    fig = plt.gcf()
    if len(fig.axes) > 0:
        ax = fig.axes[0]
    else:
        ax = fig.gca(projection='3d')
    polys = Poly3DCollection(q[ME], **kwargs)
    #polys.set_facecolor(facecolor)
    coll = ax.add_collection(
        polys)  # BUG: with alpha=... don't work with ax.add_collection3d
    if lim:
        box = np.array([
            np.min(q[:, 0]),
            np.max(q[:, 0]),
            np.min(q[:, 1]),
            np.max(q[:, 1]),
            np.min(q[:, 2]),
            np.max(q[:, 2])
        ])
        set_axes(ax, box)
    return coll
Exemplo n.º 3
0
def plotmesh_SubTh2simp2D(q, me, **kwargs):
    lim = kwargs.pop('lim', True)
    color = check_color(kwargs.pop('color', 'Blue'))
    kwargs['edgecolor'] = check_color(kwargs.pop('edgecolor', color))
    facecolor = check_color(kwargs.pop('facecolor', None))
    if facecolor is None:
        facecolor = 'none'
    z = kwargs.pop('z', None)
    if z is None:
        fig = plt.gcf()
        ax = fig.gca()
        polys = PolyCollection(q[me], **kwargs)
        polys.set_facecolor(facecolor)
        coll = ax.add_collection(polys)
        if lim:
            box = np.array([
                np.min(q[:, 0]),
                np.max(q[:, 0]),
                np.min(q[:, 1]),
                np.max(q[:, 1])
            ])
            set_axes(ax, box)
        return coll
        #return plt.triplot(q[:,0],q[:,1],triangles=me,**kwargs)
    nq = q.shape[0]
    assert isinstance(z, np.ndarray) and z.shape[0] == nq
    z.resize((nq, 1))
    return plotmesh_SubTh2simp3D(np.concatenate((q, z), axis=1),
                                 me,
                                 lim=lim,
                                 **kwargs)
Exemplo n.º 4
0
def mlab_latex3D(latex_string, **kwargs):
    preamble = kwargs.pop('preamble', '')
    dpi = kwargs.pop('dpi', 900)
    kwargs['mode'] = kwargs.pop('mode', 'point')
    kwargs['color'] = check_color(kwargs.pop('color', 'White'))
    width = kwargs.pop('width', None)
    height = kwargs.pop('heigth', 1.0)
    center = kwargs.pop('center', (0, 0, 0))
    img_array = latex_out(latex_string, preamble, dpi)
    index_array = np.where(img_array != 255)
    x = index_array[0]
    y = index_array[1]
    minx = x.min()
    miny = y.min()
    maxx = x.max()
    maxy = y.max()
    if width is not None:
        hx = (maxx - minx) / width
        x = (x - minx) / hx
        y = (y - miny) / hx
        x = x - (x.max() - x.min()) / 2 + center[0]
        y = y - (y.max() - y.min()) / 2 + center[1]
    else:
        hy = (maxy - miny) / height
        x = (x - minx) / hy
        y = (y - miny) / hy
        x = x - (x.max() - x.min()) / 2 + center[0]
        y = y - (y.max() - y.min()) / 2 + center[1]
    z = np.zeros(x.shape) + center[2]
    return mlab.points3d(x, y, z, name='LaTeX3D', **kwargs)
Exemplo n.º 5
0
def title(text, **kwargs):
    """ textproperty options
    textproperty={
      'background_color': (0.0, 1.0, 0.0),
      'background_opacity': 0.1143,
      'bold': 1,
      'color': (0.0, 0.0, 0.0),
      'font_family': 'arial',
      'font_family_min_value': 0,
      'font_file': None,
      'font_size': 16,
      'italic': 0,
      'italic_': 0,
      'justification': 'centered',
      'line_offset': 0.0,
      'line_spacing': 1.1,
      'opacity': 1.0,
      'orientation': 0.0,
      'shadow': 1,
      'shadow_offset': array([ 1, -1]),
      'vertical_justification': 'top'
    }
  """
    justification = kwargs.pop('justification', 'center')
    vertical_justification = kwargs.pop('vertical_justification', 'top')
    textproperty = kwargs.pop('textproperty', dict())
    kwargs['color'] = check_color(kwargs.pop('color', 'White'))
    t = mlab.text(0.5, 0.99, text, **kwargs)
    t.actor.text_scale_mode = 'none'
    t.actor.text_property.justification = justification
    t.actor.text_property.vertical_justification = vertical_justification
    if len(textproperty):
        t.actor.text_property.trait_set(**textproperty)
    return t
Exemplo n.º 6
0
def plotmesh(q, me, **kwargs):
    """
  mesh plotting
  
  Parameters
  ----------
    q : mesh vertices, dim-by-nq or nq-by-dim numpy array where
        dim is the space dimension (2 or 3) and nq the number of vertices.
    me: mesh elements connectivity array where elements are d-simplices. 
        me is a (d+1)-by-nme or nme-by-(d+1) numpy array where nme is the number 
        of mesh elements and d is the simplicial dimension:
          d=0: points, 
          d=1: lines, 
          d=2: triangle, 
          d=3: tetrahedron
                  
  Returns
  -------
    handles: 
        
  """
    kind = kwargs.pop('kind', 'simplicial')
    kwargs['color'] = check_color(kwargs.pop('color', 'Blue'))
    if kind == 'simplicial':
        q, me, dim, d = simplicial_dimension(q, me)
        move = kwargs.pop('move', None)
        Q = move_mesh(q, move)
        return eval("plotmesh_SubTh" + str(d) + "simp" + str(dim) +
                    "D(Q,me,**kwargs)")
    assert False, 'Not yet implemented for ' + kind + ' mesh elements'
Exemplo n.º 7
0
def plotiso_SubTh2simp2D(q, me, u, **kwargs):
    plane = kwargs.pop('plane', True)
    niso = kwargs.pop('niso', 15)
    isorange = kwargs.pop('isorange', None)
    color = check_color(kwargs.pop('color', None))
    cmap = kwargs.pop('cmap', plt.cm.get_cmap(name='viridis'))
    if isorange is None and niso > 1:
        isorange = np.linspace(min(u), max(u), num=niso)
    else:
        if np.isscalar(isorange):
            isorange = np.array([isorange])
        else:
            isorange = np.sort(isorange)
    if color is not None:
        kwargs['colors'] = np.tile(color, (len(isorange), 1))
    else:
        kwargs['cmap'] = cmap
    #kwargs['vmin']=kwargs.get('vmin',np.min(u))
    #kwargs['vmax']=kwargs.get('vmax',np.max(u))
    fig = plt.gcf()
    if plane:
        return plt.tricontour(
            q[:, 0], q[:, 1], me, u, isorange,
            **kwargs)  #vmin=vmin,vmax=vmax, shading=shading,**kwargs)
    nq = q.shape[0]
    assert isinstance(u, np.ndarray) and u.shape[0] == nq
    Q = np.concatenate((q, u.reshape((nq, 1))), axis=1)
    assert False, "Not yet implemented"
    return plotiso_SubTh2simp3D(Q, me, u, isorange=isorange, **kwargs)
Exemplo n.º 8
0
def plotmesh(sTh,**kwargs):
  assert issiMeshElt(sTh), "First argument must be a siMeshElt object"
  #kwargs['name']=kwargs.get('name', mesh_label(sTh) )
  kwargs['color']=check_color(kwargs.pop('color', sTh.color))
  z=kwargs.pop('z',None)
  if z is not None:
    kwargs['z']=getLocalValue(sTh,z)
  move=kwargs.pop('move',None)
  if move is not None:
    kwargs['move']=getLocalVector(sTh,move)
  return plt4sim.plotmesh(sTh.q,sTh.me,**kwargs)
Exemplo n.º 9
0
def quiver(Th, V, **kwargs):
    assert issiMesh(Th), "First argument must be a siMesh object"
    d = kwargs.pop('d', Th.d)
    labels = kwargs.pop('labels', Th.sThlab[Th.find(d)])
    if np.isscalar(labels):
        labels = [labels]
    scalars = kwargs.pop('scalars', None)
    scale_factor = kwargs.pop('scale_factor', 1)
    scale = kwargs.pop('scale', 1 / scale_factor)
    color = check_color(kwargs.pop('color', None))
    colormap = kwargs.pop('colormap', 'viridis')
    kwargs['cmap'] = kwargs.pop('map', plt.cm.get_cmap(name=colormap))
    nvec = kwargs.pop('nvec', None)

    if color is not None:
        kwargs['color'] = color
    else:
        if scalars is None:
            scalars = np.sqrt(np.sum(V**2, axis=0))
        if kwargs.get('clim', None) is None:
            vmin = np.min(scalars)
            vmax = np.max(scalars)
            vmin = kwargs.pop('vmin', vmin)
            vmax = kwargs.pop('vmax', vmax)
            kwargs['clim'] = kwargs.pop('clim', [vmin, vmax])

    Q, toGlobal = Th.merge_vertices(d=d, labels=labels)
    N = len(toGlobal)
    if np.isscalar(nvec):
        if nvec < N:
            I = np.random.choice(N, nvec, replace=False)
            N = nvec
            toGlobal = toGlobal[I]
            Q = Q[:, I]
    #toGlobal=[]
    #Q=np.zeros((Th.nq,Th.dim))
    #for l in labels:
    #k=Th.find(d,labels=[l])
    #if len(k)==1:
    #toGlobal=np.hstack((toGlobal,Th.sTh[k[0]].toGlobal))
    #Q[Th.sTh[k[0]].toGlobal]=Th.sTh[k[0]].q
    #toGlobal=np.int32(np.unique(toGlobal))
    #Q=Q[toGlobal].T
    W = np.zeros((Th.dim, N))
    for i in np.arange(Th.dim):
        W[i] = V[i][toGlobal]

    args = []
    if scalars is not None:
        if Th.dim == 2:
            args.append(scalars[toGlobal])
        if Th.dim == 3:
            cmap = plt.get_cmap()
            # One arrow : 3 lines (shaft,headA and headB) so 3 colors
            # Three arrows with colors value v1,v2 and v3: array is [ v1,v2,v3,v1,v1,v2,v2,v3,v3]!
            #       for N arrows [shaft1,...,shaftN, headA1,headB1,headA2,headB2,...,headAN,headBN]
            C = scalars[toGlobal]  # First all --
            C = np.concatenate((C, np.repeat(C, 2)))
            kwargs['colors'] = cmap(C)
            kwargs['array'] = C  # for colormap
    if Th.dim == 2:
        pq = plt.quiver(Q[0], Q[1], W[0], W[1], scale=scale, *args, **kwargs)
    if Th.dim == 3:
        fig = plt.gcf()
        if len(fig.axes) > 0:
            ax = fig.axes[0]
        else:
            ax = fig.gca(projection='3d')
        pq = ax.quiver(Q[0], Q[1], Q[2], W[0] / scale, W[1] / scale,
                       W[2] / scale, **kwargs)

    return pq
Exemplo n.º 10
0
def quiver(q, V, **kwargs):
    dim = q.shape[1]
    assert V.shape[0] == dim
    scalars = kwargs.pop('scalars', None)
    scale_factor = kwargs.pop('scale_factor', 1)
    scale = kwargs.pop('scale', 1 / scale_factor)
    color = check_color(kwargs.pop('color', None))
    colormap = kwargs.pop('colormap', 'viridis')
    #kwargs['cmap']=kwargs.pop('map',plt.cm.get_cmap(name=colormap))
    nvec = kwargs.pop('nvec', None)

    N = q.shape[0]
    toGlobal = np.arange(N)
    if np.isscalar(nvec):
        if nvec < N:
            I = np.random.choice(N, nvec, replace=False)
            N = nvec
            Q = q[I]
            toGlobal = toGlobal[I]
    W = np.zeros((dim, N))
    for i in np.arange(dim):
        W[i] = V[i][toGlobal]

    if color is not None:
        kwargs['color'] = color
    else:
        if scalars is None:
            scalars = np.sqrt(np.sum(V**2, axis=0))
        if kwargs.get('clim', None) is None:
            vmin = np.min(scalars)
            vmax = np.max(scalars)
            vmin = kwargs.pop('vmin', vmin)
            vmax = kwargs.pop('vmax', vmax)
            kwargs['clim'] = kwargs.pop('clim', [vmin, vmax])

    args = []
    if scalars is not None:
        if dim == 2:
            args.append(scalars[toGlobal])
        if dim == 3:
            cmap = plt.get_cmap()
            # One arrow : 3 lines (shaft,headA and headB) so 3 colors
            # Three arrows with colors value v1,v2 and v3: array is [ v1,v2,v3,v1,v1,v2,v2,v3,v3]!
            #       for N arrows [shaft1,...,shaftN, headA1,headB1,headA2,headB2,...,headAN,headBN]
            C = scalars[toGlobal]  # First all --
            C = np.concatenate((C, np.repeat(C, 2)))
            kwargs['colors'] = cmap(C)
            kwargs['array'] = C  # for colormap
    if dim == 2:
        return plt.quiver(Q[:, 0],
                          Q[:, 1],
                          W[0],
                          W[1],
                          scale=scale,
                          *args,
                          **kwargs)
    if dim == 3:
        fig = plt.gcf()
        if len(fig.axes) > 0:
            ax = fig.axes[0]
        else:
            ax = fig.gca(projection='3d')
        return ax.quiver(Q[:, 0], Q[:, 1], Q[:, 2], W[0] / scale, W[1] / scale,
                         W[2] / scale, **kwargs)
    assert False, "dimension must be 2 or 3 but is %d" % dim