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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlottingDataGroup -.-> matplotlib/bar_charts("`Bar Charts`") matplotlib/PlotCustomizationGroup -.-> matplotlib/axis_ticks("`Axis Ticks Customization`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("`Customizing Matplotlib Configurations`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} matplotlib/importing_matplotlib -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} matplotlib/figures_axes -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} matplotlib/line_plots -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} matplotlib/bar_charts -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} matplotlib/axis_ticks -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} matplotlib/matplotlib_config -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/variables_data_types -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/booleans -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/for_loops -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/lists -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/tuples -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/importing_modules -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/standard_libraries -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/math_random -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/data_collections -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/numerical_computing -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/data_visualization -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} python/build_in_functions -.-> lab-48753{{"`Matplotlib Plotting with ggplot Style`"}} end

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