Create Event Plots with Matplotlib

PythonPythonBeginner
Practice Now

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

Introduction

In this lab, you will learn how to create an event plot using Matplotlib. An event plot is a way to show the occurrence of events over time. The events can be represented as lines or dots. This lab will guide you through creating horizontal and vertical event plots with different line properties.

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/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/AdvancedTopicsGroup -.-> matplotlib/event_handling("`Event Handling`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/matplotlib_config("`Customizing Matplotlib Configurations`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/list_comprehensions("`List Comprehensions`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/sets("`Sets`") 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 python/comments -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} matplotlib/importing_matplotlib -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} matplotlib/figures_axes -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} matplotlib/event_handling -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} matplotlib/matplotlib_config -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/for_loops -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/list_comprehensions -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/lists -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/tuples -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/sets -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/importing_modules -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/standard_libraries -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/math_random -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/numerical_computing -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/data_visualization -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} python/build_in_functions -.-> lab-48721{{"`Create Event Plots with Matplotlib`"}} end

Import libraries and set random seed

We will start by importing the necessary libraries and setting a random seed for reproducibility.

import matplotlib.pyplot as plt
import numpy as np

import matplotlib

matplotlib.rcParams['font.size'] = 8.0

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

Create random data

Next, we will create some random data to use for our event plots.

data1 = np.random.random([6, 50])

Set colors and line properties for the first event plot

We will set different colors and line properties for each set of positions in the first event plot.

colors1 = [f'C{i}' for i in range(6)]

lineoffsets1 = [-15, -3, 1, 1.5, 6, 10]
linelengths1 = [5, 2, 1, 1, 3, 1.5]

Create the first event plot - horizontal orientation

We will create the first event plot in a horizontal orientation.

fig, axs = plt.subplots(2, 2)

axs[0, 0].eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
                    linelengths=linelengths1)

Create the first event plot - vertical orientation

We will create the first event plot in a vertical orientation.

axs[1, 0].eventplot(data1, colors=colors1, lineoffsets=lineoffsets1,
                    linelengths=linelengths1, orientation='vertical')

Create random data for the second event plot

We will create another set of random data for the second event plot. We will use the gamma distribution for aesthetic purposes.

data2 = np.random.gamma(4, size=[60, 50])

Set line properties for the second event plot

We will use individual values for the line properties in the second event plot. These values will be used for all data sets except for lineoffsets2, which sets the increment between each data set.

colors2 = 'black'
lineoffsets2 = 1
linelengths2 = 1

Create the second event plot - horizontal orientation

We will create the second event plot in a horizontal orientation.

axs[0, 1].eventplot(data2, colors=colors2, lineoffsets=lineoffsets2,
                    linelengths=linelengths2)

Create the second event plot - vertical orientation

We will create the second event plot in a vertical orientation.

axs[1, 1].eventplot(data2, colors=colors2, lineoffsets=lineoffsets2,
                    linelengths=linelengths2, orientation='vertical')

Show the event plots

We will show the event plots using plt.show().

plt.show()

Summary

In this lab, you learned how to create event plots in Matplotlib. You learned how to create horizontal and vertical event plots with different line properties. By following the step-by-step guide, you can easily create your own event plots for your data.

Other Python Tutorials you may like