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.

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