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
Best way to display Seaborn/Matplotlib plots with a dark iPython Notebook profile
To display Seaborn/Matplotlib plots with a dark background in iPython Notebook, we can use sns.set_style("dark") method. This creates an aesthetic dark theme that's easier on the eyes during extended coding sessions.
Setting Up Dark Style
The set_style() method controls the visual appearance of plots. The "dark" style provides a dark background with light grid lines ?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Set figure parameters
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Apply dark style
sns.set_style("dark")
print("Dark style applied successfully!")
Dark style applied successfully!
Creating a Dark-Themed Plot
Let's create a bar plot with the dark style applied ?
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Configure plot settings
plt.rcParams["figure.figsize"] = [8, 4]
plt.rcParams["figure.autolayout"] = True
# Set dark style
sns.set_style("dark")
# Create sample data
np.random.seed(42) # For reproducible results
data = pd.DataFrame({
"Category": [f"Item_{i}" for i in range(8)],
"Values": np.random.randint(5, 25, 8)
})
# Create bar plot
plt.figure(figsize=(8, 4))
bar_plot = sns.barplot(x='Category', y='Values', data=data, palette='viridis')
plt.xticks(rotation=45)
plt.title("Sample Bar Plot with Dark Theme")
plt.tight_layout()
plt.show()
Available Dark Styles
Seaborn provides several style options for dark themes ?
import seaborn as sns
# Available styles
styles = ["darkgrid", "dark", "whitegrid", "white", "ticks"]
print("Available Seaborn styles:")
for style in styles:
print(f"- {style}")
# Dark-specific styles
print("\nDark theme options:")
print("- 'dark': Dark background with no grid")
print("- 'darkgrid': Dark background with white grid lines")
Available Seaborn styles: - darkgrid - dark - whitegrid - white - ticks Dark theme options: - 'dark': Dark background with no grid - 'darkgrid': Dark background with white grid lines
Comparison of Dark Styles
| Style | Background | Grid Lines | Best For |
|---|---|---|---|
dark |
Dark gray | None | Clean, minimal plots |
darkgrid |
Dark gray | White grid | Easier data reading |
Additional Dark Theme Customization
You can further customize the dark appearance using context and palette settings ?
import seaborn as sns
import matplotlib.pyplot as plt
# Set comprehensive dark theme
sns.set_theme(style="dark", context="notebook", palette="dark")
# Verify current style
print(f"Current style: {sns.axes_style()['axes.facecolor']}")
print("Dark theme configuration applied!")
Current style: #EAEAF2 Dark theme configuration applied!
Conclusion
Use sns.set_style("dark") for clean dark backgrounds or sns.set_style("darkgrid") for dark themes with grid lines. This creates eye-friendly visualizations perfect for iPython Notebook environments during extended data analysis sessions.
