Adding Horizontal Lines in Matplotlib

Horizontal lines are often used in plots to indicate specific y-values, such as thresholds, averages, or baseline values. The `axhline` and `hlines` functions in Matplotlib make it easy to add and customize horizontal lines in your plots.

This tutorial will guide you through various ways to use and customize horizontal lines using the `matplotlib` library.

### Basic Horizontal Line with `axhline`

The `axhline` function can be used to add a horizontal line across the entire width of a plot.

import matplotlib.pyplot as plt

# Sample data
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]

plt.plot(x, y, label='Quadratic')
plt.axhline(y=10, color='r', linestyle='--', label='y=10')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Basic Horizontal Line')
plt.legend()

plt.show()
- **`axhline(y=10, color='r', linestyle='--', label='y=10')`** adds a red dashed horizontal line at `y=10`.

### Adding Multiple Horizontal Lines

You can add multiple horizontal lines to indicate different y-values.

import matplotlib.pyplot as plt

# Sample data
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]

plt.plot(x, y, label='Quadratic')
plt.axhline(y=5, color='g', linestyle='-', label='y=5')
plt.axhline(y=15, color='b', linestyle=':', label='y=15')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Multiple Horizontal Lines')
plt.legend()

plt.show()
- **`axhline(y=5, color='g', linestyle='-', label='y=5')`** adds a green solid horizontal line at `y=5`.
- **`axhline(y=15, color='b', linestyle=':', label='y=15')`** adds a blue dotted horizontal line at `y=15`.

### Horizontal Line with Limited X-Range using `hlines`

The `hlines` function allows you to add horizontal lines over a specified range of x-values.

import matplotlib.pyplot as plt

# Sample data
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]

plt.plot(x, y, label='Quadratic')
plt.hlines(y=10, xmin=1, xmax=4, color='purple', linestyle='--', label='y=10 from x=1 to x=4')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Horizontal Line with Limited X-Range')
plt.legend()

plt.show()
- **`hlines(y=10, xmin=1, xmax=4, color='purple', linestyle='--', label='y=10 from x=1 to x=4')`** adds a purple dashed horizontal line at `y=10` only from `x=1` to `x=4`.

### Adding Horizontal Span using `axhspan`

The `axhspan` function can be used to add a shaded region between two horizontal lines, providing a visual cue for a range of y-values.

import matplotlib.pyplot as plt

# Sample data
x = [0, 1, 2, 3, 4, 5]
y = [0, 1, 4, 9, 16, 25]

plt.plot(x, y, label='Quadratic')
plt.axhline(y=10, color='r', linestyle='--', label='y=10')
plt.axhspan(5, 15, color='gray', alpha=0.3, label='y-span from 5 to 15')

plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Horizontal Span with axhspan')
plt.legend()

plt.show()
- **`axhspan(5, 15, color='gray', alpha=0.3, label='y-span from 5 to 15')`** adds a gray shaded region between `y=5` and `y=15` with transparency set to 0.3.

### Example: Highlighting Mean and Standard Deviation

Here’s an example illustrating how to use horizontal lines to highlight the mean and standard deviation of a dataset.

import matplotlib.pyplot as plt
import numpy as np

# Sample data
np.random.seed(0)
data = np.random.randn(100)

mean = np.mean(data)
std = np.std(data)

plt.plot(data, label='Data')
plt.axhline(mean, color='blue', linestyle='--', linewidth=2, label=f'Mean: {mean:.2f}')
plt.axhline(mean + std, color='green', linestyle=':', label=f'Mean + 1 SD: {mean + std:.2f}')
plt.axhline(mean - std, color='green', linestyle=':', label=f'Mean - 1 SD: {mean - std:.2f}')

plt.xlabel('Sample index')
plt.ylabel('Value')
plt.title('Highlighting Mean and Standard Deviation')
plt.legend()

plt.show()
- **`axhline(mean, color='blue', linestyle='--', linewidth=2, label=f'Mean: {mean:.2f}')`** adds a blue dashed horizontal line at the mean value.
- **`axhline(mean + std, color='green', linestyle=':', label=f'Mean + 1 SD: {mean + std:.2f}')`** adds a green dotted horizontal line at one standard deviation above the mean.
- **`axhline(mean - std, color='green', linestyle=':', label=f'Mean - 1 SD: {mean - std:.2f}')`** adds a green dotted horizontal line at one standard deviation below the mean.