Matplotlib Plotting with ggplot Style

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. This tutorial will guide you through the process of creating a plot using the ggplot style sheet in Matplotlib.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Import Libraries and Set Style Sheet

First, we need to import the required libraries and set the ggplot style sheet.

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('ggplot')

Create a Scatter Plot

We will create a scatter plot with random data points.

## Fixing random state for reproducibility
np.random.seed(19680801)

## Create random data points
x, y = np.random.normal(size=(2, 200))

## Create a scatter plot
plt.plot(x, y, 'o')
plt.show()

Create Sinusoidal Lines

We will create sinusoidal lines with colors from the default color cycle.

## Create sinusoidal lines
L = 2*np.pi
x = np.linspace(0, L)
ncolors = len(plt.rcParams['axes.prop_cycle'])
shift = np.linspace(0, L, ncolors, endpoint=False)

for s in shift:
    plt.plot(x, np.sin(x + s), '-')
plt.margins(0)
plt.show()

Create Bar Graphs

We will create bar graphs with random data points.

## Create bar graphs
x = np.arange(5)
y1, y2 = np.random.randint(1, 25, size=(2, 5))
width = 0.25

plt.bar(x, y1, width)
plt.bar(x + width, y2, width, color=list(plt.rcParams['axes.prop_cycle'])[2]['color'])
plt.xticks(x + width, labels=['a', 'b', 'c', 'd', 'e'])
plt.show()

Create Circles

We will create circles with colors from the default color cycle.

## Create circles
fig, ax = plt.subplots()
for i, color in enumerate(plt.rcParams['axes.prop_cycle']):
    xy = np.random.normal(size=2)
    ax.add_patch(plt.Circle(xy, radius=0.3, color=color['color']))
ax.axis('equal')
ax.margins(0)
plt.show()

Summary

In this tutorial, we learned how to create a plot using the ggplot style sheet in Matplotlib. We created a scatter plot, sinusoidal lines, bar graphs, and circles with colors from the default color cycle. Matplotlib is a powerful tool for creating visualizations in Python.

Other Python Tutorials you may like