Customizing Matplotlib Markers for Data Visualization

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, we will explore different ways to specify markers using Python Matplotlib. Markers are used to denote points on a graph and can be customized in various ways to enhance data visualization.

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`"]) 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/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/scatter_plots("`Scatter Plots`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") 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/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} matplotlib/importing_matplotlib -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} matplotlib/figures_axes -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} matplotlib/scatter_plots -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} python/booleans -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} python/lists -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} python/tuples -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} python/importing_modules -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} python/standard_libraries -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} python/math_random -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} python/numerical_computing -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} python/data_visualization -.-> lab-48916{{"`Customizing Matplotlib Markers for Data Visualization`"}} end

Import libraries and set random seed

We will start by importing the necessary libraries and setting a random seed to ensure reproducibility of results.

import matplotlib.pyplot as plt
import numpy as np

## Set random seed
np.random.seed(19680801)

Generate random data

We will generate random data using NumPy's random module.

x = np.random.rand(10)
y = np.random.rand(10)
z = np.sqrt(x**2 + y**2)

Create subplots

We will create a 2x3 grid of subplots using subplots() function.

fig, axs = plt.subplots(2, 3, sharex=True, sharey=True, layout="constrained")

Customize markers

We will customize markers in the following ways:

Method 1: Matplotlib marker symbol

We will use the marker parameter to specify a Matplotlib marker symbol.

axs[0, 0].scatter(x, y, s=80, c=z, marker=">")
axs[0, 0].set_title("marker='>'")
Method 2: Marker from TeX

We will use the marker parameter to specify a marker from TeX by enclosing a TeX symbol name in $-signs.

axs[0, 1].scatter(x, y, s=80, c=z, marker=r"$\clubsuit$")
axs[0, 1].set_title(r"marker=r'\$\clubsuit\$'")
Method 3: Marker from path

We will use the marker parameter to specify a custom path of N vertices as a (N, 2) array-like.

verts = [[-1, -1], [1, -1], [1, 1], [-1, -1]]
axs[0, 2].scatter(x, y, s=80, c=z, marker=verts)
axs[0, 2].set_title("marker=verts")
Method 4: Regular polygon marker

We will use the marker parameter to specify a regular polygon marker using a tuple (N, 0).

axs[1, 0].scatter(x, y, s=80, c=z, marker=(5, 0))
axs[1, 0].set_title("marker=(5, 0)")
Method 5: Regular star marker

We will use the marker parameter to specify a regular star marker using a tuple (N, 1).

axs[1, 1].scatter(x, y, s=80, c=z, marker=(5, 1))
axs[1, 1].set_title("marker=(5, 1)")
Method 6: Regular asterisk marker

We will use the marker parameter to specify a regular asterisk marker using a tuple (N, 2).

axs[1, 2].scatter(x, y, s=80, c=z, marker=(5, 2))
axs[1, 2].set_title("marker=(5, 2)")

Show the plot

We will display the plot using show() function.

plt.show()

Summary

In this lab, we learned different ways to customize markers in Python Matplotlib. We explored various methods to specify markers and demonstrated their usage with code examples. By customizing markers, we can enhance the visual appeal of our data plots and make them more informative.

Other Python Tutorials you may like