How to Draw Horizontal Grid Lines Matplotlib
In this Python tutorial, we will discuss Horizontal line matplotlib in python. Here we will cover different examples related to the horizontal lines using matplotlib. And we will also cover the following topics:
- Horizontal line matplotlib
- Dotted horizontal line matplotlib
- Horizontal line matplotlib with label
- Multiple horizontal line matplotlib
- Matplotlib horizontal line thickness
- Matplotlib horizontal line color
- Matplotlib horizontal line at value
- Matplotlib horizontal line at 0
- Horizontal line matplotlib subplot
- Matplotlib horizontal line full width
- Matplotlib errorbar horizontal line
- Horizontal grid lines matplotlib
- Matplotlib horizontal line with text
- Annotate horizontal line matplotlib
- Matplotlib remove horizontal line
- Matplotlib add horizontal line to bar chart
Horizontal line matplotlib
In this section, we learn about how to plot or draw a horizontal line in matplotlib in Python. Before starting the topic, firstly we have to understand what is a horizontal line or how it looks.
In general, a horizontal line is a straight line that goes from right to left or vice versa. Or if we talk in coordinates plane, a line parallel to X-axis is known as a horizontal line.
In Python, matplotlib is a popular library used for plotting. It provides different methods to draw a horizontal line which we have discussed below.
- Using plot()
- Using hlines()
- Using axhline()
By using plot() function
In matplotlib, the plot() method is used to generate 2D plots.
The syntax of the plot() method is as given below:
matplotlib.pyplot.plot(x_points, y_points)
The parameters used above are:
- x_points: x-axis points to plot.
- y_points: y-axis points to plot.
Let's see an example of drawing a horizontal line using the plot() method:
# Import Library import matplotlib.pyplot as plt # Define Data X = [2, 4, 6, 8] Y = [0, 0, 0, 0] # Plot horizontal line plt.plot(X,Y) # Show plt.show()
- In the above example, we import matplotlib.pyplot library.
- After this, we define data points for plotting. Here we specify the y-axis points to 0 as we want to plot a horizontal line.
- Next, by using the plt.plot() method we plot a line, and by using the plt.show() method user visualizes the plot.
By using hlines() function
In matplotlib, the hlines() method is used to draw a horizontal line across the axes. Here hlines stand for the horizontal lines.
The syntax of the hlines() method is as given below:
matplotlib.pyplot.hlines(y, xmin, xmax, colors, linestyles)
The parameters used above are described as below:
- y: specify the position on the y-axis to plot the line.
- xmin and xmax: specify the beginning and end of each line. These attributes are compulsory.
- colors: specify the color of the line.
- linestyle: specify the style or type of the line
Let's see an example of the horizontal line created by using the hlines() method:
# Import Library import matplotlib.pyplot as plt # Plot horizontal line plt.hlines(5, 4, 8, color='red') # Show plt.show()
- In the above example, we import matplotlib.pyplot library.
- By using plt.hlines() method we plot horizontal lines. Here we pass y, xmin, xmax, color as an argument and set it to 5, 4, 8, and red.
By using axhline() function
In matplotlib, the axhline() method is used to draw horizontal lines to the plot.
The syntax of the axhline() method is as given below:
matplotlib.pyplot.axhline(y, xmin, xmax, color, linestyle)
The above-used parameters are described as below:
- y: specify position on the y-axis to plot the line.
- xmin and xmax: specify the starting and ending range of the line.
- color: specify the color of the line.
- linestyle: specify the style of the line.
Example of axhline() method:
# Import Library import matplotlib.pyplot as plt # Plot horizontal line plt.axhline(y=1.5, xmin=0.1, xmax= 0.8) # Show plt.show()
Here we use the axhline() method to draw a horizontal line and pass the y, xmin, xmax as a parameter and set its value to 1.5, 0.1, 0.8 respectively.
Read: Matplotlib plot a line
Dotted horizontal line matplotlib
Here we are going to learn how we can draw horizontal dotted lines in matplotlib in python. We have to change the line style of lines to make them dotted in style.
Matplotlib.pyplot library provides the parameter linestyle to set the type of line.
Let's see an example of a horizontal dotted line:
# Import Library import matplotlib.pyplot as plt # Plot horizontal dotted line plt.axhline(y=5, xmin=0.25, color='red', linestyle='dotted', linewidth=5) # Show plt.show()
- Here we use the axhline() method to draw a dotted horizontal line. We pass parameters y, xmin, color, linestyle, and linewidth.
- Our main objective is to draw a horizontal line that is dotted in nature, so we set linestyle to dotted and also increase the width of the line by using linewidth parameter.
Read: Python plot multiple lines using Matplotlib
Horizontal line matplotlib with label
In this section, we are going to learn how to draw a horizontal line with a label. Here we use the axhline() method to draw a horizontal line and pass label as a parameter.
The syntax to draw a horizontal line with label:
matplotlib.pyplot.axhline(y, xmin, xmax, color, label)
Example:
# Import Library import matplotlib.pyplot as plt # Plot horizontal line plt.axhline(y=10, linewidth=5, label= 'horizontal-line') # Add label plt.legend(loc = 'upper left') # Show plt.show()
- By using the plt.axhline() method we draw a horizontal line and pass the parameter label.
- Then by using the plt.legend() method we set the location of the label to the upper left.
Read: Matplotlib subplot tutorial
Multiple horizontal line matplotlib
Here we are going to plot multiple horizontal lines in matplotlib in python. The methods used to draw multiple horizontal lines are:
- Using axhline()
- Using hlines()
By using axhline()
In matplotlib, by using the axhline() method we can draw multiple horizontal lines in the plot.
Let's see an example related to this:
# Import library import matplotlib.pyplot as plt # Horizontal line 1 plt.axhline (y = 10, color = 'b') # Horizontal line 2 # We also set range of the line plt.axhline (y = 6, xmin =0.1, xmax =0.5, color='red') # Horizontal line 3 plt.axhline (y = 2, color = 'm') # Horizontal line 4 # We also set range of the line plt.axhline (y = 4, xmin =0.6, xmax =0.8, color='c') # Display plt.show()
By using hlines()
In matplotlib, by using the hlines() method we can draw multiple horizontal lines in the plot.
Example:
# Import library import matplotlib.pyplot as plt # Horizontal line 1 plt.hlines (y= 2, xmin= 0.1, xmax= 0.5, color='c', linewidth = 2 ) # Horizontal line 2 plt.hlines (y= 6, xmin = 0.4, xmax = 0.9, color='m', linestyle = 'dotted', linewidth = 3) # Horizontal line 3 plt.hlines (y = 8, xmin = 0.5, xmax = 0.9, color='orange', linestyle = 'dashed', linewidth = 5) # Horizontal line 4 plt.hlines (y = 10, xmin =0.6, xmax =0.8, color='red', linewidth = 8 ) # Display plt.show()
Also, read: Matplotlib plot bar chart
Matplotlib horizontal line thickness
In matplotlib, to adjust the thickness of the horizontal line we have to pass an argument linewidth. By using linewidth as a parameter we can easily increase or decrease the width or thickness of the line.
The syntax to adjust thickness is as follow:
matplotlib.pyplot.axhine(y, xmin, xmax, linewidth )
Let's see an example where we adjust the thickness of the horizontal line:
# Import library import matplotlib.pyplot as plt # Thickness increased plt.axhline (y = 5, color = 'red', linewidth = 8) # Thickness decreased plt.axhline (y = 2, linewidth = 0.5) # Display plt.show()
- In the above example, we use plt.axhline() method to plot a horizontal line.
- Here we pass linewidth as an argument and set its value to 8 and 0.5 in different cases.
Read: Matplotlib plot error bars
Matplotlib horizontal line color
In matplotlib, to change the color of the horizontal line we have to pass an argument color to the method axhilne() or hlines(). By default color of the line is blue.
The syntax to change the color of the line is given below:
# Using axhline() matplotlib.pyplot.axhline(y, color= None) # Using hlines() matplotlib.pyployt,hlines(y, xmin, xmax, color= None)
Let's see an example related to change of color of horizontal line:
# Import library import matplotlib.pyplot as plt # Change color by using hlines() plt.hlines (y = 8, xmin = 0.5, xmax = 0.9, color='orange', linestyle = 'dashed', linewidth = 5) # OR # Change color by using axhline() plt.axhline (y = 10, xmin =0.6, xmax =0.8, color='red', linewidth = 8 ) # Display plt.show()
- In the above example, we use plt.hlines() method and pass the color as an argument and set its value to orange.
- In the next case, we use the plt.axhline() method to draw a horizontal line and here we pass the color as an argument to change the color of the line and set its value to red.
Read: Matplotlib remove tick labels
Matplotlib horizontal line at value
Here we are going to learn how to draw a horizontal line at a specific value on the plotted graph.
Let's see an example:
# Import Library import matplotlib.pyplot as plt import numpy as np # Define Data x = np.arange(0,25, 0.1) y = np.sin(x) # Plot figure plt.plot(x, y) # Horizontal line plt.axhline(y= -0.5,color='red', linestyle ='dashed') # Title plt.title("Sine Function", fontsize= 15, fontweight='bold') # Generate Plot plt.show()
- In the above example, we import matplotlib and numpy library.
- After this, we define data to plot a graph by using np.arange() method and np.sin() method.
- By using plt.plot() method we plot a sin graph.
- Then to draw a horizontal line at a specific value we use plt.axhline() method and pass the argument y = -0.5.
- plt.title method is used add title on the plot and plt.show() method is used for plot visualization.
Read: Matplotlib rotate tick labels
Matplotlib horizontal line at 0
In matplotlib, to draw a horizontal line at 0 we have to set the value of the y coordinate 0.
The syntax to draw a horizontal line at 0:
# By using axhline matplotlib.pyplot.axhline(y = 0) # By using hlines matplotlib.pyplot.lines(y=0, xmin, xmax)
Example ( By using hlines() ) :
# Import Library import matplotlib.pyplot as plt # Define Data x = [1, 2, 3.5, 4, 5, 6.5] y = [0, 2, 4, 15, 8, 10] # Plot figure plt.plot(x, y) # Horizontal line at 0 plt.hlines(y= 0, xmin= 1.5, xmax= 5.5, color='red', linestyle ='dashed', linewidth = 5) # Generate Plot plt.show()
- Here we use hlines() method and pass the y, xmin, xmax, color, linestyle, and linewidth as an argument.
- We set their values to 0, 1.5, 5.5, dashed, and 5 respectively.
Read: Matplotlib change background color
Horizontal line matplotlib subplot
Here we will discuss how we can draw the horizontal line at the specific subplot if we draw subplots in a figure area.
We will use the axhline() method to draw a horizontal line at specific subplots.
Let's understand the concept with the help of an example:
# Importing Libraries import numpy as np import matplotlib.pyplot as plt # Define Data x1= [0.2, 0.4, 0.6, 0.8, 1] y1= [0.3, 0.6, 0.8, 0.9, 1.5] x2= [2, 6, 7, 9, 10] y2= [3, 4, 6, 9, 12] x3= [5, 8, 12] y3= [3, 6, 9] x4= [7, 8, 15] y4= [6, 12, 18] fig, ax = plt.subplots(2, 2) # Plot graph # Set horizontal line at specfic subplot ax[0, 0].plot(x1, y1) ax[0, 1].axhline(y=4) ax[1, 0].plot(x3, y3) ax[1, 1].axhline(y=2.5) # Display Graph fig.tight_layout() plt.show()
- In the above example, we plot multiple plots in a figure area. And we want to draw a horizontal line at the specific plot.
- Here we use the axhline() method to draw a horizontal line.
Read: Matplotlib scatter marker
Matplotlib horizontal line full width
In matplotlib, if you want to draw a horizontal line with full width simply use the axhline() method.
You can also use the hlines() method to draw a full-width horizontal line but in this method, you have to set xmin and xmax to full width.
Example:
# Import Library import matplotlib.pyplot as plt # Plot vertical line plt.axhline(y=1.5) # Show plt.show()
In the above example, we use the axhline() method to plot a horizontal line of full width.
Read: Matplotlib dashed line
Matplotlib errorbar horizontal line
To draw a horizontal line on the errorbar we have to pass an argument xerr in ax.errorbar().
The syntax for the errorbar horizontal line is as given below:
matplotlib.pyplot.errorbar(x, y, yerr, capsize)
The above-used parameters are as follow:
- x: specifies horizontal coordinates.
- y: specifies vertical coordinates.
- xerr: Define the horizontal error bar sizes.
Let's see an example for more understanding:
# Import Library import matplotlib.pyplot as plt # Define Data x= [1, 2, 3, 5] y= [9, 15, 5, 25] # Plot horizontal error bar plt.errorbar(x, y, xerr = 1.2) # Display graph plt.show()
- In the above, example we import thematplotlib.pyplot library.
- Then we define the x-axis and y-axis data points.
- plt.errorbar() method is used to plot error bars and we pass the argumentx, y, andxerr and set the value of xerr =1.2.
- Then we useplt.show() method to display the error bar plotted graph.
Read: Matplotlib log log plot
Horizontal grid lines matplotlib
In matplotlib, the grid() method is used to create grid lines.
The syntax to create horizontal grid lines:
matplotlib.axis.YAxis.grid()
This method is used to draw horizontal lines only.
Let's see an example:
# Import Library import matplotlib.pyplot as plt # Current axes axes = plt.gca() # Horizontal grid lines axes.yaxis.grid() # Display plt.show()
Here we use plt.gca() method to get current axes of the graph and axes.yaxis.grid() method to plot horizontal gridlines.
Read: Matplotlib best fit line
Matplotlib horizontal line with text
To add text on a horizontal line we have to use the text() method.
The syntax is as given below:
# Horizontal line matplotlib.pyplot.axhline(y) # Text matplotlib.pyplot.text(x,y,text)
Let's see an example of a horizontal line with text:
# Import Library import matplotlib.pyplot as plt # Plot horizontal line plt.axhline(y=1.5) # Add text plt.text(0.4,1.51,'Horizontal Line',rotation=360) # Show plt.show()
Read: Matplotlib subplots_adjust
Annotate horizontal line matplotlib
In matplotlib, annotate() method is used to annotate the point with text.
annotate() method works same as text() method, but annotate() method have more features. Like you can add arrows to highlight something.
The syntax of annotate function is as given below:
matplotlib.pyplot.annotate(s, xy, xytext, arrowprops ....)
The parameters used are described below:
- s: text used for annotation.
- xy: specify point (x,y) to annotate.
- xytext: specify the position (x,y) to place the text.
- arrowprops: It contains dict type.
Let's see an example:
# Import Library import matplotlib.pyplot as plt # Plot Horizontal line plt.axhline(y=1.47) # Annotate plt.annotate('Horizontal Line', xy = (0.4,1.48), xytext =(0.6,1.52), color='red', fontweight='bold', fontsize = 15, arrowprops = dict(facecolor = 'yellow')) # Show plt.show()
Here we use the plt.annotate() method and pass text, xy, xytext, color, fontweight, fontsize, and arrowprops as a parameter.
Matplotlib remove horizontal line
In matplotlib, we can easily remove the horizontal lines which are drawn by using the plt.plot() method.
remove() method is used to remove the horizontal line.
Example:
# Import Library import matplotlib.pyplot as plt # Define Data x = [2, 4, 6, 8] y = [0, 0, 0, 0] y1 = [2, 2, 2, 2] y2 = [3, 3, 3, 3] # Plot Horizontal lines line_1 = plt.plot(x,y) line_2 = plt.plot(x,y1) line_3 = plt.plot(x,y2) # Remove horizontal line r = line_2.pop(0) r.remove() # Show plt.show()
Here we use the remove() method to delete horizontal line 2.
Matplotlib add horizontal line to bar chart
If you want to create a horizontal line to bar chart in matplotlib, you must have knowledge of how to create a bar chart. Let's see the syntax to create a bar chart.
matplotlib.pyplot.bar(x, height)
Let's see an example where we draw a horizontal line on the bar chart:
# Import Library import matplotlib.pyplot as plt # Define data x =[2, 4, 8, 5, 6] y = [8, 16, 15, 2, 4] # Creating the bar plot plt.bar(x, y, color ='maroon') # Horizontal Line plt.axhline(y=9.5,linewidth= 3, color='k', linestyle= 'dotted') # Display plt.show()
- In the above example, we import matplotlib.pyplot library for data visualization.
- Next we define data, and by using plt.bar() method we plot bar chart.
- plt.axhline() method is used to draw horizontal line on the bar chart at y=9.5.
In this Python tutorial, we have discussed the"Horizontal line matplotlib" and we have also covered some examples related to it. These are the following topics that we have discussed in this tutorial.
- Horizontal line matplotlib
- Dotted horizontal line matplotlib
- Horizontal line matplotlib with label
- Multiple horizontal line matplotlib
- Matplotlib horizontal line thickness
- Matplotlib horizontal line color
- Matplotlib horizontal line at value
- Matplotlib horizontal line at 0
- Horizontal line matplotlib subplot
- Matplotlib horizontal line full width
- Matplotlib errorbar horizontal line
- Horizontal grid lines matplotlib
- Matplotlib horizontal line with text
- Annotate horizontal line matplotlib
- Matplotlib remove horizontal line
- Matplotlib add horizontal line to bar chart
How to Draw Horizontal Grid Lines Matplotlib
Source: https://pythonguides.com/horizontal-line-matplotlib/
0 Response to "How to Draw Horizontal Grid Lines Matplotlib"
Enregistrer un commentaire