def plot(object, *args, **kwargs): """ Plot given object. *Arguments* object a :py:class:`Mesh <dolfin.cpp.Mesh>`, a :py:class:`MeshFunction <dolfin.cpp.MeshFunction>`, a :py:class:`Function <dolfin.functions.function.Function>`, a :py:class:`Expression` <dolfin.cpp.Expression>, a :py:class:`DirichletBC` <dolfin.cpp.DirichletBC> or a :py:class:`FiniteElement <ufl.FiniteElement>`. *Examples of usage* In the simplest case, to plot only e.g. a mesh, simply use .. code-block:: python mesh = UnitSquare(4,4) plot(mesh) Use the ``title`` argument to specify title of the plot .. code-block:: python plot(mesh, tite="Finite element mesh") It is also possible to plot an element .. code-block:: python element = FiniteElement("BDM", tetrahedron, 3) plot(element) Vector valued functions can be visualized with an alternative mode .. code-block:: python plot(u, mode = "glyphs") A more advanced example .. code-block:: python plot(u, wireframe = True, # use wireframe rendering interactive = False, # do not hold plot on screen scalarbar = False, # hide the color mapping bar hardcopy_prefix = "myplot", # default plotfile name scale = 2.0 # scale the warping/glyphs title = "Fancy plot" # Set your own title ) """ mesh = kwargs.get('mesh') p = cpp.Parameters() for key in kwargs: # If there is a "mesh" kwarg it should not be added to the parameters if key != "mesh": try: p.add(key, kwargs[key]) except TypeError: cpp.warning("Incompatible type for keyword argument \"%s\". Ignoring." % key) # Plot element if isinstance(object, ufl.FiniteElementBase): if os.environ.get("DOLFIN_NOPLOT", "0") != "0": return import ffc return ffc.plot(object, *args, **kwargs) if mesh is None and len(args) == 1 and isinstance(args[0], cpp.Mesh): mesh = args[0] # Plot expression if isinstance(object, cpp.Expression): if mesh is None: raise TypeError, "expected a mesh when plotting an expression." return cpp.plot(object, mesh, p) # Try to project if object is not a standard plottable type if not isinstance(object, (cpp.Function, cpp.Expression, cpp.Mesh, cpp.DirichletBC, cpp.MeshFunction, cpp.MeshFunctionBool, cpp.MeshFunctionInt, cpp.MeshFunctionDouble, cpp.MeshFunctionSizet, cpp.DirichletBC, cpp.CSGGeometry)): from dolfin.fem.projection import project try: cpp.info("Object cannot be plotted directly, projecting to"\ " piecewise linears.") object = project(object, mesh=mesh) except Exception as e: msg = ("Don't know how to plot given object:\n %s\n"\ "and projection failed:\n %s") % (str(object), str(e)) #raise RuntimeError(msg) raise plot_object = cpp.plot(object, p) plot_object.write_ps = _VTKPlotter_write_ps # Avoid premature deletion of plotted objects if they go out of scope # before the plot window is closed. The plotter itself is safe, since it's # created in the plot() C++ function, not directly from Python. But the # Python plotter proxy may disappear, so we can't store the references # there. global _objects_referenced_from_plot_windows _objects_referenced_from_plot_windows[plot_object.key()] = (object, mesh, p) return plot_object
def plot(object, *args, **kwargs): """ Plot given object. *Arguments* object a :py:class:`Mesh <dolfin.cpp.Mesh>`, a :py:class:`MeshFunction <dolfin.cpp.MeshFunction>`, a :py:class:`Function <dolfin.functions.function.Function>`, a :py:class:`Expression` <dolfin.cpp.Expression>, a :py:class:`DirichletBC` <dolfin.cpp.DirichletBC> or a :py:class:`FiniteElement <ufl.FiniteElement>`. *Examples of usage* In the simplest case, to plot only e.g. a mesh, simply use .. code-block:: python mesh = UnitSquare(4,4) plot(mesh) Use the ``title`` argument to specify title of the plot .. code-block:: python plot(mesh, tite="Finite element mesh") It is also possible to plot an element .. code-block:: python element = FiniteElement("BDM", tetrahedron, 3) plot(element) Vector valued functions can be visualized with an alternative mode .. code-block:: python plot(u, mode = "glyphs") A more advanced example .. code-block:: python plot(u, wireframe = True, # use wireframe rendering interactive = False, # do not hold plot on screen scalarbar = False, # hide the color mapping bar hardcopy_prefix = "myplot", # default plotfile name scale = 2.0 # scale the warping/glyphs title = "Fancy plot" # Set your own title ) """ # Plot element if isinstance(object, ufl.FiniteElementBase): if os.environ.get("DOLFIN_NOPLOT", "0") != "0": return import ffc return ffc.plot(object, *args, **kwargs) # Get mesh from explicit mesh kwarg, only positional arg, or via object mesh = kwargs.pop('mesh', None) if isinstance(object, cpp.Mesh): if mesh is not None and mesh.id() != object.id(): cpp.dolfin_error("plotting.py", "plot mesh", "Got different mesh in plot object and keyword argument") mesh = object if mesh is None: if isinstance(object, cpp.Function): mesh = object.function_space().mesh() elif hasattr(object, "mesh"): mesh = object.mesh() # Expressions do not carry their own mesh if isinstance(object, cpp.Expression) and mesh is None: cpp.dolfin_error("plotting.py", "plot expression", "Expecting a mesh as keyword argument") # Try to project if object is not a standard plottable type if not isinstance(object, _plottable_types): from dolfin.fem.projection import project try: cpp.info("Object cannot be plotted directly, projecting to"\ " piecewise linears.") object = project(object, mesh=mesh) except Exception as e: msg = "Don't know how to plot given object:\n %s\n" \ "and projection failed:\n %s" % (str(object), str(e)) cpp.dolfin_error("plotting.py", "plot object", msg) # Select backend backend = cpp.parameters["plotting_backend"] if backend == "vtk": return _plot_cpp(object, mesh, kwargs) elif backend == "matplotlib": return _plot_matplotlib(object, mesh, kwargs)
def plot(object, *args, **kwargs): """ Plot given object. *Arguments* object a :py:class:`Mesh <dolfin.cpp.Mesh>`, a :py:class:`MeshFunction <dolfin.cpp.MeshFunction>`, a :py:class:`Function <dolfin.functions.function.Function>`, a :py:class:`Expression` <dolfin.cpp.Expression>, a :py:class:`DirichletBC` <dolfin.cpp.DirichletBC> or a :py:class:`FiniteElement <ufl.FiniteElement>`. *Examples of usage* In the simplest case, to plot only e.g. a mesh, simply use .. code-block:: python mesh = UnitSquare(4,4) plot(mesh) Use the ``title`` argument to specify title of the plot .. code-block:: python plot(mesh, tite="Finite element mesh") It is also possible to plot an element .. code-block:: python element = FiniteElement("BDM", tetrahedron, 3) plot(element) Vector valued functions can be visualized with an alternative mode .. code-block:: python plot(u, mode = "glyphs") A more advanced example .. code-block:: python plot(u, wireframe = True, # use wireframe rendering interactive = False, # do not hold plot on screen scalarbar = False, # hide the color mapping bar hardcopy_prefix = "myplot", # default plotfile name scale = 2.0 # scale the warping/glyphs title = "Fancy plot" # Set your own title ) """ # Plot element if isinstance(object, ufl.FiniteElementBase): if os.environ.get("DOLFIN_NOPLOT", "0") != "0": return import ffc return ffc.plot(object, *args, **kwargs) # Get mesh from explicit mesh kwarg, only positional arg, or via object mesh = kwargs.pop('mesh', None) if isinstance(object, cpp.Mesh): if mesh is not None and mesh.id() != object.id(): cpp.dolfin_error( "plotting.py", "plot mesh", "Got different mesh in plot object and keyword argument") mesh = object if mesh is None: if isinstance(object, cpp.Function): mesh = object.function_space().mesh() elif hasattr(object, "mesh"): mesh = object.mesh() # Expressions do not carry their own mesh if isinstance(object, cpp.Expression) and mesh is None: cpp.dolfin_error("plotting.py", "plot expression", "Expecting a mesh as keyword argument") # Try to project if object is not a standard plottable type if not isinstance(object, _plottable_types): from dolfin.fem.projection import project try: cpp.info("Object cannot be plotted directly, projecting to"\ " piecewise linears.") object = project(object, mesh=mesh) except Exception as e: msg = "Don't know how to plot given object:\n %s\n" \ "and projection failed:\n %s" % (str(object), str(e)) cpp.dolfin_error("plotting.py", "plot object", msg) # Select backend backend = cpp.parameters["plotting_backend"] if backend == "vtk": return _plot_cpp(object, mesh, kwargs) elif backend == "matplotlib": return _plot_matplotlib(object, mesh, kwargs)
def plot(object, *args, **kwargs): """ Plot given object. *Arguments* object a :py:class:`Mesh <dolfin.cpp.Mesh>`, a :py:class:`MeshFunction <dolfin.cpp.MeshFunction>`, a :py:class:`Function <dolfin.functions.function.Function>`, a :py:class:`Expression` <dolfin.cpp.Expression>, a :py:class:`DirichletBC` <dolfin.cpp.DirichletBC>, a :py:class:`FiniteElement <ufl.FiniteElement>`. *Examples of usage* In the simplest case, to plot only e.g. a mesh, simply use .. code-block:: python mesh = UnitSquare(4, 4) plot(mesh) Use the ``title`` argument to specify title of the plot .. code-block:: python plot(mesh, tite="Finite element mesh") It is also possible to plot an element .. code-block:: python element = FiniteElement("BDM", tetrahedron, 3) plot(element) Vector valued functions can be visualized with an alternative mode .. code-block:: python plot(u, mode = "glyphs") A more advanced example .. code-block:: python plot(u, wireframe = True, # use wireframe rendering interactive = False, # do not hold plot on screen scalarbar = False, # hide the color mapping bar hardcopy_prefix = "myplot", # default plotfile name scale = 2.0, # scale the warping/glyphs title = "Fancy plot", # set your own title ) """ # Return if plotting is disables if os.environ.get("DOLFIN_NOPLOT", "0") != "0": return # Return if Matplotlib is not available if not _has_matplotlib(): cpp.log.info("Matplotlib is required to plot from Python.") return # Plot element if isinstance(object, ufl.FiniteElementBase): import ffc return ffc.plot(object, *args, **kwargs) # For dolfin.function.Function, extract cpp_object if hasattr(object, "cpp_object"): object = object.cpp_object() # Get mesh from explicit mesh kwarg, only positional arg, or via # object mesh = kwargs.pop('mesh', None) if isinstance(object, cpp.mesh.Mesh): if mesh is not None and mesh.id() != object.id(): raise RuntimeError( "Got different mesh in plot object and keyword argument") mesh = object if mesh is None: if isinstance(object, cpp.function.Function): mesh = object.function_space().mesh() elif hasattr(object, "mesh"): mesh = object.mesh() # Expressions do not carry their own mesh if isinstance(object, cpp.function.Expression) and mesh is None: raise RuntimeError("Expecting a mesh as keyword argument") backend = kwargs.pop("backend", "matplotlib") if backend not in ("matplotlib", "x3dom"): raise RuntimeError("Plotting backend %s not recognised" % backend) # Try to project if object is not a standard plottable type if not isinstance(object, _all_plottable_types): from dolfin.fem.projection import project try: cpp.log.info("Object cannot be plotted directly, projecting to " "piecewise linears.") object = project(object, mesh=mesh) mesh = object.function_space().mesh() object = object._cpp_object except Exception as e: msg = "Don't know how to plot given object:\n %s\n" \ "and projection failed:\n %s" % (str(object), str(e)) raise RuntimeError(msg) # Plot if backend == "matplotlib": return _plot_matplotlib(object, mesh, kwargs) elif backend == "x3dom": return _plot_x3dom(object, kwargs) else: assert False, "This code should not be reached."
def plot(object, *args, **kwargs): """ Plot given object. *Arguments* object a :py:class:`Mesh <dolfin.cpp.Mesh>`, a :py:class:`MeshFunction <dolfin.cpp.MeshFunction>`, a :py:class:`Function <dolfin.functions.function.Function>`, a :py:class:`Expression` <dolfin.cpp.Expression>, a :py:class:`DirichletBC` <dolfin.cpp.DirichletBC>, a :py:class:`FiniteElement <ufl.FiniteElement>`. *Examples of usage* In the simplest case, to plot only e.g. a mesh, simply use .. code-block:: python mesh = UnitSquare(4, 4) plot(mesh) Use the ``title`` argument to specify title of the plot .. code-block:: python plot(mesh, tite="Finite element mesh") It is also possible to plot an element .. code-block:: python element = FiniteElement("BDM", tetrahedron, 3) plot(element) Vector valued functions can be visualized with an alternative mode .. code-block:: python plot(u, mode = "glyphs") A more advanced example .. code-block:: python plot(u, wireframe = True, # use wireframe rendering interactive = False, # do not hold plot on screen scalarbar = False, # hide the color mapping bar hardcopy_prefix = "myplot", # default plotfile name scale = 2.0, # scale the warping/glyphs title = "Fancy plot", # set your own title ) """ # Return if plotting is disables if os.environ.get("DOLFIN_NOPLOT", "0") != "0": return # Return if Matplotlib is not available if not _has_matplotlib(): cpp.log.info("Matplotlib is required to plot from Python.") return # Plot element if isinstance(object, ufl.FiniteElementBase): import ffc return ffc.plot(object, *args, **kwargs) # For dolfin.function.Function, extract cpp_object if hasattr(object, "cpp_object"): object = object.cpp_object() # Get mesh from explicit mesh kwarg, only positional arg, or via # object mesh = kwargs.pop('mesh', None) if isinstance(object, cpp.mesh.Mesh): if mesh is not None and mesh.id() != object.id(): raise RuntimeError("Got different mesh in plot object and keyword argument") mesh = object if mesh is None: if isinstance(object, cpp.function.Function): mesh = object.function_space().mesh() elif hasattr(object, "mesh"): mesh = object.mesh() # Expressions do not carry their own mesh if isinstance(object, cpp.function.Expression) and mesh is None: raise RuntimeError("Expecting a mesh as keyword argument") backend = kwargs.pop("backend", "matplotlib") if backend not in ("matplotlib", "x3dom"): raise RuntimeError("Plotting backend %s not recognised" % backend) # Try to project if object is not a standard plottable type if not isinstance(object, _all_plottable_types): from dolfin.fem.projection import project try: cpp.log.info("Object cannot be plotted directly, projecting to " "piecewise linears.") object = project(object, mesh=mesh) mesh = object.function_space().mesh() object = object._cpp_object except Exception as e: msg = "Don't know how to plot given object:\n %s\n" \ "and projection failed:\n %s" % (str(object), str(e)) raise RuntimeError(msg) # Plot if backend == "matplotlib": return _plot_matplotlib(object, mesh, kwargs) elif backend == "x3dom": return _plot_x3dom(object, kwargs) else: assert False, "This code should not be reached."
def plot(object, *args, **kwargs): """ Plot given object. *Arguments* object a :py:class:`Mesh <dolfin.cpp.Mesh>`, a :py:class:`MeshFunction <dolfin.cpp.MeshFunction>`, a :py:class:`Function <dolfin.functions.function.Function>`, a :py:class:`Expression` <dolfin.cpp.Expression>, a :py:class:`DirichletBC` <dolfin.cpp.DirichletBC> or a :py:class:`FiniteElement <ufl.FiniteElement>`. *Examples of usage* In the simplest case, to plot only e.g. a mesh, simply use .. code-block:: python mesh = UnitSquare(4,4) plot(mesh) Use the ``title`` argument to specify title of the plot .. code-block:: python plot(mesh, tite="Finite element mesh") It is also possible to plot an element .. code-block:: python element = FiniteElement("BDM", tetrahedron, 3) plot(element) Vector valued functions can be visualized with an alternative mode .. code-block:: python plot(u, mode = "glyphs") A more advanced example .. code-block:: python plot(u, wireframe = True, # use wireframe rendering interactive = False, # do not hold plot on screen scalarbar = False, # hide the color mapping bar hardcopy_prefix = "myplot", # default plotfile name scale = 2.0 # scale the warping/glyphs title = "Fancy plot" # Set your own title ) """ mesh = kwargs.get('mesh') p = cpp.Parameters() for key in kwargs: # If there is a "mesh" kwarg it should not be added to the parameters if key != "mesh": try: p.add(key, kwargs[key]) except TypeError: cpp.warning( "Incompatible type for keyword argument \"%s\". Ignoring." % key) # Plot element if isinstance(object, ufl.FiniteElementBase): if os.environ.get("DOLFIN_NOPLOT", "0") != "0": return import ffc return ffc.plot(object, *args, **kwargs) if mesh is None and len(args) == 1 and isinstance(args[0], cpp.Mesh): mesh = args[0] # Plot expression if isinstance(object, cpp.Expression): if mesh is None: raise TypeError, "expected a mesh when plotting an expression." return cpp.plot(object, mesh, p) # Try to project if object is not a standard plottable type if not isinstance( object, (cpp.Function, cpp.Expression, cpp.Mesh, cpp.DirichletBC, cpp.MeshFunction, cpp.MeshFunctionBool, cpp.MeshFunctionInt, cpp.MeshFunctionDouble, cpp.MeshFunctionSizet, cpp.DirichletBC, cpp.CSGGeometry)): from dolfin.fem.projection import project try: cpp.info("Object cannot be plotted directly, projecting to"\ " piecewise linears.") object = project(object, mesh=mesh) except Exception as e: msg = ("Don't know how to plot given object:\n %s\n"\ "and projection failed:\n %s") % (str(object), str(e)) #raise RuntimeError(msg) raise plot_object = cpp.plot(object, p) plot_object.write_ps = _VTKPlotter_write_ps # Avoid premature deletion of plotted objects if they go out of scope # before the plot window is closed. The plotter itself is safe, since it's # created in the plot() C++ function, not directly from Python. But the # Python plotter proxy may disappear, so we can't store the references # there. global _objects_referenced_from_plot_windows _objects_referenced_from_plot_windows[plot_object.key()] = (object, mesh, p) return plot_object