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.
Let’s create a matplotlib vlines graph in the following steps.
Firstly, We need to import matplotlib.pyplot for using vlines() function.
import matplotlib.pyplot as plt
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.
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.
We can draw multiple vline in a chart. Here is the way to achieve it.
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()
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.
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