.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/scatter_plots/scatter_with_regression_line.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_scatter_plots_scatter_with_regression_line.py: Creating a Scatter Plot with a Regression Line ---------------------------------------------- The following is an example of how to plot data as a scatter plot and include a linear regression line. Calling the linear regression function will give the user the y=mx+b equation as well as the R-squared value if the user specifies a legend. .. GENERATED FROM PYTHON SOURCE LINES 11-50 .. image-sg:: /examples/scatter_plots/images/sphx_glr_scatter_with_regression_line_001.png :alt: Test Scatter Plot :srcset: /examples/scatter_plots/images/sphx_glr_scatter_with_regression_line_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 from emcpy.stats import get_linear_regression def main(): # Create test data rng = np.random.RandomState(0) x = rng.randn(100) y = rng.randn(100) # Create Scatter object sctr1 = Scatter(x, y) # Add linear regression feature in scatter object sctr1.do_linear_regression = True sctr1.add_linear_regression() # Create plot object and add features plot1 = CreatePlot() plot1.plot_layers = [sctr1] plot1.add_title(label='Test Scatter Plot') plot1.add_xlabel(xlabel='X Axis Label') plot1.add_ylabel(ylabel='Y Axis Label') plot1.add_legend() # Create figure fig = CreateFigure() fig.plot_list = [plot1] fig.create_figure() plt.show() if __name__ == '__main__': main() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.068 seconds) .. _sphx_glr_download_examples_scatter_plots_scatter_with_regression_line.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_line.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: scatter_with_regression_line.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_