Matplotlib - Step Plots



A step plot is a type of graph that shows how values change abruptly at specific points, rather than changing continuously. It looks like a series of horizontal and vertical lines connecting the data points. Each horizontal line represents a period where the value remains constant, and each vertical line indicates when the value changes to a new level.

Imagine you are monitoring the temperature of a room throughout the day. You take measurements every hour, and instead of recording every slight change, you note the temperature only when it significantly shifts, like when the air conditioning turns on or off. A step plot of this data would show a horizontal line for each period when the temperature remains constant, connected by vertical lines where the temperature changes abruptly −

Step Plots

Step Plots in Matplotlib

In Matplotlib, a step plot is a type of graph that connects data points using horizontal and vertical lines in the XY plane, forming a series of steps. We can use the step() function from the 'pyplot' module to create a step plot. This function accepts the X and Y coordinates as arrays. It connects the X coordinates horizontally until it reaches an X coordinate where the value of Y coordinate changes vertically to a new constant.

Lets start by drawing a basic step plot.

Basic Step Plot

A basic step plot in Matplotlib visually represents a plot where the steps are present at the midpoint of each interval. This means that the change in the y-axis value occurs at the midpoint between two x-axis values. For example, if the x-axis has values 1 and 2, then the step will be present at the value 1.5.

Example

In the following example, we are creating a basic step plot. The data points consists of two arrays, 'x' and 'y,' which represent the values along the x-axis and y-axis, respectively. We are then joining the data points at the center of the interval −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 1, 3, 5])

# Creating a basic step plot
plt.step(x, y, where='mid', color='blue', label='Step Plot')

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Step Plot')

# Displaying the plot
plt.show()

Output

Following is the output of the above code −

Basic Step Plot

Post Step Plot

A post step plot in Matplotlib represents a plot where the steps are positioned at the end of each interval. This means that the change in the y-axis value occurs at the endpoint of the x-axis values.

We can create a post step plot in Matplotlib by specifying the value 'post' to the 'where' parameter. This type of plot creates a staircase-like pattern. For example, if the x-axis has values 1 and 2, then the step will be present at the value 2.

Example

In here, we are generating a post step plot. The data points are represented by two arrays, 'x' and 'y', for the x-axis and the y-axis values, respectively. We then join the data points at the end of each interval by setting the 'where' parameter to 'post' −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 1, 3, 5])

# Creating a step plot with 'post' where parameter
plt.step(x, y, where='post', color='green', label='Step Plot with "post"')

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Post Step Plot')

# Displaying the plot
plt.show()

Output

Output of the above code is as follows −

Post Step Plot

Pre Step Plot

In Matplotlib, a pre step plot refers to creating a plot where the steps are positioned at the start of each interval. This means that the change in the y-axis value occurs at the starting point of the x-axis values.

We can create a pre step plot in Matplotlib by specifying the value 'pre' to the 'where' parameter. For example, if the x-axis has values 1 and 2, then the step will be present at the value 1.

Example

The following example creates a pre step plot. The data points are represented by two arrays, 'x' and 'y', which define the values for the x-axis and the y-axis, respectively. We then join the data points at the start of each interval by setting the 'where' parameter to 'pre' −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 1, 3, 5])

# Creating a step plot with 'pre' where parameter
plt.step(x, y, where='pre', color='red', label='Step Plot with "pre"')

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Pre Step Plot')

# Displaying the plot
plt.show()

Output

After executing the above code, we get the following output −

Pre Step Plot

Sinusoidal Step Plot

A sinusoidal step plot in Matplotlib is a visualization of a plot where the steps form a sinusoidal wave in the XY plane. This means that the steps are continuously present throughout the graph instead of being located at specific locations.

Example

Now, we are generating a sinusoidal step plot in Matplotlib. Here, we first create the x-axis values from 0 to 10 and then calculate the y-axis values by taking the sine value of each x-axis value. We then combine the data points to produce a continuous step plot −

import matplotlib.pyplot as plt
import numpy as np

# Generating data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Creating a basic step plot
plt.step(x, y, where='mid', color='blue', label='Step Plot')

# Adding labels and title
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Sinusoidal Step Plot')

# Displaying the plot
plt.show()

Output

The output obtained is as shown below −

Sinusoidal Step Plot
Advertisements