Matplotlib Grayscale Style Sheet

PythonPythonBeginner
Practice Now

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

Introduction

Matplotlib is a data visualization library in Python used to create a variety of charts, graphs, and plots. It provides a wide range of customization options to create attractive visualizations. One of these options is style sheets. A style sheet is a collection of settings that define the look of a plot. In this lab, we will explore the "grayscale" style sheet, which changes all colors to grayscale.

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 matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) 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/heatmaps("`Heatmaps`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("`Customizing Matplotlib Configurations`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") 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 matplotlib/importing_matplotlib -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} matplotlib/figures_axes -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} matplotlib/line_plots -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} matplotlib/heatmaps -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} matplotlib/matplotlib_config -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/booleans -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/for_loops -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/lists -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/tuples -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/function_definition -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/importing_modules -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/standard_libraries -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/math_random -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/numerical_computing -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/data_visualization -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} python/build_in_functions -.-> lab-48756{{"`Matplotlib Grayscale Style Sheet`"}} end

Importing the Required Libraries

We start by importing the required libraries. We will need NumPy and Matplotlib.

import matplotlib.pyplot as plt
import numpy as np

Setting Up the Random State

To ensure the reproducibility of the results, we set up the random state using the following code:

np.random.seed(19680801)

Defining the Color Cycle Example Function

We define the color_cycle_example function that takes an axis object as input and plots a sine wave for each color in the color cycle. The color cycle is defined by the rcParams.

def color_cycle_example(ax):
    L = 6
    x = np.linspace(0, L)
    ncolors = len(plt.rcParams['axes.prop_cycle'])
    shift = np.linspace(0, L, ncolors, endpoint=False)
    for s in shift:
        ax.plot(x, np.sin(x + s), 'o-')

Defining the Image and Patch Example Function

We define the image_and_patch_example function that takes an axis object as input, plots a random image, and adds a patch to the plot.

def image_and_patch_example(ax):
    ax.imshow(np.random.random(size=(20, 20)), interpolation='none')
    c = plt.Circle((5, 5), radius=5, label='patch')
    ax.add_patch(c)

Using the Grayscale Style Sheet

We set the style sheet to "grayscale" using the following code:

plt.style.use('grayscale')

Creating the Subplots

We create a figure with two subplots using the following code:

fig, (ax1, ax2) = plt.subplots(ncols=2)
fig.suptitle("'grayscale' style sheet")

Plotting the Examples

We plot the color cycle example on the first subplot and the image and patch example on the second subplot using the following code:

color_cycle_example(ax1)
image_and_patch_example(ax2)

Displaying the Plot

We display the plot using the following code:

plt.show()

Summary

In this lab, we learned how to use the "grayscale" style sheet in Matplotlib to create plots with all colors in grayscale. We also learned how to create subplots, plot examples, and display the plot. Style sheets are an excellent way to customize the look of your plots, and Matplotlib provides many built-in style sheets to choose from.

Other Python Tutorials you may like