How to change the figure style to Dark in Seaborn?


In Seaborn, the figure style refers to the overall visual appearance and aesthetics of a plot. Seaborn provides several built-in figure styles that can be used to enhance the look and feel of your visualizations. These figure styles affect various elements such as colors, grid lines, background, fonts, and more.

To set the figure style in Seaborn, we can use the sns.set_style() function. In this article we are going to see how to change the figure style to Dark in Seaborn. The following are the steps to be followed.

Installing Seaborn Library

First we have to ensure that Seaborn is installed in our Python environment. We can install it using the pip package manager by running the following command in your terminal or command prompt.

pip install seaborn

Import the necessary libraries

Next we have to import the required libraries such as Seaborn and Matplotlib.pyplot in our Python script or Jupyter Notebook. These libraries provide the functionality to create and customize plots.

import seaborn as sns
import matplotlib.pyplot as plt

Set the figure style

Next we have to change the figure style to "dark" in Seaborn, use the sns.set_style() function. This function allows us to modify the default styles of Matplotlib to match the chosen Seaborn style.

sns.set_style("dark")

Create and customize our plots

Once the figure style is set, we can proceed to create and customize our plots using Seaborn and Matplotlib.

Example

In this example, sns.scatterplot() creates a scatter plot using Seaborn. The subsequent lines set the x-label, y-label, and title using Matplotlib. Finally, plt.show() displays the plot.

import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style("dark")
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a scatter plot
sns.scatterplot(x=x, y=y)
# Set labels and title
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Scatter Plot")
# Display the plot
plt.show()

Output

In the output we can observe that the figure style was in the dark color, as we set it to dark by using the .set_style() function.

Further customization

We can further customize our plots using various Seaborn functions and Matplotlib options. For example, we can modify the color palette, font size, marker styles, etc.

The below lines demonstrate some additional customizations. sns.set_palette() changes the color palette to "dark". plt.xticks() and plt.yticks() set the font size for x-axis and y-axis ticks, respectively.

Example

import seaborn as sns
import matplotlib.pyplot as plt
sns.set_style("dark")
# Sample data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create a scatter plot
sns.scatterplot(x=x, y=y)
# Set labels and title
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Scatter Plot")
sns.set_palette("Oranges")  # Change the color palette
plt.xticks(fontsize=12)  # Set x-axis tick font size
plt.yticks(fontsize=12)
# Display the plot
plt.show()

Output

Updated on: 02-Aug-2023

257 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements