
04/11/2024
Creating a scatter plot with a regression line using Seaborn is a straightforward process. Below, I’ll walk you through the steps to achieve this, assuming you have some familiarity with Python and the basics of data visualization.
First, ensure you have Seaborn and Matplotlib installed. If you haven’t installed them yet, you can do so using pip:
pip install seaborn matplotlib
Now, import the necessary libraries in your Python script or Jupyter Notebook:
import seaborn as sns import matplotlib.pyplot as plt import pandas as pd
You need some sample data to work with. For this example, let’s create a simple DataFrame using pandas:
# Sample Data data = { 'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'y': [2.3, 2.5, 3.1, 4.0, 4.2, 5.5, 6.8, 7.5, 8.2, 10.1] } df = pd.DataFrame(data)
Next, let’s create a basic scatter plot to visualize the relationship between the x and y values:
plt.figure(figsize=(10, 6)) sns.scatterplot(data=df, x='x', y='y') plt.title('Scatter Plot of X vs Y') plt.xlabel('X-axis Label') plt.ylabel('Y-axis Label') plt.show()
This will give you a scatter plot without any regression line.
To add a regression line to your scatter plot, you can use the sns.regplot function, which combines scatter plotting with a linear regression model fit. Here’s how you can do it:
plt.figure(figsize=(10, 6)) sns.regplot(data=df, x='x', y='y', marker='o', color='blue') plt.title('Scatter Plot with Regression Line') plt.xlabel('X-axis Label') plt.ylabel('Y-axis Label') plt.show()
Confidence Interval: By default, sns.regplot includes a shaded area representing the confidence interval for the regression line. You can control this with the ci parameter, setting it to None if you want to remove it:
sns.regplot(data=df, x='x', y='y', ci=None)
Polynomial Regression: If you're interested in fitting a polynomial rather than a linear regression, you can do it by specifying the order parameter:
sns.regplot(data=df, x='x', y='y', order=2)
You can further customize your plot by adjusting the size, labels, and adding grid lines or additional elements to enhance readability.
plt.figure(figsize=(10, 6)) sns.regplot(data=df, x='x', y='y', marker='o', color='blue') plt.title('Scatter Plot with Regression Line', fontsize=16) plt.xlabel('Independent Variable', fontsize=12) plt.ylabel('Dependent Variable', fontsize=12) plt.grid(True) # Adds grid lines plt.show()
By following these steps, you can easily add a regression line to your scatter plot using Seaborn, making your data visualizations both informative and visually appealing!
03/11/2024 | Python
03/11/2024 | Python
04/11/2024 | Python
04/11/2024 | Python
04/11/2024 | Python
04/11/2024 | Python
04/11/2024 | Python