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.