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 figure style to Darkgrid in Seaborn?
Seaborn provides several built-in figure styles that we can choose from to enhance the visual appearance of our plots. These styles affect various elements such as colors, grid lines, background, and fonts. To set the figure style in Seaborn, we can use the sns.set_style() function.
Available Seaborn Styles
The following are the available figure styles in Seaborn library:
Darkgrid This style features a dark gray background with grid lines, which helps in focusing attention on the data points.
Whitegrid This style is similar to "darkgrid" but with a white background, making it suitable for plots with lighter color schemes.
Dark This style has a dark background without grid lines, giving a clean and minimalistic look to the plots.
White This style is similar to "dark" but with a white background, which is useful for plots with lighter colors.
Ticks This style removes the background grid and only shows the tick marks on the axes.
Setting Darkgrid Style
To change the figure style to "darkgrid" in Seaborn, use the sns.set_style() function ?
import seaborn as sns
import matplotlib.pyplot as plt
# Set the figure style to darkgrid
sns.set_style("darkgrid")
# Create sample data
x = ["A", "B", "C", "D"]
y = [10, 20, 15, 25]
# Create a bar plot
sns.barplot(x=x, y=y)
# Set labels and title
plt.xlabel("Categories")
plt.ylabel("Values")
plt.title("Bar Chart with Darkgrid Style")
# Display the plot
plt.show()
Comparing Styles
Here's how different styles look when applied to the same plot ?
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data
x = ["A", "B", "C", "D"]
y = [10, 20, 15, 25]
# Create subplots for comparison
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
styles = ["darkgrid", "whitegrid", "dark", "white"]
for i, style in enumerate(styles):
row = i // 2
col = i % 2
# Set style for current subplot
sns.set_style(style)
# Create bar plot
sns.barplot(x=x, y=y, ax=axes[row, col])
axes[row, col].set_title(f"Style: {style}")
axes[row, col].set_xlabel("Categories")
axes[row, col].set_ylabel("Values")
plt.tight_layout()
plt.show()
Advanced Darkgrid Customization
You can further customize the darkgrid style with additional parameters ?
import seaborn as sns
import matplotlib.pyplot as plt
# Set darkgrid with custom parameters
sns.set_style("darkgrid", {
"axes.facecolor": "#2d2d2d",
"grid.color": "#404040",
"grid.linewidth": 0.8
})
# Create sample data
x = ["Python", "Java", "C++", "JavaScript"]
y = [85, 70, 65, 80]
# Create enhanced bar plot
plt.figure(figsize=(10, 6))
sns.barplot(x=x, y=y, palette="viridis")
plt.xlabel("Programming Languages", fontsize=12)
plt.ylabel("Popularity Score", fontsize=12)
plt.title("Programming Language Popularity with Custom Darkgrid", fontsize=14)
plt.show()
Style Context Manager
You can temporarily apply darkgrid style using a context manager ?
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
# Generate sample data
np.random.seed(42)
data = np.random.randn(100)
# Use darkgrid temporarily
with sns.axes_style("darkgrid"):
plt.figure(figsize=(8, 5))
sns.histplot(data, kde=True)
plt.title("Histogram with Darkgrid Style")
plt.xlabel("Values")
plt.ylabel("Frequency")
plt.show()
# Style reverts to default after the context
print("Context manager completed - style reverted")
Style Comparison Table
| Style | Background | Grid Lines | Best For |
|---|---|---|---|
| darkgrid | Dark gray | Yes | Data-focused plots, presentations |
| whitegrid | White | Yes | Light color schemes, reports |
| dark | Dark gray | No | Minimalistic, clean plots |
| white | White | No | Publications, academic papers |
Conclusion
The darkgrid style in Seaborn provides an excellent dark background with grid lines that help emphasize data points. Use sns.set_style("darkgrid") to apply this style globally, or use it within a context manager for temporary application.
