matplotlib.pyplot.step() function in Python
Last Updated :
14 Aug, 2020
The step() function designs the plot such that, it has a horizontal baseline to which the data points will be connected by vertical lines. This kind of plot is used to analyze at which points the change in Y-axis value has occurred exactly with respect to X-axis. This is very useful in discrete analysis. The step plotting can be combined with any other plots. The occurrence of steps can also be formatted by supplying appropriate parameter values. The parameters for this function are tabulated below.
Parameters
| Description
|
x
| an 1-D array of values. |
y
| an 1-D array of values. |
fmt
| Formatting strings that specify line color, marker type ,etc,. |
data
| Two iterables containing the label names for labelled data. |
where
| To decide the position of the vertical line. (values : pre|post|mid) |
Note: The parameters 'fmt', 'data', 'where' are optional.
'where' parameter
The where parameter is used to denote at which place the vertical line should connect the data point to the horizontal base line. It decides where to place the step like visualization. To be more clear, this parameter decides where the Y-value should be constantly drawing a horizontal line. It can take any one among the three values which are explained below with examples.
First, consider two arrays that are to be plotted :
x = [1,3,4,5,7]
y = [1,9,16,25,49]
The array x contains some integers and the array y contains the squares of the numbers in the corresponding index in x. If these values are simply plotted without any step function one gets a straight line plotted in the graph as given below.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 3, 4, 5, 7])
y = np.array([1, 9, 16, 25, 49])
plt.plot(x, y)
plt.show()
Output:
a plot without steps
pre : The Y-value remains constant to the left of the data point. For example the value y[i] remains constant between x[i-1] and x[i]. The plot with step for the same one plotted above is given below. In the code below, 'g^' denotes the line color is green and the marker should be an upward triangle.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 3, 4, 5, 7])
y = np.array([1, 9, 16, 25, 49])
plt.step(x, y, 'g^', where='pre')
plt.show()
Output:
where='pre'
In the image above you can see the horizontal lines to the left of the data points which are marked by small green triangles. For example, x[0] is 1 and its corresponding y-value is 1. The next value x[1] is 3 and the y-value is 9. Now you can see a constant line in 9 from 1. The vertical line shoots up once the data point 3 is reached in the X-axis. To be more clear, look at the image with highlighted portions, given below.
horizontal lines in range x[i-1] and x[i]- post: The Y-value remains constant to the right of the data point. For example the value y[i] remains constant between x[i] and x[i+1]. The plot with step for the same one plotted above is given below. In the code below, 'r*' denotes the line color is red and the marker should be an asterisk ("*").
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 3, 4, 5, 7])
y = np.array([1, 9, 16, 25, 49])
plt.step(x, y, 'r*', where='post')
plt.show()
Output:
where='post'
Look at the highlighted portions to view the difference between the values 'pre' and 'post'.
horizontal lines in range x[i] and x[i+1]- mid: The vertical line shoots up at a value (x[i-1] and x[i+1])/2. The plot with step for the same one plotted above is given below. In the code below, 'cs' denotes the line color is red and the marker should be a square.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 3, 4, 5, 7])
y = np.array([1, 9, 16, 25, 49])
plt.step(x, y, 'cs', where='mid')
plt.xlim(1, 9)
plt.show()
Output:
where='mid'
For better understanding, look at the image below.
horizontal line in a value (x[i-1] and x[i+1])/2
For example, x[0] is 1, x[1] is 3 and the mid-value is 2. In a similar way, for other values also the mid-value is calculated and the vertical line raises there.
The step() can be combined with other plots also. Look at the example below for the same data points used above.
Python3
import matplotlib.pyplot as plt
import numpy as np
x = np.array([1, 3, 4, 5, 7])
y = np.array([1, 9, 16, 25, 49])
plt.step(x, y, 'ys', where='mid')
plt.xlim(1, 9)
plt.bar(x, y)
plt.show()
Output:
In the same way, any plot type can be combined with the step() function.
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read