Matplotlib Horizontal Line in python

Matplotlib Horizontal Line: Add and Plot horizontal line in Python

Suppose you want to draw horizontal lines on the figure created using Matplotlib. Then how you can do so. In this entire tutorial you will know how to add matplotlib horizontal line in python using the various methods.

Methods to Implement Matplotlib Horizontal Line

Please note that I am doing all the coding works on the Jupyter Notebook for the sake of simplicity. It’s better for you to go along with me for more understanding.  And another thing is I am using the EURUSD forex pair dataset. It’s not compulsory to use it and You can use your own dataset. Click on the link below to download the EURUSD pair dataset.

EURUSD

Follow the following methods to plot Plot horizontal line in Python using Matplotlib.

Method 1: Using the hlines() function

Matplotlib has a function hlines() that allows you to draw horizontal lines on your figure easily. The general syntax for the function is below.

matplotlib.pyplot.hlines(y, xmin, xmax, colors=None, linestyles='solid')

The explanation of the parameters is below.

y: Y-axis value from where you want to draw horizontal lines.

xmin: x-axis value from where you want to start drawing horizontal lines.

xmax: x-axis value from where you want to end drawing horizontal lines.

colors: The color of the lines.

linestyles: Use it to style the lines like solid, dot, dash e.t.c.

Execute the followings lines of code

import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
data = pd.read_csv("EURUSD.csv")
data["Close"].plot(figsize=(20,10))
plt.hlines(1.216,2.5,20,colors="red")
plt.hlines(1.208,3,10,colors="green")
plt.hlines(1.2265,4,14,colors="blue")

Explanation of the code

Here %matplotlib inline allows you to show the figure inline. Here I am plotting the close prices of the data after reading the EURUSD CSV file using the pd.read_csv() method. Lastly, I am plotting the horizontal lines using the hlines() method. If you execute the above code then you will get the following output.

Output

Plotting multiple horizontal lines using the hlines() method
Plotting multiple horizontal lines using the hlines() method

Method 2: Matplotlib Horizontal Lines using the axhline() function

The other method to add the horizontal lines is the use of axline() method. It does not use the x-min and x-max parameters just like the above. Here you have to use the y-axis value and it will plot the lines. If you want to add colors and style then you can do so using the color and linesyle parameters. 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.axhline(1.216,color="red")
plt.axhline(1.208,color="green")
plt.axhline(1.2265,color="blue")

Output

Plotting multiple horizontal lines using the axhlines() method
Plotting multiple horizontal lines using the axhlines() method

 

If you compare both the outputs then you will see the limits of the lines drawn on the chart in the latter method is the whole axis whereas in method 1 you have to set the minimum and maximum limits for the line.

Conclusion

These are the methods I have compiled for you to add and plot the Matplotlib Horizontal Lines. You can use any method according to your convenience. Hope you have liked this tutorial and it has solved your queries. If you have any other query then you can contact us for more help. Our team will be always ready to support you.

Source:

matplotlib.pyplot.hlines

matplotlib.axes.Axes.axhline

 

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.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner