Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober 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 Now×
Skip to content
World desk2 min

How to Set the Y-Axis Range in Matplotlib Bar Charts

Use ax.set_ylim(bottom, top) after plotting to control a Matplotlib bar chart’s y-axis range. Learn one-sided limits, pyplot syntax, autoscaling, and inversion.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set a Matplotlib bar chart’s y-axis range with ax.set_ylim(bottom, top) after plotting. For example, ax.set_ylim(0, 100) displays a range from 0 to 100; choose bounds that fit your data.

Set both y-axis limits with Axes.set_ylim()

The object-oriented Matplotlib method sets the y-axis view limits in data coordinates:

As an Amazon Associate I earn from qualifying purchases.

import matplotlib.pyplot as plt

labels = ["A", "B", "C"]
values = [25, 60, 45]

fig, ax = plt.subplots()
ax.bar(labels, values)
ax.set_ylim(0, 80)
plt.show()

Here, the chart displays values from 0 through 80. The chosen bounds are examples, not recommended limits for every dataset. Matplotlib’s Axes.set_ylim API documents the method and its arguments.

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

Set only one bound, or use pyplot

Change just the upper or lower limit

Pass a named argument to change one bound while leaving the other unchanged. Passing None has the same effect for that bound.

ax.set_ylim(top=80)       # Keep the current lower limit
ax.set_ylim(bottom=0)    # Keep the current upper limit
ax.set_ylim(bottom=None, top=80)

Use plt.ylim() in pyplot-style code

The pyplot function sets limits on the current Axes, so it fits a simple chart when you are not working with a retained Axes variable:

plt.bar(labels, values)
plt.ylim(0, 80)

For a figure with multiple subplots, prefer ax.set_ylim(...) to make clear which chart is being changed. The plt.ylim API describes the pyplot equivalent and its behavior when called without arguments.

Check the current limits and handle autoscaling

Read the current bounds with ax.get_ylim(). In pyplot style, call plt.ylim() without arguments to inspect the current Axes’ limits. Matplotlib normally derives view limits from the data and applies margins; its current stable autoscaling guide documents a default margin of 5%. The autoscaling guide explains how view limits and data limits interact.

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

Setting limits explicitly fixes the displayed range and turns y-axis autoscaling off by default. If you add more bars or other artists later, the view may not expand to include them. To fit the data again, use the Axes autoscaling controls, such as ax.autoscale() or ax.autoscale_view(), as appropriate to the Axes state. See also the APIs for ax.get_ylim() and ax.autoscale_view().

Reverse the y-axis intentionally

Limits do not have to be in ascending order. Setting the bottom value higher than the top reverses the axis, so values decrease from bottom to top:

ax.set_ylim(80, 0)

Use this only when an inverted y-axis is intended; the usual upward-increasing orientation uses ascending limits.

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

Version note

The linked API pages use Matplotlib’s /stable/ documentation, which can change as releases advance. If your project pins an older Matplotlib version, consult documentation matching that installation.

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

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 *

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.

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.