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. 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.

In this article we are going to see how to change the figure style to Dark Grid in seaborn. To change the figure style to "darkgrid" in Seaborn, we can follow below steps.

Install Seaborn

We should make sure we have Seaborn installed in our Python environment. we can install it using pip command in the python working environment.

pip install seaborn

Import the necessary libraries

In our Python script or Jupyter Notebook, next we have to import the required libraries such as Seaborn and Matplotlib.pyplot.

import seaborn as sns
import matplotlib.pyplot as plt

Set the figure style

Use the `sns.set_style()` function to set the figure style to "darkgrid". This function modifies the default styles of Matplotlib.

sns.set_style("darkgrid")

This line of code sets the figure style to "darkgrid".

Plot your data

Now we can create and customize our plots using Seaborn and Matplotlib. Here's a simple example to plot a bar chart.

Example

In this example, `sns.barplot()` creates a bar plot using Seaborn. The following 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

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")
# Display the plot
plt.show()

Output

Customization

We can further customize our plots using various Seaborn functions and Matplotlib options. For example, we can adjust the color palette, font size, grid lines, etc. Refer to the Seaborn documentation for more customization options.

The below lines of code demonstrate some additional customizations where `sns.set_palette()` changes the color palette to "husl". `plt.xticks()` and `plt.yticks()` set the font size for x-axis and y-axis ticks, respectively. `plt.grid()` adds grid lines to the plot with a dashed line style and a linewidth of 0.5.

Example

import seaborn as sns
import matplotlib.pyplot as plt

x = ["A", "B", "C", "D"]
y = [10, 20, 15, 25]
# Create a bar plot
sns.barplot(x=x, y=y)
sns.set_palette("husl")  # Change the color palette
plt.xticks(fontsize=15)  # Set x-axis tick font size
plt.yticks(fontsize=15)  # Set y-axis tick font size
plt.grid(True, linestyle="--", linewidth=1.5)  # Add grid lines  
# Set labels and title
plt.xlabel("Categories")
plt.ylabel("Values")
plt.title("Bar Chart")
# Display the plot
plt.show()

Output

Updated on: 02-Aug-2023

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements