Learn Pain Less

HomeOur TeamContact
Python
How to Add Correlation Coefficient to Scatter Plot in Python
Pawneshwer Gupta
Pawneshwer Gupta
March 12, 2023
1 min

Table Of Contents

01
Step 1: Import the necessary libraries
02
Step 2: Create the data
03
Step 3: Plot the scatter plot
04
Step 4: Add the correlation coefficient
05
Step 5: Display the plot
How to Add Correlation Coefficient to Scatter Plot in Python

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.

Step 1: Import the necessary libraries

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

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

Step 2: Create the data

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)

Step 3: Plot the scatter plot

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.

Step 4: Add the correlation coefficient

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 specifies the coordinate system.

Step 5: Display the plot

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!

Conclusion

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.

Subscribe to our newsletter!

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

Tags

QuestionHow To

Share


Pawneshwer Gupta

Pawneshwer Gupta

Software Developer

Pawneshwer Gupta works as a software engineer who is enthusiastic in creating efficient and innovative software solutions.

Expertise

Python
Flutter
Laravel
NodeJS

Social Media

Related Posts

How to understand modulus % in python
How to understand modulus % in python
March 28, 2023
1 min
Learn Pain Less  © 2024, All Rights Reserved.
Crafted with by Prolong Services

Quick Links

Advertise with usAbout UsContact Us

Social Media