As a business owner or data analyst, you may need to analyze the correlation between two variables. The correlation coefficient is a statistical measure that helps you determine the degree of correlation between two variables. In Python, you can easily plot a scatter plot to visualize the relationship between two variables. However, you may also need to add the correlation coefficient to the plot to get a better understanding of the relationship. In this article, we will show you **how to add a correlation coefficient to a scatter plot in Python**.

To plot the scatter plot and add the correlation coefficient, you will need to import the following libraries:

import matplotlib.pyplot as pltimport numpy as npimport scipy.stats as stats

Next, you will need to create the data that you want to plot. In this example, we will create two arrays of random data using the NumPy library.

x = np.random.randn(100)y = np.random.randn(100)

Now, you can plot the scatter plot using the following code:

plt.scatter(x, y)plt.title('Scatter plot with correlation coefficient')plt.xlabel('X')plt.ylabel('Y')

This will create a basic scatter plot that shows the relationship between the two variables.

To add the correlation coefficient to the scatter plot, you will need to use the ** stats.pearsonr** function from the SciPy library. This function calculates the Pearson correlation coefficient and the p-value for testing non-correlation.

r, p = stats.pearsonr(x, y)plt.annotate('r = {:.2f}'.format(r), xy=(0.7, 0.9), xycoords='axes fraction')

This code will add the correlation coefficient (r) to the scatter plot in the top right corner. The ** xy** the argument specifies the position of the text, and

`xycoords`

Finally, you can display the plot using the ** plt.show()** function.

plt.show()

Congratulations, you have now successfully plotted a scatter plot with the correlation coefficient in Python!

In conclusion, adding the correlation coefficient to a scatter plot in Python is an essential step in data analysis. By visualizing the relationship between two variables and adding the correlation coefficient, you can gain valuable insights into your data. We hope this article has been helpful in showing you how to add the correlation coefficient to a scatter plot in Python.

We'll send you the best of our blog just once a month. We promise.

Learn Pain Less © 2023, All Rights Reserved.

Crafted with by Prolong Services

Quick Links

Legal Stuff

Social Media