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
How to change the default matplotlib plot to seaborn plot?
Changing the default matplotlib plot settings to Seaborn involves modifying the default plot parameters to match the style and aesthetics provided by Seaborn. This can be achieved by adjusting various elements such as the color palette, gridlines, font styles, and plot themes.
Import the Necessary Libraries
To get started, import the required libraries such as matplotlib.pyplot for creating plots and seaborn for applying Seaborn styles and aesthetics ?
import matplotlib.pyplot as plt import seaborn as sns import numpy as np # Sample data for demonstration x = [0, 1, 2, 3, 4] y = [0, 1, 4, 2, 3]
Apply the Seaborn Style
To change the default plot settings to Seaborn, apply the Seaborn style using the set() function. This will modify the default parameters of matplotlib to match the Seaborn style ?
import matplotlib.pyplot as plt
import seaborn as sns
# Apply seaborn default style
sns.set()
# Create a simple plot
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 2, 3]
plt.plot(x, y)
plt.title("Default Seaborn Style")
plt.show()
Customize the Color Palette
Seaborn provides several color palettes that can be used to customize the color scheme of your plots. You can choose from various built-in palettes or create your own. To set a specific color palette, use the set_palette() function ?
import matplotlib.pyplot as plt
import seaborn as sns
# Set color palette
sns.set_palette("deep")
# Create multiple lines to show different colors
x = [0, 1, 2, 3, 4]
y1 = [0, 1, 4, 2, 3]
y2 = [1, 2, 3, 4, 2]
y3 = [2, 0, 1, 3, 4]
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.plot(x, y3, label='Line 3')
plt.legend()
plt.title("Deep Color Palette")
plt.show()
Customize Plot Themes
Seaborn provides different plot themes that can be applied to change the overall appearance of the plot. You can choose from options such as "darkgrid", "whitegrid", "dark", "white", or "ticks". To apply a theme, use the set_style() function ?
import matplotlib.pyplot as plt
import seaborn as sns
# Set theme style
sns.set_style("darkgrid")
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 2, 3]
plt.plot(x, y, marker='o')
plt.title("Darkgrid Theme")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
Customize Gridlines and Axes
Seaborn provides options to customize gridlines and axes. To remove spines (borders), use the despine() function. By default, it removes the top and right spines ?
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")
x = [0, 1, 2, 3, 4]
y = [0, 1, 4, 2, 3]
plt.plot(x, y, marker='o')
plt.title("Plot with Despined Axes")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Remove top and right spines
sns.despine()
plt.show()
Complete Example with All Customizations
Here's a comprehensive example that combines all the customization options ?
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Set seaborn style and theme
sns.set()
sns.set_style("darkgrid")
sns.set_palette("deep")
# Sample data
x = np.linspace(0, 10, 50)
y1 = np.sin(x)
y2 = np.cos(x)
# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(x, y1, label='sin(x)', linewidth=2)
plt.plot(x, y2, label='cos(x)', linewidth=2)
# Customize labels and title
plt.title("Seaborn Styled Plot", fontweight="bold", fontsize=16)
plt.xlabel("X values", fontsize=12)
plt.ylabel("Y values", fontsize=12)
plt.legend(fontsize=11)
# Remove spines
sns.despine()
plt.tight_layout()
plt.show()
Comparison of Styles
| Style | Description | Best For |
|---|---|---|
darkgrid |
Dark grid on white background | Data-heavy plots |
whitegrid |
White grid on gray background | Clean presentations |
dark |
Dark background, no grid | Minimal aesthetic |
white |
White background, no grid | Publications |
ticks |
White background with ticks | Scientific plots |
Conclusion
Use sns.set() to apply default Seaborn styling to matplotlib plots. Combine set_style(), set_palette(), and despine() for complete customization. This approach gives you the aesthetic appeal of Seaborn while maintaining matplotlib's flexibility.
