logo

Drawing Vertical and Horizontal Lines in Python matplotlib 📂Programing

Drawing Vertical and Horizontal Lines in Python matplotlib

CODE1 2

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,2*np.pi,num=1000)
y = np.sin(x)

plt.plot(x,y)
plt.show()

1.PNG

  • axhline(y=0, xmin=0, xmax=1, **kwargs)
  • axvline(x=0, ymin=0, ymax=1, **kwargs)

Horizontal lines can be added with plt.axhline(), and vertical lines with plt.axvline(). The min/max values for the range to draw these lines are not real values but should be entered as a ratio between 0 and 1. The major options include:

  • color or c
  • linestyle or ls
  • linewidth or lw
  • marker
plt.plot(x,y)
plt.axhline(y=0.5, xmax=0.5, color='r', linestyle='-')
plt.axhline(y=0.25, xmax=0.75, c='g', linestyle='--')
plt.axhline(y=-0.2, xmax=1, color='tomato', linestyle=':', linewidth = 2)
plt.axhline(y=-0.3, xmax=1, color='tomato', linestyle=':', linewidth = 3)
plt.axhline(y=-0.5, xmax=1, color='darkviolet', linestyle='-.', linewidth = 1)
plt.axhline(y=-0.6, xmax=1, color='darkviolet', linestyle='-.', linewidth = 3)
plt.show()

2.PNG

plt.plot(x,y)
plt.axvline(x=0.5, ymax=0.5, color='r', linestyle='-')
plt.axvline(x=1, ymax=0.75, c='g', linestyle='--', linewidth = 3)
plt.axvline(x=2, ymax=1, color='tomato', linestyle=':', linewidth = 2)
plt.axvline(x=3, ymax=1, color='tomato', linestyle=':', linewidth = 10)
plt.axvline(x=4, ymax=1, color='darkviolet', linestyle='-.', linewidth = 1)
plt.axvline(x=5, ymax=1, color='darkviolet', linestyle='-.', linewidth = 3)
plt.imshow()

3.PNG

ENVIRONMENT

  • OS: Windows10
  • Version: Python 3.7.12, matplotlib 3.2.2

IN OTHER LANGUAGES