How do I use ggplot styles in matplotlib plots?

Using ggplot styles in matplotlib plots is possible by importing the seaborn library. Seaborn is a library built on top of matplotlib and is designed to make creating attractive visualizations easier. It has built-in methods to set the ggplot-style aesthetic, allowing one to quickly and easily change the look and feel of their matplotlib plots. Additionally, the seaborn library contains several high-level plotting functions that can be used to quickly create visualizations with the desired ggplot-style look.


One of the most popular data visualization packages in the R programming language is ggplot2.

To apply ggplot2 styling to a plot created in Matplotlib, you can use the following syntax:

import matplotlib.pyplot as plt

plt.style.use('ggplot')

The following example shows how to use this syntax in practice.

Example: Using ggplot Styles in Matplotlib Plots

Suppose we have a NumPy array with 1,000 values:

import numpy as np

#make this example reproducible.
np.random.seed(1)

#create numpy array with 1000 values that follow normal dist with mean=10 and sd=2
data = np.random.normal(size=1000, loc=10, scale=2)

#view first five values
data[:5]

array([13.24869073,  8.77648717,  8.9436565 ,  7.85406276, 11.73081526])

We can use the following code to create a histogram in Matplotlib to visualize the distribution of values in the NumPy array:

import matplotlib.pyplot as plt

#create histogram
plt.hist(data, color='lightgreen', ec='black', bins=15)

To apply ggplot2 styling to this histogram, we can use plt.syle.use(‘ggplot’) as follows:

import matplotlib.pyplot as plt

#specify ggplot2 style
plt.style.use('ggplot')

#create histogram with ggplot2 style
plt.hist(data, color='lightgreen', ec='black', bins=15)

matplotib ggplot2 style

The histogram now has the style of a plot created in ggplot2.

Namely, this style adds a light grey background with white gridlines and uses slightly larger axis tick labels.

Note that we applied ggplot2 styling to a histogram, but the statement plt.style.use(‘ggplot’) can be used to apply ggplot2 styling to any plot in Matplotlib.

Note: You can find more style sheets available to use in Matplotlib plots .

The following tutorials explain how to create other common charts in Python:

x