Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

To show horizontal grid lines in Matplotlib, call ax.grid(axis="y") on your Axes object. To control whether those lines sit behind or in front of your bars, areas and lines, call ax.set_axisbelow() with True, "line" or False. The combined pattern below handles both steps:

ax.grid(visible=True, axis="y", which="major",
        color="0.8", linestyle="--", linewidth=0.8)
ax.set_axisbelow(True)

The rest of this guide explains each argument, the three layering modes, and how to fix the most common cases where gridlines do not appear the way you expect.

Draw only horizontal gridlines with axis="y"

Horizontal gridlines are the gridlines that run across the y-axis, so you select them with the axis argument of Axes.grid. Passing axis="y" restricts the grid to that axis, which leaves vertical lines off the chart. The Axes.grid reference documents the full signature.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4], [3, 7, 5, 9])
ax.grid(axis="y")
plt.show()

The three values accepted by axis are "x", "y" and "both". Use "y" for horizontal lines only, "x" for vertical lines only, or "both" for a full grid.

#1 Best Overall
Sale
Philips 24 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 241V8LB
  • CRISP CLARITY: This 23.8″ Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
  • INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
  • THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors
  • WORK SEAMLESSLY: This sleek monitor is virtually bezel-free on three sides, so the screen looks even bigger for the viewer. This minimalistic design also allows for seamless multi-monitor setups that enhance your workflow and boost productivity
  • A BETTER READING EXPERIENCE: For busy office workers, EasyRead mode provides a more paper-like experience for when viewing lengthy documents

Choose which tick group gets gridlines

The which argument decides whether gridlines follow major ticks, minor ticks or both. The three options are:

Value Gridlines drawn at What you need
"major" Major tick positions only Nothing extra; this is the default in the configuration reference
"minor" Minor tick positions only Minor ticks must exist, or nothing is drawn
"both" Major and minor tick positions Minor ticks must exist for the minor lines to appear

If you request minor gridlines on a plot without minor ticks, call ax.minorticks_on() first. Minor gridlines are usually lighter than major ones, so pair them with a lower-contrast color such as "0.9".

ax.minorticks_on()
ax.grid(axis="y", which="both", color="0.9", linewidth=0.5)
ax.grid(axis="y", which="major", color="0.7", linewidth=0.9)

Style the horizontal gridlines

Gridlines accept Line2D properties, so the familiar line settings work here. The three you will use most are:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Philips 22 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 221V8LB
  • CRISP CLARITY: This 22 inch class (21.5″ viewable) Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
  • 100HZ FAST REFRESH RATE: 100Hz brings your favorite movies and video games to life. Stream, binge, and play effortlessly
  • SMOOTH ACTION WITH ADAPTIVE-SYNC: Adaptive-Sync technology ensures fluid action sequences and rapid response time. Every frame will be rendered smoothly with crystal clarity and without stutter
  • INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
  • THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors
  • color: a name, a hex value or a grey level string such as "0.8". Lighter values keep the grid from competing with the data.
  • linestyle: for example "--" for dashed or ":" for dotted.
  • linewidth: a value in points. Values near 0.5 to 1.0 suit most charts.

These properties are passed directly to ax.grid() as keyword arguments, as shown in the opening example.

Decide whether gridlines sit above or below the data

Axes.set_axisbelow controls the drawing order of the axis, which includes its ticks and gridlines. The reference page opens with the sentence: “Set whether axis ticks and gridlines are above or below most artists.” (Axes.set_axisbelow reference, Matplotlib 3.10.0).

Because ticks and gridlines move together, set_axisbelow is a layering switch for the whole axis, not a setting for one gridline at a time. It accepts three values:

Rank #3
Dell 24 Monitor - SE2426H - 23.8-inch FHD (1920x1080) 144Hz 1ms Display, in-Plane Switching (IPS) Technology, AMD FreeSync™, TÜV 3-Star 2X HDMI, Tilt
  • Clear visuals. Fluid motion: A 144Hz refresh rate and 1ms MPRT deliver smooth, tear‑free motion across work, gaming, and streaming for clearer, more fluid viewing.
  • Eye comfort: TÜV Rheinland 3‑star* certification reduces harmful blue light while preserving stunning color quality without compromise. *TÜV Rheinland 3-star eye comfort certification.
  • Wide viewing angle: Get consistent views across a wide 178° /178° viewing angle.
  • In-Plane Switching (IPS): See excellent color accuracy and consistency across wide viewing angles with In-plane Switching (IPS) technology.
  • Ultra-thin bezels: Maximize your viewing experience with thin bezels.
Value Z-order Relative to patches (bars, spans, filled areas) Relative to lines and markers
True 0.5 Below Below
"line" 1.5 Above Below
False 2.5 Above Above

