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

Make Matplotlib’s Y-Axis Logarithmic—and Handle Zeros Correctly

Use ax.set_yscale('log') for a logarithmic y-axis, set base as needed, and switch to symlog when meaningful data cross zero.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For an existing Matplotlib axes, call ax.set_yscale('log') to make its y-axis logarithmic. The default base is 10; choose another with base=. A standard log scale cannot show zero or negative measurements as themselves. If your data crosses zero, use symlog with a linear region controlled by linthresh, or choose a different scale if that better represents the data.

Set the y-axis to a logarithmic scale

Use the object-oriented Axes method after creating the plot:

As an Amazon Associate I earn from qualifying purchases.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_yscale('log')
plt.show()

set_yscale applies the scale to the y-axis, including its data-to-position transform and scale-appropriate tick locators and formatters. See Matplotlib’s Axes.set_yscale API and axis scales guide.

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.

Choose a logarithm base

The ordinary log scale defaults to base 10. Pass another base when that suits the data or the way you want to interpret equal intervals on the axis:

ax.set_yscale('log', base=2)

For example, a base-2 axis is useful when powers of two are meaningful to the reader. Changing the base changes the logarithmic scale and its tick spacing; it does not change the underlying measurements. Matplotlib’s log-scale example documents the default base and the base parameter.

What happens to zero and negative values?

A real logarithm is undefined at zero and for negative values, so an ordinary log axis cannot display those measurements at their actual values. Matplotlib provides two ways to handle non-positive values, selected with nonpositive:

  • ax.set_yscale('log', nonpositive='mask') masks non-positive values. Plot segments or artists that rely on those values may be omitted.
  • ax.set_yscale('log', nonpositive='clip') clips them to a small positive value. This can keep some elements, such as an error bar extending below zero, visible near the lower plot edge, but it changes their visual representation.

Clipping does not make zero or a negative measurement mathematically valid on a log axis. Matplotlib’s log-scale example illustrates these behaviors with error bars; choose the handling that accurately communicates what the data show.

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

Use symlog when values cross zero

The symlog scale supports negative and positive values by using a linear region around zero and logarithmic scaling farther from it:

ax.set_yscale('symlog', linthresh=1)

linthresh is in the units of your data. Set it to the range around zero where you need linear resolution. Matplotlib suggests a threshold near the smallest absolute data value as a rule of thumb, often leaving no more than a few points in the linear region; the suitable choice depends on the data and chart. The transition between linear and logarithmic regions has a gradient discontinuity, so the apparent rate of change can shift at the boundary.

The optional linscale parameter changes how much visual space the linear region occupies. Adjust it deliberately: the displayed distances are part of how readers interpret the values. See Matplotlib’s symlog guide for the scale’s behavior and parameters.

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

Choose the scale that fits the measurements

Scale Values around zero Controls and considerations
log Non-positive values cannot be shown as themselves. base sets the logarithm base; nonpositive='mask' or 'clip' controls treatment of non-positive values. Use when values are positive and logarithmic distances make sense.
symlog Supports negative and positive values with a linear band around zero. linthresh sets the linear region; linscale adjusts its visual width, and base sets the logarithmic base. The linear-to-log transition has a gradient discontinuity.
asinh Matplotlib presents it as an alternative for wide-ranging values with a smooth gradient. linear_width controls the scale. Consider it when a smooth transition is desirable, and make sure the transform communicates the data appropriately.

These scales make different visual trade-offs; none is universally best. Matplotlib discusses them in its axis scales guide.

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

Check the result against your data

  • Confirm whether zero and negative values are meaningful observations or invalid entries for the chart’s purpose.
  • If using ordinary log, inspect the plotted range and any masked or clipped artists so the treatment is not mistaken for the original measurement.
  • If using symlog, choose linthresh in the data’s units and check how the linear band and transition affect comparisons near zero.
  • Tick appearance and defaults can vary by Matplotlib release. These links point to the stable documentation available on October 7, 2026; your installed version may differ.

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
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.