Matplotlib Vertical Line: Add and Plot Vertical Lines in Python

Matplotlib Vertical Line featured image

Sometimes you have to divide the chart using vertical lines. It makes you find meaningful information about the chart. In this entire tutorial, you will learn how to implement matplotlib vertical line in python using various methods

Methods to Implement Matplotlib Vertical Line

Before going to the coding demonstration part please note that I am coding in Jupyter Notebook. So it’s better that you should also do it for more understanding. The dataset for plotting the chart or figure is the EURUSD forex pair. You can download it from the below.

EURUSD Dataset

The below are the methods for plotting vertical lines in the chart.

Method 1: Using the vlines() function

The Matplotlib python module provides a method for adding the vertical lines in the figure. The syntax for the vlines() function is below.

vlines(x_coordinate, y_axis_min, y_axis_max, colors)

Here the x_coordinate is the value on the x axis you want to draw a vertical line. The  y_axis_min and  y_axis_max allow you to specify from where you want to draw the vertical lines.

Execute the code below draw the vertical lines.

import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
data = pd.read_csv("EURUSD.csv")
data["Close"].plot(figsize=(20,10))
min = data["Close"].values.min()
max = data["Close"].values.max()
plt.vlines(15,min,max,colors="red")
plt.vlines(6,min,max,colors="green")
plt.vlines(12,min,max,colors="blue")

Explanation of the code

Here I am first reading the forex pair dataset using the pandas.read_csv() method. After that plotting the line chart on the Closing price of the pair. If you look into the code I have set the y_axis_min as the minimum of all the closing prices. In the same way for y_axis_max I am using the maximum value of the close prices. Lastly, I am adding three vertical lines with different colors. When you execute the above lines of code you will get the following output.

Output

Plotting vertical lines using the vlines() method
Plotting vertical lines using the vlines() method

Method 2: Matplotlib Vertical lines using axvline() function

In the above method, you can see in the output, vertical lines are drawn by specifying the minimum and maximum value of the y coordinate. Suppose you want to draw vertical lines using the axis coordinates only. To do so you have to use axvline() method. Inside the method, you have to just pass the x-axis value and pass the other arguments like color, linstyles e.t.c to draw the vertical lines.

Just execute the below lines of code and see the output.

import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
data = pd.read_csv("EURUSD.csv")
data["Close"].plot(figsize=(20,10))
plt.axvline(x=6,color="green")
plt.axvline(x=12,color="blue")
plt.axvline(x=15,color="red")

Here in the code, you can see I am passing the x =6,12,15 as the coordinate value to plot the vertical lines on the figure. If you run the code then you will see the output as below.

Output

Plotting vertical lines using the axvline() method
Plotting vertical lines using the axvline() method

Conclusion

These are the methods to plot the vertical lines on any figure using the Matplotlib module. You can choose any method you want but I will prefer the second method as it is simple and just require the axis value(x) to draw the lines. In my example, I have called the method for each line. You can use a loop for plotting all the points available in the list.

Hope this tutorial has clearly your query on how to plot vertical lines in python using Matplotlib. If you want to learn more then please contact us for more information.

Source:

matplotlib.pyplot.vlines()

matplotlib.pyplot.axvline

 

 

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner