SwadhyAI Notes

All Python notes

Matplotlib Grid πŸ•ΈοΈ

Grid Lines

Grid lines help the viewer estimate the values of data points. Use the grid() function to add them.

✨ Adding a Grid

python
import matplotlib.pyplot as plt
import numpy as np

x = np.array([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

plt.plot(x, y)
plt.grid() # Adds grid lines
plt.show()

πŸ“ Axis Selection

You can specify which grid lines to display using the axis parameter ('x', 'y', or 'both').

python
plt.grid(axis = 'x') # Only vertical lines

🎨 Customizing Grid

You can change the line properties of the grid (color, style, width).

python
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)

πŸ’‘ Did You Know?

Grid lines are drawn behind the plot elements by default.