Uncategorized

Plot a Vertical line in Matplotlib in Python


In this tutorial, we will learn about what is the use of vertical lines in the graph and how to install the Matplolib library. We will also see how to draw verticle lines for given data using Matplotlib in Python. Also, we will learn about how to customize vertical lines.

What is the use of vertical lines?

Suppose, we have a graph indicating stock movement in the stock market. We want to set the threshold at a particular value. In this scenario, we can draw a vertical line to show that threshold. If we want to highlight specific values, indicate important boundaries in the graph, or make divisions then we can make use of vertical lines.

How to install the Matplotlib library?

We can install the Matplotlib library using the pip command in Python as follows

pip install Matplotlib

This will install the latest version of Matplotlib in our system.

Draw a vertical line in Matplotlib

Importing required libraries

import matplotlib.pyplot as plt
import numpy as np

In the above code, we are using Matplotlib to plot graphs as well as to draw vertical lines. The Numpy library is useful for handling numerical data.

Creating random data for visualization

np.random.seed(42)  
x = np.random.rand(6)  
y = 2 * x + np.random.randn(6)  

In the above code, we are setting the seed as 42 to ensure the code will generate the same values each time when we run it. Then using random.rand, we are generating random value. The number 6 is the number indicating how many values we want to generate.

Plotting scatterplot with the above values

plt.scatter(x, y)

Output:

Creating random data for visualizationCreating random data for visualization

It will plot a scatterplot for values of x and y.

Draw a vertical line in the scatterplot

To plot the vertical line in the graph, make sure, the original graph (in this case a scatter plot) and the axvline function should be in the same cell.

plt.scatter(x, y) 
plt.axvline(x=0.5)

The above code will plot a vertical line at x=0.5 in the graph.

Output:

Draw a vertical line in the scatterplot - MatplotlibDraw a vertical line in the scatterplot - Matplotlib

How to customize a graph?

We can set line color, and line style also we can give a label to the axis as follows

plt.scatter(x, y) 
plt.axvline(x=0.5, color="r", linestyle="--", label="Vertical Line at x=0.5")
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plotting a Vertical Line in Matplotlib with Random Data')
plt.legend()
plt.show() 

In the above code, the axvline function takes parameters as color (to add color), line style (to set line style), and label (to add a particular label) in the graph. Finally, we are using the xlabel, ylabel, and tittle functions in Matplotlib to add labels to the x-axis, y-axis, and titles to the graph.

Output:

customize a graph - Matplotlibcustomize a graph - Matplotlib



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *