How to change the background color of the Seaborn plot

You can change the background color of the Seaborn plot with the following code. The given example helps you to understand how to change the background color of the Seaborn plot. I highly recommend you “Python Crash Course Book” to learn Python.

Example 1: Set Built-in Background Color

# Import the required libraries
import seaborn as sns
import matplotlib.pyplot as plt

# Load the Dataset
dataset = sns.load_dataset("iris")

# Set Background Color 
# Built-in = darkgrid, whitegrid, dark, white, ticks
sns.set_style("dark")

# Plot the Histogram
sns.histplot(x="sepal_length", data=dataset)

# Display the plot
plt.show()

Output:

Example 2: Set your own background colors

# Import the required libraries
import seaborn as sns
import matplotlib.pyplot as plt

# Load the Dataset
dataset = sns.load_dataset("iris")

# Set Background Color 
sns.set(rc={'axes.facecolor':'lightgray', 'figure.facecolor':'orange'})

# Plot the Histogram
sns.histplot(x="sepal_length", data=dataset)

# Display the plot
plt.show()

Output:

Leave a Comment

Your email address will not be published.