Sunday, 29 October 2023

How to Add Vertical Lines to Plots in Python with Matplotlib?

Using 'axvline' method, we can draw a vertical line on the plot.

Example

plot.axvline(x=100, color='red', label=f'Vertical Line at x={100}')

 

Above statement draws a vertical line at x=100, and applies red color to it.

 

Find the below working application.

 

vertical_line.py

import matplotlib.pyplot as plot
import numpy as np

# Sample data
x = np.arange(1, 100)
y = 10 * x + 123

# Plot the data points
plot.plot(x, y, label='y=10x+123')

median = np.median(x)

plot.axvline(x=median, color='red', label=f'Vertical Line at x={median}')
text = f'This is x={median}'
plot.text(median+15, 1000, text, color='red', fontsize=12, ha='center')

# Set labels and legend
plot.legend()

# Show the plot
plot.show()

 

Output

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment