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
Changing the color and marker of each point using Seaborn jointplot
Seaborn's jointplot creates scatter plots with marginal distributions. To customize individual point colors and markers, we need to clear the default plot and manually add points with different styling.
Basic Jointplot Structure
First, let's create a basic jointplot to understand the structure ?
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Set figure parameters
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Load sample data
tips = sns.load_dataset("tips")
print(f"Dataset shape: {tips.shape}")
print(tips.head(3))
Dataset shape: (244, 7) total_bill tip sex smoker day time size 0 16.99 1.01 Female No Sun Dinner 2 1 10.34 1.66 Male No Sun Dinner 3 2 21.01 3.50 Male No Sun Dinner 3
Creating Custom Colored Points
We'll create a jointplot and then replace the default scatter points with custom colored markers ?
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
plt.rcParams["figure.figsize"] = [8.00, 6.00]
plt.rcParams["figure.autolayout"] = True
# Load data
tips = sns.load_dataset("tips")
# Create jointplot framework
g = sns.jointplot(x="total_bill", y="tip", data=tips, height=5)
# Clear the joint axes to remove default points
g.ax_joint.cla()
# Create random colors for each point
colors = np.random.random((len(tips), 3))
# Create marker list (cycling through different markers)
markers = ['o', 'v', '^', '<', '>', 's', 'p', '*', 'h', 'H', 'D', 'd']
marker_cycle = [markers[i % len(markers)] for i in range(len(tips))]
# Plot each point with custom color and marker
for i, (_, row) in enumerate(tips.iterrows()):
g.ax_joint.plot(row['total_bill'], row['tip'],
color=colors[i], marker=marker_cycle[i],
markersize=6, alpha=0.7)
# Set axis labels
g.set_axis_labels('Total Bill ($)', 'Tip ($)', fontsize=12)
plt.show()
Using Categorical Colors
For better visualization, we can color points based on categorical data ?
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
plt.rcParams["figure.figsize"] = [8.00, 6.00]
# Load data
tips = sns.load_dataset("tips")
# Create jointplot
g = sns.jointplot(x="total_bill", y="tip", data=tips, height=5)
g.ax_joint.cla()
# Define colors for different categories
color_map = {'Dinner': 'red', 'Lunch': 'blue'}
marker_map = {'Male': 'o', 'Female': '^'}
# Plot points with category-based styling
for i, (_, row) in enumerate(tips.iterrows()):
color = color_map[row['time']]
marker = marker_map[row['sex']]
g.ax_joint.plot(row['total_bill'], row['tip'],
color=color, marker=marker,
markersize=6, alpha=0.7)
g.set_axis_labels('Total Bill ($)', 'Tip ($)', fontsize=12)
# Add legend
import matplotlib.patches as patches
legend_elements = [
patches.Patch(color='red', label='Dinner'),
patches.Patch(color='blue', label='Lunch')
]
g.ax_joint.legend(handles=legend_elements, loc='upper left')
plt.show()
Key Parameters
| Parameter | Purpose | Example Values |
|---|---|---|
color |
Point color | RGB tuple, hex, name |
marker |
Point shape | 'o', 'v', '^', 's', '*' |
markersize |
Point size | 6, 8, 10 |
alpha |
Transparency | 0.5, 0.7, 1.0 |
Conclusion
Use ax_joint.cla() to clear default points, then manually plot each point with custom colors and markers. This approach provides complete control over individual point styling in Seaborn jointplots.
Advertisements
