Last Updated : 23 Jul, 2025
Matplotlib and Seaborn are two of the most powerful Python libraries for data visualization. While Matplotlib provides a low-level, flexible approach to plotting, Seaborn simplifies the process by offering built-in themes and functions for common plots.
Before diving into plotting, ensure you have both libraries installed:
pip install matplotlib seaborn
After installation, Import them in your script:
Basic plotting with matplotlibimport matplotlib.pyplot as plt
import seaborn as sns
Matplotlib allows you to create simple plots using plt.plot(). Here’s an example of plotting lines and dots:
Python
import matplotlib.pyplot as plt
plt.plot([0, 1], [10, 11], label='Line 1')
plt.plot([0, 1], [11, 10], label='Line 2')
plt.scatter([0, 1], [10.5, 10.5], color='blue', marker='o', label='Dots')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Line and Dot Plot')
plt.legend()
plt.show()
Explanation:
Seaborn makes plotting easier, but it is built on top of Matplotlib, so we can use both together for better results:
Seaborn simplifies data visualization with built-in themes and high-level functions.
Example 1. Applying seaborn style to matplotlib plots
Python
import matplotlib.pyplot as plt
import seaborn as sns
# Apply Seaborn theme
sns.set_theme(style="darkgrid")
# Creating a simple Matplotlib plot
x = [1, 2, 3, 4, 5]
y = [10, 12, 15, 18, 22]
plt.plot(x, y, marker='o', linestyle='-', color='blue', label="Trend")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Matplotlib Plot with Seaborn Theme")
plt.legend()
plt.show()
Output:
Explanation:
Example 2. Customizing a seaborn plot with matplotlib
Python
import matplotlib.pyplot as plt
import seaborn as sns
import panda as pd
data = pd.DataFrame({
'Year': [2018, 2019, 2020, 2021, 2022],
'Sales': [100, 150, 200, 250, 300]
})
plt.figure(figsize=(8, 5))
sns.lineplot(x='Year', y='Sales', data=data, marker='o')
# Customizing using Matplotlib
plt.title("Yearly Sales Growth", fontsize=14, fontweight='bold')
plt.xlabel("Year", fontsize=12)
plt.ylabel("Total Sales", fontsize=12)
plt.xticks(rotation=45)
plt.grid(True, linestyle='--')
plt.show()
Output:
Explanation:
Example 3. Overlaying seaborn and matplotlib plots
Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = np.linspace(0, 10, 20)
y = np.sin(x)
plt.figure(figsize=(8, 5))
# Seaborn Line Plot
sns.lineplot(x=x, y=y, color='blue', label='Sine Wave')
# Matplotlib Scatter Plot
plt.scatter(x, y, color='red', marker='o', label="Data Points")
plt.title("Seaborn Line Plot with Matplotlib Scatter Overlay")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.show()
Output:
Explanation:
Example 4. Enhancing Seaborn Histogram with Matplotlib Annotations
Python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
data = np.random.randn(1000)
plt.figure(figsize=(8, 5))
sns.histplot(data, kde=True, bins=30, color='purple')
# Adding Mean Line using Matplotlib
mean_value = np.mean(data)
plt.axvline(mean_value, color='red', linestyle='dashed', linewidth=2)
plt.text(mean_value + 0.1, 50, f'Mean: {mean_value:.2f}', color='red')
plt.title("Distribution with Seaborn and Matplotlib Customization")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
Output:
Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4