This article explains how we can draw vertical lines on a given plot. If the plotting points are given and we have to draw vertical lines from them, we can implement this by using the matplotlib library.
What is matplotlib?
Matplotlib is an open source graph plotting library that is supported by python. The installation of this library is pretty simple. If you have python installed in your system, you just have to type in this command in your terminal
pip install matplotlib
The output must look something like this :
Requirement already satisfied: numpy>=1.19 in c:\users\azka\appdata\local\programs\python\python310\lib\site-packages (from matplotlib) (1.22.2)
Requirement already satisfied: cycler>=0.10 in c:\users\azka\appdata\local\programs\python\python310\lib\site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: pillow>=6.2.0 in c:\users\azka\appdata\local\programs\python\python310\lib\site-packages (from matplotlib) (9.1.0)
Requirement already satisfied: packaging>=20.0 in c:\users\azka\appdata\roaming\python\python310\site-packages (from matplotlib) (21.3)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\azka\appdata\local\programs\python\python310\lib\site-packages (from matplotlib) (4.32.0)
Requirement already satisfied: pyparsing>=2.2.1 in c:\users\azka\appdata\roaming\python\python310\site-packages (from matplotlib) (3.0.7)
Requirement already satisfied: six>=1.5 in c:\users\azka\appdata\roaming\python\python310\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
Installing collected packages: contourpy, matplotlib
Successfully installed contourpy-1.0.5 matplotlib-3.6.1
Now you can just import matplotlib into your code and you can use all the functionalities of the library.
The matplotlib pyplot submodule
The matplotlib pylplot submodule is the most useful submodule. All the graphs can be drawn via this submodule. Here is an example of code that uses pyplot to create a simple graph
import matplotlib.pyplot as plt
x_axis=[1,2]
y_axis=[1,3]
plt.plot(x_axis, y_axis, color = "blue" )
The output for this code is :
Similarly you can add labels, change colors etc.
How to draw vertical lines on a given plot?
Method-1: axvlines
To draw vertical lines on a given plot, you can use the axvline function, this function adds a vertical span across the axis. The parameters of this function are
axvline(x,ymin, ymax, kwargs)
parameter | datatype | function |
x | float | X position |
ymin | float | Between 0-1 minimum value of y |
ymax | float | Between 0-1 maximum value of y |
**kwargs | Keyword argument | Used for properties |
Since we have to draw vertical line, we just have to specify the x
axis. So the code to draw vertical lines on a given plot will be :
import matplotlib.pyplot as plt
# draw vertical lines on a given plot
plt.axvline(x=0.34211321321)
plt.axvline(x=0.7012231312)
plt.axvline(x=0.4353452542)
The output
Method-2: Vlines
Another function that helps draw vertical lines on a given plot is vlines function, the arguments are same as that of arvlines function, here is a code to draw the vertical lines
import matplotlib.pyplot as plt
# draw vertical lines on a given plot
plt.vlines(1, 0, 2, linestyles ="solid", colors ="red")
The output of this code is :
Multiple Vertical Lines
For multiple vertical lines, create an array, with all the given plots and traverse the array through a loop to draw vertical lines on a given plot
The code is :
xposition = [1,2,3,4]
for xc in xposition:
    #draw vertical lines on a given plot
    plt.axvline(x=xc, color='purple', linestyle='dotted')
Output of this code is :
Conclusion
In this article we studied how to draw vertical lines on a given plot using matplotlib module and the pyplot submodule. We studied how different functions like arvline and vline function is used for the implementation of vertical lines plotting on graph. We also studied how multiple lines can be drawn by using a loop.
Further Reading