code: def plot_model(
      model: Model,
      filename: Optional[str] = None,
      show: bool = False,
      expand_nested: bool = False,
      prog: str = "dot",
      **dot_kwargs,
  ) -> pydot.Dot:
      """Plot a model to file and/or display it.
  
      This function requires pydot and graphviz. It also requires matplotlib
      to display plots to the screen.
  
      Parameters
      ----------
      model
          The model to plot.
  
      filename
          Filename (optional).
  
      show
          Whether to display the plot in the screen or not. Requires matplotlib.
  
      expand_nested
          Whether to expand any nested models or not (display the nested model as
          a single step).
  
      prog
          Program to use to process the dot file into a graph.
  
      dot_kwargs
          Keyword arguments to pydot.Dot.
  
      Returns
      -------
      dot_graph
          Dot graph of the given model. It can be used to generate an image for plotting.
  
      Examples
      --------
      ::
  
          import matplotlib.pyplot as plt
          import matplotlib.image as mpimg
          from baikal.plot import plot_model
  
          dot_graph = plot_model(model, include_targets=True, expand_nested=False)
          png = dot_graph.create(format="png", prog=prog)
          img = mpimg.imread(io.BytesIO(png))
          plt.imshow(img, aspect="equal")
          plt.axis("off")
          plt.show()
  
      """
  
      dot_transformer = _DotTransformer(expand_nested, **dot_kwargs)
      dot_graph = dot_transformer.transform(model)
  
      # save plot
      if filename:
          basename, ext = os.path.splitext(filename)
          with open(filename, "wb") as fh:
              fh.write(
                  dot_graph.create(
                      format=ext.lstrip(".").lower(), prog=prog
                  )
              )
  
      # display graph via matplotlib
      if show:  # pragma: no cover
          import matplotlib.pyplot as plt
          import matplotlib.image as mpimg
  
          png = dot_graph.create(format="png", prog=prog)
          img = mpimg.imread(io.BytesIO(png))
          plt.imshow(img, aspect="equal")
          plt.axis("off")
          plt.show()
  
      return dot_graph
  
file_path: baikal/baikal/plot.py
imports: import io
  import os
  from typing import Optional
  from baikal import Model
  from baikal._core.utils import make_name, safezip2
label_desc: Use image node_id 9 for calling a global function with example usage: plot_model(model, filename, show, expand_nested, prog) and returns: dot_graph