.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/statistical_plots/scatter_with_regression.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_statistical_plots_scatter_with_regression.py: Scatter with Linear Regression Fit ================================== Plot points and automatically add a regression line with R², slope, intercept. .. GENERATED FROM PYTHON SOURCE LINES 7-38 .. image-sg:: /examples/statistical_plots/images/sphx_glr_scatter_with_regression_001.png :alt: Scatter with regression fit :srcset: /examples/statistical_plots/images/sphx_glr_scatter_with_regression_001.png :class: sphx-glr-single-img .. code-block:: Python import numpy as np import matplotlib.pyplot as plt from emcpy.plots.plots import Scatter from emcpy.plots.create_plots import CreatePlot, CreateFigure rng = np.random.default_rng(3) x = rng.normal(0, 1, 400) y = 0.6 * x + rng.normal(0, 0.8, 400) p = CreatePlot() sc = Scatter(x, y) sc.markersize = 15 sc.alpha = 0.6 sc.label = "observations" sc.do_linear_regression = True sc.linear_regression = {"linewidth": 2, "color": "tab:orange"} # style for the fit line p.plot_layers = [sc] p.add_title("Scatter with regression fit") p.add_xlabel("x") p.add_ylabel("y") p.add_grid() p.add_legend(loc="upper left", frameon=False) fig = CreateFigure(nrows=1, ncols=1, figsize=(7.5, 4)) fig.plot_list = [p] fig.create_figure() fig.tight_layout() plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.090 seconds) .. _sphx_glr_download_examples_statistical_plots_scatter_with_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: scatter_with_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: scatter_with_regression.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: scatter_with_regression.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_