Matplotlib Grid

Add grid lines to make your plots easier to read and interpret

📐 Adding Grid Lines

Grid lines help viewers read values more accurately and make your plots look more professional and easier to interpret.


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 8, 7]

plt.plot(x, y, 'o-')
plt.grid(True)  # Add grid
plt.show()
                                    
Major
Grid Lines
Minor
Grid Lines
Custom
Styling

Grid Options

Different ways to display grid lines on your plots:

Basic Grid

Simple on/off grid display

plt.grid(True)

Major Grid

Grid at major tick marks

which='major'

Minor Grid

Finer grid at minor ticks

which='minor'
🎨

Custom Style

Control color, style, and opacity

alpha linestyle

🔹 Basic Grid

Add simple grid lines to your plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 25, 15, 30, 20]

# Plot without grid
plt.subplot(1, 2, 1)
plt.plot(x, y, 'bo-')
plt.title('Without Grid')

# Plot with grid
plt.subplot(1, 2, 2)
plt.plot(x, y, 'bo-')
plt.grid(True)  # Enable grid
plt.title('With Grid')

plt.tight_layout()
plt.show()

🔹 Grid Styling

Customize the appearance of grid lines

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 8, 7]

# Different grid styles
plt.figure(figsize=(12, 4))

# Default grid
plt.subplot(1, 3, 1)
plt.plot(x, y, 'ro-')
plt.grid(True)
plt.title('Default Grid')

# Styled grid
plt.subplot(1, 3, 2)
plt.plot(x, y, 'go-')
plt.grid(True, linestyle='--', alpha=0.7)
plt.title('Dashed Grid')

# Custom colored grid
plt.subplot(1, 3, 3)
plt.plot(x, y, 'bo-')
plt.grid(True, color='red', linestyle=':', alpha=0.5)
plt.title('Colored Grid')

plt.tight_layout()
plt.show()

🔹 Major and Minor Grids

Control different levels of grid detail

import matplotlib.pyplot as plt

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

plt.plot(x, y, 'o-', linewidth=2)

# Enable minor ticks
plt.minorticks_on()

# Major grid
plt.grid(True, which='major', linestyle='-', alpha=0.8)

# Minor grid
plt.grid(True, which='minor', linestyle=':', alpha=0.4)

plt.title('Major and Minor Grid Lines')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()

# Another example with different styling
plt.figure()
plt.plot(x, y, 's-', linewidth=2, markersize=8)

plt.minorticks_on()
plt.grid(True, which='major', color='blue', linestyle='-', alpha=0.6)
plt.grid(True, which='minor', color='gray', linestyle='--', alpha=0.3)

plt.title('Styled Major and Minor Grids')
plt.show()

🔹 Axis-Specific Grids

Control grid lines for specific axes

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 5, 3, 8, 7]

plt.figure(figsize=(12, 4))

# X-axis grid only
plt.subplot(1, 3, 1)
plt.plot(x, y, 'ro-')
plt.grid(True, axis='x')  # Only x-axis grid
plt.title('X-axis Grid Only')

# Y-axis grid only
plt.subplot(1, 3, 2)
plt.plot(x, y, 'go-')
plt.grid(True, axis='y')  # Only y-axis grid
plt.title('Y-axis Grid Only')

# Both axes (default)
plt.subplot(1, 3, 3)
plt.plot(x, y, 'bo-')
plt.grid(True, axis='both')  # Both axes
plt.title('Both Axes Grid')

plt.tight_layout()
plt.show()

🔹 Advanced Grid Customization

Fine-tune grid appearance for professional plots

import matplotlib.pyplot as plt
import numpy as np

# Generate sample data
x = np.linspace(0, 10, 50)
y = np.sin(x)

plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2, label='sin(x)')

# Comprehensive grid customization
plt.grid(True, 
         which='major',      # Major grid lines
         color='gray',       # Grid color
         linestyle='-',      # Solid lines
         linewidth=0.5,      # Line thickness
         alpha=0.7)          # Transparency

# Add minor grid
plt.minorticks_on()
plt.grid(True, 
         which='minor', 
         color='lightgray', 
         linestyle=':', 
         linewidth=0.3, 
         alpha=0.5)

plt.title('Sine Wave with Custom Grid', fontsize=14, fontweight='bold')
plt.xlabel('X values', fontsize=12)
plt.ylabel('Y values', fontsize=12)
plt.legend()

# Set axis limits for better view
plt.xlim(0, 10)
plt.ylim(-1.2, 1.2)

plt.tight_layout()
plt.show()

# Example with different grid patterns
plt.figure(figsize=(8, 6))
plt.plot(x, y, 'r-', linewidth=2)

# Create a subtle grid pattern
plt.grid(True, color='#E0E0E0', linestyle='-', linewidth=1, alpha=0.8)
plt.gca().set_facecolor('#FAFAFA')  # Light background

plt.title('Professional Grid Style')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.show()

🧠 Test Your Knowledge

Which parameter controls grid transparency?

How do you show grid only on the x-axis?