October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober 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 Invert the Y-Axis in Matplotlib: `set_ylim`, `imshow`, and `twinx`

Use reversed `set_ylim` bounds for a fixed range, `yaxis.set_inverted(True)` to change direction while retaining limits, and check `imshow` origin and extent before flipping an image.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To reverse a Matplotlib y-axis over a known range, call ax.set_ylim(high, low). To reverse its direction without specifying new bounds, use ax.yaxis.set_inverted(True). For an image that looks upside down, first check imshow‘s origin, image extent, and the Axes limits: image row placement and axis direction are related but distinct.

Choose the method that matches what you want to change

Goal Use Effect
Set a known range in reverse order ax.set_ylim(high, low) Sets the bounds and their direction.
Reverse direction while keeping the current limits or autoscaling behavior ax.yaxis.set_inverted(True) Changes the axis inversion state without supplying new endpoints.
Choose whether image rows run top-down or bottom-up imshow(..., origin="upper") or origin="lower" Controls how rows fill the image extent; the extent and Axes limits also affect its screen orientation.
Reverse the right-side y-axis created by twinx() Call set_ylim or set_inverted on the returned twin Axes. Changes that Axes’ independent y-axis.

Reverse a y-axis with set_ylim

When you know the numeric bounds, pass them in descending order. For example, to put 0 at the top and 10 at the bottom:

As an Amazon Associate I earn from qualifying purchases.

ax.set_ylim(10, 0)

This sets both the range and its direction. Matplotlib’s current stable inversion gallery recommends swapping limit values when you are setting explicit bounds. See the Matplotlib Inverted axis example.

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

Invert direction without supplying new bounds

If you want to keep the existing limits or autoscaling behavior and change only the direction, set the inversion state on the y-axis:

ax.yaxis.set_inverted(True)

The Matplotlib 3.11.2 inversion gallery identifies Axis.set_inverted as the method for changing direction without modifying limits. In the current stable API reference, Axes.invert_yaxis() is marked discouraged; use set_inverted(True) for this purpose, or reversed set_ylim when you intend to set explicit bounds. See the inversion gallery and Axes.invert_yaxis API reference.

Fix an upside-down imshow image

imshow‘s origin setting controls how array rows are placed within the image extent. With origin="upper", the first array row is at the top:

ax.imshow(image, origin="upper")

The default is configured by rcParams["image.origin"], whose documented value is 'upper'. The image’s extent maps it into data coordinates, while the Axes limits also influence its displayed orientation. Consequently, an image that appears top-down does not necessarily need an additional y-axis inversion. Check the origin, extent, and limits together before changing direction. The Matplotlib 3.10.7 guide explains these interactions in Origin and extent in imshow.

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

Invert the y-axis of a twinx() plot

twinx() creates a second Axes that shares the x-axis but has its own y-axis, typically shown on the right. Apply the change to the Axes whose y scale you want to reverse:

ax_right = ax_left.twinx()
ax_right.set_ylim(100, 0)  # reverses the right-side y-axis

If the two y scales have different ranges, set each Axes’ range independently. The Matplotlib twinx API reference documents the shared x-axis and independent y-axis.

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

What changes when axes share y

Axes joined with sharey synchronize their view limits. Changing the y limits on one shared Axes therefore changes the others as well; this differs from twinx(), whose y-axes are independent. See Matplotlib’s Shared axis example.

Check the result

  • Use reversed set_ylim endpoints when you want to specify a particular reversed range.
  • Use ax.yaxis.set_inverted(True) when direction should change without supplying new limits.
  • For images, inspect origin, extent, and the Axes limits before applying an additional inversion.
  • For twinx(), make the change on the returned twin Axes if the right-side scale is the one you intend to reverse.

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.

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
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

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.