October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Show Horizontal Grid Lines in Matplotlib and Control Whether They Sit Behind Your Data

Use ax.grid(axis="y") for horizontal gridlines in Matplotlib, then set_axisbelow to place them behind bars, behind lines, or above everything.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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, lines, and markers, call ax.set_axisbelow() with True, "line", or False. Both calls are part of the Matplotlib Axes API, and the sections below cover styling, tick groups, layering modes, and version differences.

Draw horizontal grid lines with Axes.grid

Horizontal grid lines are the gridlines that run along the y-axis, one at each y-tick. Matplotlib exposes them through the axis argument of Axes.grid. Passing axis="y" limits the grid to the y-axis, so vertical lines are not drawn.

As an Amazon Associate I earn from qualifying purchases.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.bar(["Q1", "Q2", "Q3", "Q4"], [12, 18, 9, 15])
ax.grid(axis="y")
plt.show()

Three arguments control which gridlines appear and how they look:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • axis chooses the direction. "y" gives horizontal lines, "x" gives vertical lines, and "both" draws both.
  • which chooses the tick group. "major" uses major ticks, "minor" uses minor ticks, and "both" uses both groups. Minor gridlines only appear if the axis has minor ticks, which you add with a locator such as ax.yaxis.set_minor_locator().
  • Line properties such as color, linestyle, and linewidth style the lines. They follow the same names as Line2D properties.

A light, dashed horizontal grid for a chart with both tick groups looks like this:

#1 Best Overall
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
ax.grid(axis="y", which="both", color="0.85", linestyle="--", linewidth=0.7)

The value "0.85" is a grey shade on the 0 (black) to 1 (white) scale, which keeps the grid readable without competing with the data.

Put gridlines behind or in front of plotted data

Gridlines are drawn as part of the axis, so their layer is governed by the axis z-order. Axes.set_axisbelow sets that layer for the ticks and gridlines together. Its documentation describes it as setting “whether axis ticks and gridlines are above or below most artists.” It does not control individual gridlines separately.

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

The method accepts three values. Each maps to a fixed z-order:

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.
Value passed to set_axisbelow Gridlines and ticks drawn above Gridlines and ticks drawn below Z-order of the axis
True Nothing in the Axes Patches (bars, spans, filled areas) and lines and markers 0.5
"line" Patches Lines and markers 1.5
False Patches and lines Nothing in the Axes 2.5

The default z-order of patches is 1 and of lines and markers is 2, which explains the middle row. In "line" mode the grid sits between those two layers, so it stays visible over a bar but does not cross a line or marker.

Rank #3
Sale
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.

Choose the mode by the effect you want:

  • True is the usual choice for bar charts and shaded regions. The grid recedes behind both fills and data lines.
  • "line" keeps the grid visible through bars while leaving lines and markers clean. This is the Matplotlib default.
  • False places the grid over everything, which is useful only when the gridlines are meant to read as an overlay, such as a reference scale on a heatmap-style figure. Expect it to reduce the contrast of the data beneath it.

Set the mode once per Axes, after plotting or before, since it applies to the whole axis:

ax.set_axisbelow(True)

If none of the three modes gives the layering you need, the Axes.grid documentation points to setting the z-order on the axis itself, which gives finer control than the three named modes.

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
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Defaults and version differences

Matplotlib’s configuration documentation lists the rcParams defaults that govern these settings:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • axes.grid is False, so no grid appears unless you request one.
  • axes.grid.axis is "both", so a grid request without an axis argument covers both directions.
  • axes.grid.which is "major".
  • axes.axisbelow is "line", which is why gridlines appear over bars by default.

The method pages cited here were versioned differently when checked. The Axes.grid page and the Axes API index were in the 3.11.2 stable documentation, and the set_axisbelow page was in the 3.10.0 documentation. The arguments and z-order values described above are the ones in those pages. If your installed version differs, check matplotlib.__version__ and the matching documentation before relying on exact behavior.

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.

In practice, the most reliable way to match a chart to a version is to set the grid and layering explicitly in code rather than rely on the rcParams defaults, since a shared stylesheet or a changed axes.axisbelow value will change the result without any change to the plotting call.

“

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.