How to independently set horizontal and vertical, major and minor gridlines of a plot?

Matplotlib allows you to independently control horizontal and vertical, major and minor gridlines using the grid() method along with axis-specific methods. This provides fine-grained control over plot appearance.

Basic Syntax

The grid() method can be applied to specific axes:

ax.xaxis.grid(which="major", color='color', linestyle='style', linewidth=width)
ax.yaxis.grid(which="minor", color='color', linestyle='style', linewidth=width)

Parameters

  • which − Specifies "major" or "minor" gridlines
  • color − Sets the gridline color
  • linestyle − Defines line style ('-', '--', '-.', ':')
  • linewidth − Controls line thickness

Complete Example

Here's how to create independent horizontal and vertical gridlines with different styles ?

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np

# Set figure size
plt.rcParams["figure.figsize"] = [8, 5]
plt.rcParams["figure.autolayout"] = True

# Create figure and subplot
fig, ax = plt.subplots()

# Add some sample data
x = np.linspace(0, 2, 100)
y = np.sin(2 * np.pi * x)
ax.plot(x, y, 'b-', linewidth=2, label='sin(2?x)')

# Configure major horizontal gridlines (y-axis)
ax.yaxis.grid(which="major", color='red', linestyle='-', linewidth=2)

# Configure minor vertical gridlines (x-axis)
minor_locator = MultipleLocator(0.1)
ax.xaxis.set_minor_locator(minor_locator)
ax.xaxis.grid(which="minor", color='black', linestyle='-.', linewidth=0.7)

# Add labels and title
ax.set_xlabel('X values')
ax.set_ylabel('Y values')
ax.set_title('Independent Grid Control Example')
ax.legend()

plt.show()

Advanced Grid Configuration

You can also configure both major and minor gridlines for both axes ?

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np

fig, ax = plt.subplots(figsize=(8, 6))

# Sample data
x = np.linspace(0, 3, 50)
y = x**2
ax.plot(x, y, 'g-', linewidth=2)

# Major gridlines for both axes
ax.xaxis.grid(which="major", color='blue', linestyle='-', linewidth=1.5, alpha=0.7)
ax.yaxis.grid(which="major", color='blue', linestyle='-', linewidth=1.5, alpha=0.7)

# Minor gridlines for both axes
x_minor = MultipleLocator(0.2)
y_minor = MultipleLocator(0.5)

ax.xaxis.set_minor_locator(x_minor)
ax.yaxis.set_minor_locator(y_minor)

ax.xaxis.grid(which="minor", color='gray', linestyle=':', linewidth=0.5, alpha=0.5)
ax.yaxis.grid(which="minor", color='gray', linestyle=':', linewidth=0.5, alpha=0.5)

ax.set_xlabel('X values')
ax.set_ylabel('Y² values')
ax.set_title('Complete Grid Configuration')

plt.show()

Grid Style Options

Parameter Options Description
linestyle '-', '--', '-.', ':' Solid, dashed, dash-dot, dotted
which 'major', 'minor', 'both' Gridline type
alpha 0.0 to 1.0 Transparency level

Conclusion

Use ax.xaxis.grid() and ax.yaxis.grid() to independently control horizontal and vertical gridlines. Combine with MultipleLocator for precise minor grid positioning and customize appearance with color, linestyle, and linewidth parameters.

Updated on: 2026-03-25T22:57:13+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements