Matplotlib Vertical Line

This article will explore a few different methods to display Matplotlib vertical lines in the Python language. Matplotlib is a commonly used Python visual analytics module. It contains a lot of graphs and visualization techniques for drawing 2D graphs using datasets in Python arrays. Matplotlib is a NumPy array-based cross-platform framework. To use the Matplotlib library’s pyplot component, we could draw vertical line graphs in Python.

Pyplot is a sequence of instructions that can be used to make different graphs. On any X–Y coordinate plane, vertical line graphs illustrate the connection among two parameters on the X-axis and Y-axis. The execution of Matplotlib vertical lines entails displaying a vertical line with the Matplotlib library’s multiple functionalities.

Utilizing the vlin() Method in Matplotlib to Create a Vertical Line Graph:

Here, the vertical lines are displayed throughout the axes using the matplotlib.pyplot.vlines() method:

import matplotlib.pyplot as plt

import numpy as np

a = [50, 200]

plt.figure(figsize = (5, 10))

plt.vlines(x = 25, ymin = 30, ymax = max(a),

colors = 'red',

label = 'vline_multiple - height')

At the start of the code, we import Matplotlib and Numpy libraries. After this, we initialize a variable to declare the array. Further, we set the size of the figure by using the figsize() function. Here, the vertical lines have been represented as vline(). The arguments provided to the vline() method construct a vertical line in this instance. The “x = 25” signifies that this will create a vertical line on the x-axis at position 25.

The initial and final values of the vertical line are indicated by the notation “ymin” and “ymax”, correspondingly. The initial stage is the “ymin”, which would be 30. Therefore, “ymax” is equivalent to max (a), in which the “a” variable represents an array with the dimensions [50, 200]. Here, we specify the color of the line by passing the “color” argument, which is red here. In the end, we call the function to display the following graph:

Python Vertical Lines With Matplotlib Utilizing axvline() Function:

The Matplotlib axvline() method, similar to the vline() method, is being used to generate vertical lines along the plot’s dimensions.

import matplotlib.pyplot as plt

import numpy as np

plt.figure(figsize = (5, 8))

plt.axvline(x = 10, color = 'k', label = 'axvline - height')

Before starting the coding, we include Matplotlib and Numpy libraries. Now, the figure size is adjusted by using the figsize() method. In addition, we are using the axvline() method to draw a vertical line in the graph. This function holds four parameters: x, color, and label. The value of “x” is 10 in this case. It takes numerals to identify the place within the x-axis to create the figure.

As the color scheme is adjusted to “k”, it produces a vertical line that is black in shade. Further, we must apply the method to show the following graph:


The method matplotlib.pyplot.vlines() is used to display a dataset. Vertical lines are represented as vlines in this function. The extended form, which specifies that this method interacts for visualizing vertical lines all over the axes, makes the technique apparent how this method executes.

import matplotlib.pyplot as plt

import numpy as np

plt.vlines(13, 10, 15, linestyles ="dashed", colors ="b")

plt.vlines(16, 11, 17, linestyles ="solid", colors ="b")

plt.vlines(18, 12, 19, linestyles ="dashed", colors ="b")

plt.xlim(10, 20)

plt.ylim(10, 20)

After introducing the Matplotlib and Numpy libraries, we call the plt.vlines() functions. Then, we draw three lines in this graph. The axis-point where the vertical line will have to be formed is the first parameter in the vlines() method. The next parameter is the lower limit of the entire length of the line, and the third parameter is the maximum limit of the entire length of the line drawn. And after all those basic arguments, we could utilize line styles to specify the sort of line displayed.

Another parameter is “color.” By using this parameter, we can set the color of the lines. Further, we apply the plt.lim() function to the x and y axes. To represent the graph, we employ the following function:

Use ax.vlines() to Generate Vertical Lines:

In the following illustration, we will deliberate using the ax.vlines() method to create vertical lines:

import matplotlib.pyplot as plt

import numpy as np

fig, ax = plt.subplots(figsize=(4, 4))


x = np.random.rand(100)


ax.vlines([30, 200], 0, 2, linestyles='solid', colors='black')

Here, we integrate Numpy and Matplotlib libraries. Next, we initialize a new object for defining the size of the figure. Further, we utilize the rand() function of the NumPy library to set the value of the x-axis. The ax.plot() method is applied to draw the line.

In addition, we employ the ax.vlines() function. The vlines() method takes two parameters: a numerical value or a 1-dimensional collection of X-values to draw a straight line. Here, we have provided [30, 200], which indicates two values. After this, there are ymin and ymax parameters, which are the line altitudes.

We have specified a range of 0 to 2 because that is the probability of the np.random.rand() method. Furthermore, we can specify the line style and color of the line. The function show() is applied to display the following graph:

At 30 and 200 coordinates on the X-axis, we have two solid vertical lines of black color. This method enables users to easily specify the ymin and ymax in quantitative data, whereas axvline() allows us to specify the altitude in proportions.


In this article, we have learned how to create a vertical line on a Matplotlib graph and label or emphasize specific areas of the figure. To execute the Matplotlib vertical line method, we must first integrate the Matplotlib library. Vertical lines could be incorporated by Matplotlib pyplot methods, such as vline(), an axvline(). Furthermore, it enables the visualization of many lines in identical figures. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content