Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to show different colors for points and line in a Seaborn regplot?
Seaborn's regplot() allows you to customize the appearance of scatter points and regression lines separately. You can specify different colors using the scatter_kws and line_kws parameters.
Basic Example with Different Colors
Here's how to create a regression plot with red points and a green line ?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create sample data
np.random.seed(42) # For reproducible results
df = pd.DataFrame({
"X-Axis": [np.random.randint(1, 6) for i in range(15)],
"Y-Axis": [np.random.randint(1, 6) for i in range(15)]
})
# Create regplot with different colors
sns.regplot(x='X-Axis', y='Y-Axis', data=df,
scatter_kws={"color": "red"},
line_kws={"color": "green"})
plt.show()
Advanced Customization Options
You can further customize the appearance by modifying marker size, line width, and transparency ?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
plt.rcParams["figure.figsize"] = [8, 4]
# Create sample data with more correlation
np.random.seed(42)
x = np.random.randn(50)
y = 2 * x + np.random.randn(50) * 0.5
df = pd.DataFrame({"x": x, "y": y})
# Advanced customization
sns.regplot(x='x', y='y', data=df,
scatter_kws={"color": "blue", "s": 60, "alpha": 0.7},
line_kws={"color": "orange", "linewidth": 3})
plt.title("Custom Colors and Styling")
plt.show()
Available Parameters
| Parameter | Description | Example Values |
|---|---|---|
scatter_kws |
Dictionary of scatter plot properties | {"color": "red", "s": 50, "alpha": 0.8} |
line_kws |
Dictionary of line plot properties | {"color": "blue", "linewidth": 2} |
color |
Sets color for both points and line | "purple", "#FF5733" |
Multiple Styling Options
Here's an example showing various color combinations and styling options ?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
fig, axes = plt.subplots(1, 2, figsize=(12, 4))
# Create sample data
np.random.seed(42)
df = pd.DataFrame({
"x": np.random.randn(30),
"y": np.random.randn(30)
})
# Plot 1: Purple points, yellow line
sns.regplot(x='x', y='y', data=df, ax=axes[0],
scatter_kws={"color": "purple", "s": 40},
line_kws={"color": "gold", "linewidth": 2})
axes[0].set_title("Purple Points, Gold Line")
# Plot 2: Dark points, light line with confidence interval
sns.regplot(x='x', y='y', data=df, ax=axes[1],
scatter_kws={"color": "darkblue", "alpha": 0.6},
line_kws={"color": "lightcoral", "linewidth": 2})
axes[1].set_title("Dark Blue Points, Light Coral Line")
plt.tight_layout()
plt.show()
Conclusion
Use scatter_kws and line_kws parameters in regplot() to customize point and line colors separately. This allows you to create visually distinct regression plots that highlight both the data points and the fitted line effectively.
Advertisements
