code: def residual_plot(
      data=None,
      x=None,
      y=None,
      *,
      method=_METHOD_DEF,
      deg=_METHOD_LM_DEG_DEF,
      span=_METHOD_LOESS_SPAN_DEF,
      seed=None,
      max_n=None,
      geom=_GEOM_DEF,
      bins=None,
      binwidth=None,
      color=None,
      size=None,
      alpha=None,
      color_by=None,
      show_legend=None,
      hline=_HLINE_DEF,
      marginal=_MARGINAL_DEF,
  ):
      """
      Produce a residual plot that shows the difference between the observed response and the fitted response values.
  
      To use `residual_plot()`, the `numpy` and `pandas` libraries are required.
      Also, `statsmodels` and `scipy` are required for 'lm' and 'loess' methods.
  
      Parameters
      ----------
      data : dict or Pandas or Polars `DataFrame`
          The data to be displayed.
      x : str
          Name of independent variable.
      y : str
          Name of dependent variable that will be fitted.
      method : {'lm', 'loess', 'lowess', 'none'}, default='lm'
          Fitting method: 'lm' (Linear Model) or 'loess'/'lowess' (Locally Estimated Scatterplot Smoothing).
          If value of `deg` parameter is greater than 1 then linear model becomes polynomial of the given degree.
          If method is 'none' then data lives as is.
      deg : int, default=1
          Degree of polynomial for linear regression model.
      span : float, default=0.5
          Only for 'loess' method. The fraction of source points closest to the current point is taken into account
          for computing a least-squares regression. A sensible value is usually 0.25 to 0.5.
      seed : int
          Random seed for 'loess' sampling.
      max_n : int
          Maximum number of data-points for 'loess' method. If this quantity exceeded random sampling is applied to data.
      geom : {'point', 'tile', 'density2d', 'density2df', 'none'}, default='point'
          The geometric object to use to display the data. No object will be used if `geom='none'`.
      bins : int or list of int
          Number of bins in both directions, vertical and horizontal. Overridden by `binwidth`.
          If only one value given - interpret it as list of two equal values.
          Applicable simultaneously for 'tile' geom and 'histogram' marginal.
      binwidth : float or list of float
          The width of the bins in both directions, vertical and horizontal.
          Overrides `bins`. The default is to use bin widths that cover the entire range of the data.
          If only one value given - interpret it as list of two equal values.
          Applicable simultaneously for 'tile' geom and 'histogram' marginal.
      color : str
          Color of the geometry.
      size : float
          Size of the geometry.
      alpha : float
          Transparency level of the geometry. Accept values between 0 and 1.
      color_by : str
          Name of grouping variable.
      show_legend : bool, default=True
          False - do not show legend for the main layer.
      hline : bool, default=True
          False - do not show horizontal line passing through 0.
      marginal : str, default='dens:r'
          Description of marginal layers packed to string value.
          Different marginals are separated by the ',' char.
          Parameters of a marginal are separated by the ':' char.
          First parameter of a marginal is a geometry name.
          Possible values: 'dens'/'density', 'hist'/'histogram', 'box'/'boxplot'.
          Second parameter is a string specifying which sides of the plot the marginal layer will appear on.
          Possible values: 't' (top), 'b' (bottom), 'l' (left), 'r' (right).
          Third parameter (optional) is size of marginal.
          To suppress marginals use `marginal='none'`.
          Examples:
          "hist:tr:0.3",
          "dens:tr,hist:bl",
          "box:tr:.05, hist:bl, dens:bl".
  
      Returns
      -------
      `PlotSpec`
          Plot object specification.
  
      Examples
      --------
      .. jupyter-execute::
          :linenos:
          :emphasize-lines: 11
  
          import numpy as np
          from lets_plot import *
          from lets_plot.bistro.residual import *
          LetsPlot.setup_html()
          n = 100
          np.random.seed(42)
          data = {
              'x': np.random.uniform(size=n),
              'y': np.random.normal(size=n)
          }
          residual_plot(data, 'x', 'y')
  
      |
  
      .. jupyter-execute::
          :linenos:
          :emphasize-lines: 9-11
  
          import numpy as np
          from lets_plot import *
          from lets_plot.bistro.residual import *
          LetsPlot.setup_html()
          n, m = 1000, 5
          np.random.seed(42)
          x = np.random.uniform(low=-m, high=m, size=n)
          y = x**2 + np.random.normal(size=n)
          residual_plot({'x': x, 'y': y}, 'x', 'y', \\
                        deg=2, geom='tile', binwidth=[1, .5], \\
                        hline=False, marginal="hist:tr")
  
      |
  
      .. jupyter-execute::
          :linenos:
          :emphasize-lines: 10-13
  
          import numpy as np
          from lets_plot import *
          from lets_plot.bistro.residual import *
          LetsPlot.setup_html()
          n = 200
          np.random.seed(42)
          x = np.random.uniform(size=n)
          y = x * np.random.normal(size=n)
          g = np.random.choice(['A', 'B'], size=n)
          residual_plot({'x': x, 'y': y, 'g': g}, 'x', 'y', \\
                        method='none', bins=[30, 15], \\
                        size=5, alpha=.5, color_by='g', show_legend=False, \\
                        marginal="hist:t:.2, hist:r, dens:tr, box:bl:.05")
  
      |
  
      .. jupyter-execute::
          :linenos:
          :emphasize-lines: 12
  
          import numpy as np
          from lets_plot import *
          from lets_plot.bistro.residual import *
          LetsPlot.setup_html()
          n = 100
          color, fill = "#bd0026", "#ffffb2"
          np.random.seed(42)
          data = {
              'x': np.random.uniform(size=n),
              'y': np.random.normal(size=n)
          }
          residual_plot(data, 'x', 'y', geom='none', hline=False, marginal='none') + \\
              geom_hline(yintercept=0, size=1, color=color) + \\
              geom_point(shape=21, size=3, color=color, fill=fill) + \\
              ggmarginal('r', layer=geom_area(stat='density', color=color, fill=fill))
  
      """
      # requirements
      if np is None:
          raise ValueError(
              "Module 'numpy' is required for residual plot"
          )
      if pd is None:
          raise ValueError(
              "Module 'pandas' is required for residual plot"
          )
      # prepare data
      stat_data, xs, ys = _get_stat_data(
          data, x, y, color_by, method, deg, span, seed, max_n
      )
      # prepare parameters
      binwidth2d, bins2d = _get_bin_params_2d(xs, ys, binwidth, bins)
      # prepare mapping
      mapping_dict = {"x": x, "y": _RESIDUAL_COL}
      if color_by is not None:
          mapping_dict["color"] = color_by
          mapping_dict["fill"] = color_by
      # prepare scales
      scales = []
      if method == "none":
          scales.append(ylab(y))
      else:
          scales.append(ylab("{0} residual".format(y)))
      # prepare layers
      layers = []
      # main layer
      main_layer = _get_geom2d_layer(
          geom,
          binwidth2d,
          bins2d,
          color,
          color_by,
          size,
          alpha,
          show_legend,
      )
      if main_layer is not None:
          layers.append(main_layer)
      # hline layer
      if hline:
          layers.append(
              geom_hline(
                  yintercept=0,
                  color=_HLINE_COLOR,
                  linetype=_HLINE_LINETYPE,
              )
          )
      # marginal layers
      if marginal != "none":
          layers += _get_marginal_layers(
              marginal, binwidth2d, bins2d, color, color_by, show_legend
          )
      # theme layer
      theme_layer = theme(
          axis="blank",
          axis_text_x=element_text(),
          axis_title_x=element_text(),
          axis_line_y=element_line(),
          axis_ticks_y=element_line(),
          axis_text_y=element_text(),
          axis_title_y=element_text(),
      )
  
      return (
          PlotSpec(
              data=stat_data,
              mapping=aes(**mapping_dict),
              scales=scales,
              layers=layers,
          )
          + theme_layer
      )
  
file_path: lets-plot/python-package/lets_plot/bistro/residual.py
imports: from ..plot.core import PlotSpec, aes
  from ..plot.geom import geom_hline
  from ..plot.label import ylab
  from ..plot.theme_ import *
  from ._plot2d_common import _get_bin_params_2d, _get_geom2d_layer, _get_marginal_layers
label_desc: Use image node_id 7 for calling a global function with example usage: residual_plot(data, x, y) and returns: unknown