Matplotlib

Matplotlib vlines Example : Implementation Guide

Matplotlib vlines example with vlines() function of matplotlib.pyplot module. The vlines() function in Matplotlib draws vertical lines across the axes. In this article, We will take some examples of vlines() implementation. We will draw these lines in steps.

Matplotlib vlines example  Stepwise-

Let’s create a matplotlib vlines graph in the following steps.

Step 1: Import packages-

Firstly, We need to import matplotlib.pyplot for using vlines() function.

import matplotlib.pyplot as plt 

Step 2: Plotting vlines-

Secondly, Here is the syntax for drawing vlines().

vlines(x_coordinate, y_axis_min, y_axis_max, colors, linestyles)
plt.vlines(7, 0, 8, linestyles ="solid", colors ="red")
plt.xlim(0, 10) 
plt.ylim(0, 10) 
plt.show()

In the above example, We have used –
x_coordinate=7
y_axis_min=0
y_axis_max=8
linestyles =”solid”
colors =”red”

Here xlim and ylim are representing the scale.And plt.show() draws the chart.

Complete Code –

Let’s merge the code from each step.

import matplotlib.pyplot as plt
plt.vlines(7, 0, 8, linestyles ="solid", colors ="red")
plt.xlim(0, 10) 
plt.ylim(0, 10) 
plt.show()

Here is the output.

matplotlib vlines example

Matplotlib vlines example ( Multiple)-

We can draw multiple vline in a chart. Here is the way to achieve it.

 

Method 1: Separate adding lines in Multiple statements –

import matplotlib.pyplot as plt
plt.vlines(2, 0, 8, linestyles ="solid", colors ="red")
plt.vlines(7, 0, 8, linestyles ="dashed", colors ="blue")
plt.xlim(0, 15) 
plt.ylim(0, 15) 
plt.show()
matplotlib vlines multiple 1

 

Method 2: adding lines in single statement –

In the above example, We have draw lines in multiple statement. In this section we will pass the parameter together.

import matplotlib.pyplot as plt 
plt.vlines((3, 4, 7,), 0, 10, colors = ("b", "r", "g"), 
         linestyles = ("dotted", "dashed","solid")) 
  
plt.show() 

Further, with the above example, We have drawn three lines in the chart. Here we have used –

x_coordinate=(3, 4, 7,)
y_min=0
y_max=10
colors=(“b”, “r”, “g”)
linestyles = (“dotted”, “dashed”,”solid”)

y_min and y_max is a sharing variable.

matplotlib vlines multiple 2

 

Note –

  1. For Line style, We have most importantly four values {‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’}. In the above examples, we have already demonstrated three {‘solid’, ‘dashed’,  ‘dotted’}.
  2. Also, colors are in variety in the Matplotlib library. We can pick any of them.

Conclusion –

vlines are really helpful in drawing labels. It gives an explanatory view of charts. Well, I hope this article must have cleared all steps to create vlines in the chart.  If you have any suggestions on this topic,  Please comment below.

 

 

Thanks

Data Science Learner Team