The default is "line". Patches have a default z-order of 1 and lines have a default z-order of 2, so with the default setting a bar chart shows its gridlines over the bars. That is often why people search for a way to put grid lines behind a plot.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Put gridlines behind a bar chart

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar(["Jan", "Feb", "Mar", "Apr"], [120, 95, 140, 110], color="steelblue")
ax.grid(axis="y", color="0.85")
ax.set_axisbelow(True)   # gridlines now sit behind the bars
plt.show()

Without the set_axisbelow(True) line, the gridlines cross the blue bars. With it, the bars hide the grid where they overlap.

Bring gridlines in front of everything

ax.set_axisbelow(False)

Use this when the grid should stay visible over both filled areas and plotted lines, for example as a reference scale on a dense chart. Keep the grid light so it does not hide data.

Rank #4
Sale
Samsung 27" Essential S3 (S36GD) Series FHD 1800R Curved Computer Monitor
  • CURVED FOR ENHANCED ENGAGEMENT: An immersive viewing experience with a curved monitor that wraps more closely around your field of vision; It creates a wider view, enhancing depth perception and minimizing peripheral distraction
  • SMOOTH PERFORMANCE FOR SEAMLESS CONTENT: Stay in the action when playing games, watching videos, or working on creative projects; The 100Hz refresh rate reduces lag and motion blur so you don't miss a thing in fast-paced moments¹
  • MORE GAMING POWER: Gain the edge with optimizable game settings; Color and image contrast can be adjusted to see scenes more vividly and spot enemies hiding in the dark; Game Mode adjusts any game to fill the screen so you can view every detail²
  • KEEP IT EASY ON THE EYES: Care for your eyes and stay comfortable, even during long sessions; Advanced eye comfort technology certified by TÜV reduces eye strain by minimizing blue light and reducing irritating screen flicker²
  • INCREASED VERSATILITY: Connect to more; Plug devices straight into your monitor for increased flexibility, making your computing environment even more convenient

Keep gridlines above fills but below lines

ax.set_axisbelow("line")

This is the default. It suits most line charts with light filled areas, because the fills stay clean while the lines and markers stay on top.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Set the behaviour for every plot with rcParams

If you want the same grid settings across a notebook or script, set matplotlib’s runtime configuration (rcParams) before you create figures. The configuration reference lists the keys. The grid-related defaults are:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • axes.grid: False by default, so no gridlines are drawn.
  • axes.grid.axis: "both" by default.
  • axes.grid.which: "major" by default.
  • axes.axisbelow: "line" by default.
import matplotlib.pyplot as plt

plt.rcParams["axes.grid"] = True
plt.rcParams["axes.grid.axis"] = "y"
plt.rcParams["axes.grid.which"] = "major"
plt.rcParams["axes.axisbelow"] = True

Set these values before you call plt.subplots(). Changing them afterwards does not restyle Axes that already exist.

Best Value
Sale
Sceptre New 22-Inch Gaming Monitor, FHD 1080p, Up to 144Hz, HDMI, DisplayPort, Built-in Speakers, Machine Black (E225W-FW144 Series, 2026)
  • 【INTEGRATED SPEAKERS】Whether you're at work or in the midst of an intense gaming session, our built-in speakers provide rich and seamless audio, all while keeping your desk clutter-free.
  • 【EASY ON THE EYES】 Protect your eyes and enhance your comfort with Blue-Light Shift technology. This feature reduces harmful blue light emissions from your screen, helping to alleviate eye strain during long hours of use and promoting healthier viewing habits.
  • 【WIDEN YOUR PERSPECTIVE】Our sleek minimal bezel design ensures undivided attention. The nearly bezel-free display seamlessly connects in a dual monitor arrangement, delivering an unobstructed view that lets you focus on more at once, completely distraction-free.

Check your Matplotlib version

The reference pages above are versioned. The stable Axes.grid page and the Axes API index reflect Matplotlib 3.11.2, while the set_axisbelow page linked here is the 3.10.0 version. For a version-specific answer, check your installed release with import matplotlib; print(matplotlib.__version__) and read the matching documentation.

Troubleshooting common gridline problems

  • No gridlines appear. Confirm that ax.grid(axis="y") runs after your plotting calls, and that no later call turns the grid off. Check axes.grid in your rcParams if you expect the grid by default.
  • Gridlines cross bars or filled areas. Call ax.set_axisbelow(True). This is the default-behaviour fix shown in the bar chart example.
  • Gridlines are hidden behind lines or markers you want to see over them. Use ax.set_axisbelow(True) or keep the default "line" mode, which places lines and markers above the grid.
  • Minor gridlines do not show. The plot has no minor ticks. Call ax.minorticks_on() before setting which="minor" or which="both".
  • Changing the grid on one Axes affects another. Check whether you set rcParams globally. Settings made through ax.grid() apply only to that Axes.

When the three layering modes are not enough, the Axes API index is the place to look for lower-level control of the axis z-order.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.