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 white background with grid lines. Changing the figure style in Seaborn is a straightforward process, and it can greatly impact the aesthetics of our visualizations.

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

Install Seaborn

First we should check if 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

Next we have to use the 'sns.set_style()' function to set the figure style to "whitegrid". This function modifies the default styles of Matplotlib.

sns.set_style("whitegrid")

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

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

sns.set_style("whitegrid")
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

sns.set_style("whitegrid")
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")

# Customizing the Chart
sns.set_palette("husl")  # Change the color palette
plt.xticks(fontsize=10)  # Set x-axis tick font size
plt.yticks(fontsize=10)  # Set y-axis tick font size
plt.grid(True, linestyle="--", linewidth=0.1)

# Display the plot
plt.show()

Output

Updated on: 02-Aug-2023

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements