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
What is the way to convert the figure style to Whitegrid in Seaborn?
Seaborn is a popular data visualization library in Python that provides a variety of styles to enhance the visual appearance of plots. One of the available styles is "whitegrid," which offers a clean white background with subtle grid lines that make data easier to read and interpret.
In this article, we'll explore how to convert the figure style to whitegrid in Seaborn and demonstrate its usage with practical examples.
Setting Up the Environment
First, ensure you have Seaborn installed in your Python environment. You can install it using pip ?
pip install seaborn
Basic Usage of Whitegrid Style
To apply the whitegrid style, use the sns.set_style() function before creating your plots ?
import seaborn as sns
import matplotlib.pyplot as plt
# Set the figure style to whitegrid
sns.set_style("whitegrid")
# Create sample data
categories = ["A", "B", "C", "D"]
values = [10, 20, 15, 25]
# Create a bar plot
sns.barplot(x=categories, y=values)
# Add labels and title
plt.xlabel("Categories")
plt.ylabel("Values")
plt.title("Bar Chart with Whitegrid Style")
# Display the plot
plt.show()
The whitegrid style provides a clean white background with light grid lines that help in reading values accurately without being distracting.
Comparing Different Seaborn Styles
Here's a comparison showing how different styles affect the same plot ?
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 8, 7]
# Create subplots to compare styles
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
styles = ["whitegrid", "darkgrid", "white", "dark"]
for i, style in enumerate(styles):
plt.subplot(2, 2, i+1)
sns.set_style(style)
sns.lineplot(x=x, y=y)
plt.title(f"Style: {style}")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.tight_layout()
plt.show()
Advanced Customization with Whitegrid
You can further customize the whitegrid style by adjusting various parameters ?
import seaborn as sns
import matplotlib.pyplot as plt
# Set whitegrid with custom parameters
sns.set_style("whitegrid", {
"axes.grid": True,
"grid.color": ".8",
"grid.linestyle": "-",
"grid.linewidth": 0.5
})
# Create sample data
months = ["Jan", "Feb", "Mar", "Apr", "May"]
sales = [120, 135, 110, 145, 160]
# Create a more complex plot
plt.figure(figsize=(10, 6))
sns.barplot(x=months, y=sales, palette="viridis")
plt.xlabel("Months", fontsize=12)
plt.ylabel("Sales (in thousands)", fontsize=12)
plt.title("Monthly Sales Report", fontsize=14, fontweight="bold")
# Add value labels on bars
for i, v in enumerate(sales):
plt.text(i, v + 2, str(v), ha='center', va='bottom')
plt.show()
Using Context with Whitegrid
You can temporarily apply the whitegrid style using context managers ?
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 whitegrid style within a context
with sns.axes_style("whitegrid"):
plt.figure(figsize=(8, 5))
sns.histplot(data, bins=20, kde=True)
plt.title("Histogram with KDE - Whitegrid Style")
plt.xlabel("Values")
plt.ylabel("Frequency")
plt.show()
# Style automatically reverts after the context
print("Style reverted to default after context")
Style Comparison Table
| Style | Background | Grid Lines | Best For |
|---|---|---|---|
whitegrid |
White | Light gray | Professional presentations, reports |
darkgrid |
Light gray | White | Dark backgrounds, presentations |
white |
White | None | Minimalist, clean designs |
dark |
Dark gray | None | High contrast, modern look |
Conclusion
The whitegrid style in Seaborn provides an excellent balance between readability and aesthetics, making it ideal for professional data visualizations. Use sns.set_style("whitegrid") to apply this style globally, or use context managers for temporary application.
