How to Create Stunning Visuals with Matplotlib and Seaborn
Creating stunning visuals with Matplotlib and Seaborn involves more than just calling plotting functions — it’s about combining data understanding, design principles, and customization. Here's a guide to help you make high-quality, visually appealing plots:
π¨ How to Create Stunning Visuals with Matplotlib and Seaborn
π¦ 1. Import the Right Libraries
python
Copy
Edit
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
π ️ 2. Set a Style and Theme
Both libraries allow style settings for a consistent and professional look.
python
Copy
Edit
sns.set_theme(style="whitegrid") # or 'darkgrid', 'white', 'ticks'
plt.style.use('seaborn-vibrant') # Matplotlib styles
π 3. Use High-Quality Data
Beautiful visuals require good data. Clean your data using pandas.
python
Copy
Edit
df = pd.read_csv("your_data.csv")
df.dropna(inplace=True) # Clean missing values
π 4. Choose the Right Plot Type
Use Seaborn for statistical plots and Matplotlib for more control.
Goal Recommended Plot
Distribution sns.histplot, sns.kdeplot
Categories vs Values sns.barplot, sns.boxplot
Relationships sns.scatterplot, sns.lineplot
Time Series plt.plot or sns.lineplot
Heatmaps sns.heatmap
Example:
python
Copy
Edit
sns.scatterplot(data=df, x="age", y="income", hue="gender", palette="coolwarm")
π¨ 5. Customize Colors and Palettes
Color tells a story. Seaborn has built-in palettes:
python
Copy
Edit
sns.set_palette("Set2")
Or use custom palettes:
python
Copy
Edit
custom_palette = ["#69b3a2", "#4374B3", "#C92A2A"]
sns.set_palette(custom_palette)
π️ 6. Label Clearly and Use Titles
python
Copy
Edit
plt.title("Income by Age and Gender", fontsize=16, fontweight='bold')
plt.xlabel("Age", fontsize=12)
plt.ylabel("Income", fontsize=12)
plt.legend(title="Gender")
π 7. Use Figure Size and Layout
Make your plots readable.
python
Copy
Edit
plt.figure(figsize=(10, 6))
Use tight_layout() to prevent label overlap:
python
Copy
Edit
plt.tight_layout()
π 8. Add Annotations
Highlight key insights.
python
Copy
Edit
plt.annotate("Peak income", xy=(45, 90000), xytext=(50, 100000),
arrowprops=dict(facecolor='black', shrink=0.05))
πΎ 9. Save Your Plot
Save as high-res image for reports or presentations.
python
Copy
Edit
plt.savefig("plot.png", dpi=300, bbox_inches='tight')
π§ 10. Use Advanced Seaborn Features
FacetGrid for small multiples:
python
Copy
Edit
g = sns.FacetGrid(df, col="gender", height=5)
g.map(sns.histplot, "income")
PairPlot for multivariate relationships:
python
Copy
Edit
sns.pairplot(df, hue="gender")
✅ Final Tip: Less is More
Avoid chartjunk (clutter).
Use gridlines subtly.
Choose fonts and colors for readability and accessibility.
Learn Data Science Course in Hyderabad
Read More
Data Visualization Best Practices for Beginners
Exploratory Data Analysis (EDA): A Step-by-Step Guide
Data Wrangling Techniques Every Data Scientist Should Know
How to Handle Missing Data in Data Science
Visit Our Quality Thought Training Institute in Hyderabad
Comments
Post a Comment