Mapping Marker Properties to Multivariate Data

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to use different properties of markers to plot multivariate datasets using Python's Matplotlib library. Specifically, you will learn how to represent a successful baseball throw as a smiley face with marker size mapped to the skill of thrower, marker rotation to the take-off angle, and thrust to the marker color.

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`"]) 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`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") 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/using_packages("`Using Packages`") 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-48845{{"`Mapping Marker Properties to Multivariate Data`"}} matplotlib/figures_axes -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} matplotlib/line_plots -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/for_loops -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/lists -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/tuples -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/importing_modules -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/using_packages -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/standard_libraries -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/math_random -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/numerical_computing -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/data_visualization -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} python/build_in_functions -.-> lab-48845{{"`Mapping Marker Properties to Multivariate Data`"}} end

Import Libraries

In this step, you will import the necessary libraries for this lab. Specifically, you will import Matplotlib, Numpy, and various modules from Matplotlib such as MarkerStyle, TextPath, and Affine2D.

import matplotlib.pyplot as plt
import numpy as np

from matplotlib.colors import Normalize
from matplotlib.markers import MarkerStyle
from matplotlib.text import TextPath
from matplotlib.transforms import Affine2D

Define Success Symbols

In this step, you will define the three success symbols that will be used to represent the success of a baseball throw. Specifically, you will define a smiley face for a successful throw, a neutral face for a partially successful throw, and a sad face for an unsuccessful throw.

SUCCESS_SYMBOLS = [
    TextPath((0, 0), "☹"),
    TextPath((0, 0), "😒"),
    TextPath((0, 0), "☺"),
]

Generate Random Data

In this step, you will generate random data for the skill of the thrower, take-off angle, thrust, success, and position. Specifically, you will generate 25 data points for each variable, except for position, which will have 2 coordinates for each data point.

N = 25
np.random.seed(42)
skills = np.random.uniform(5, 80, size=N) * 0.1 + 5
takeoff_angles = np.random.normal(0, 90, N)
thrusts = np.random.uniform(size=N)
successful = np.random.randint(0, 3, size=N)
positions = np.random.normal(size=(N, 2)) * 5
data = zip(skills, takeoff_angles, thrusts, successful, positions)

Define Color Map

In this step, you will define the color map that will be used to map the thrust of the throw to the color of the marker. Specifically, you will use the "plasma" color map from Matplotlib.

cmap = plt.colormaps["plasma"]

Create Plot

In this step, you will create the plot using the random data generated earlier. Specifically, you will plot each data point as a marker with the success symbol determined by the success variable, the size determined by the skill variable, the rotation determined by the take-off angle variable, and the color determined by the thrust variable.

fig, ax = plt.subplots()
fig.suptitle("Throwing success", size=14)
for skill, takeoff, thrust, mood, pos in data:
    t = Affine2D().scale(skill).rotate_deg(takeoff)
    m = MarkerStyle(SUCCESS_SYMBOLS[mood], transform=t)
    ax.plot(pos[0], pos[1], marker=m, color=cmap(thrust))
fig.colorbar(plt.cm.ScalarMappable(norm=Normalize(0, 1), cmap=cmap),
             ax=ax, label="Normalized Thrust [a.u.]")
ax.set_xlabel("X position [m]")
ax.set_ylabel("Y position [m]")

Display Plot

In this step, you will display the plot using Matplotlib's show() function.

plt.show()

Summary

In this lab, you learned how to use different properties of markers to plot multivariate datasets using Python's Matplotlib library. Specifically, you learned how to represent a successful baseball throw as a smiley face with marker size mapped to the skill of thrower, marker rotation to the take-off angle, and thrust to the marker color. By following the steps outlined in this lab, you can create similar plots for your own multivariate datasets.

Other Python Tutorials you may like