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 DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

How to Use Matplotlib Log Scales: semilogx, semilogy, and loglog

Choose semilogx, semilogy, or loglog based on which Matplotlib axes need logarithmic spacing. Learn scale methods, positive-value constraints, bases, and ticks.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use semilogx when only x needs logarithmic spacing, semilogy when only y does, and loglog when both axes do. These plotting shortcuts work only for values that can be shown on the chosen logarithmic axes: zero and negative values are not displayed normally, so decide whether to mask or clip them before plotting.

Choose the function that matches your axes

In Matplotlib, the three functions differ by which axis uses a log scale:

As an Amazon Associate I earn from qualifying purchases.

Function Logarithmic axis Typical use
semilogx(x, y) x only Values along x span multiplicative ranges, while y remains on a linear scale.
semilogy(x, y) y only Values along y span multiplicative ranges, while x remains on a linear scale.
loglog(x, y) x and y Both variables are best interpreted with multiplicative spacing.

For example, to display positive measurements on logarithmic x and y axes:

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

x = [1, 10, 100, 1000]
y = [2, 5, 25, 80]

fig, ax = plt.subplots()
ax.loglog(x, y, marker="o")
ax.set_xlabel("x (log scale)")
ax.set_ylabel("y (log scale)")
ax.grid(True, which="both")
plt.show()

Replace ax.loglog(x, y) with ax.semilogx(x, y) or ax.semilogy(x, y) to make just the indicated axis logarithmic. Matplotlib documents these as convenience plotting functions; for instance, ax.semilogx(x, y) is equivalent to setting the x scale to log and plotting the data. Matplotlib’s log-scale examples show the shortcuts and their behavior.

What a logarithmic axis changes

A logarithmic axis spaces positions according to ratios rather than equal differences. On a base-10 axis, the distances from 1 to 10 and from 10 to 100 are equal, even though the numerical differences are 9 and 90. This makes multiplicative change and values spanning several orders of magnitude easier to see, but it also changes how visual distances should be interpreted: equal steps on the axis represent equal ratios, not equal additions.

Choose the scale based on what each variable means and how its values are distributed, rather than applying log scales automatically because a plot looks compressed. Keep an axis linear when equal additive differences are the interpretation you need to preserve.

Handle zero and negative values explicitly

Matplotlib states, “Non-positive values cannot be displayed on a log scale.” A zero or negative value therefore cannot appear at its ordinary position on a logarithmic axis. The documented options are to mask such values so they are ignored, or clip them to a small positive value. The log-scale gallery illustrates that the choice can affect the visible result: masking may make an error bar disappear, while clipping may draw it to the edge of the axes.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Mask values when omitting non-positive observations is appropriate for the plot. The affected points or error-bar portions will not be shown.
  • Clip values only when the display goal justifies it, and make the preprocessing clear. A clipped value is a plotting choice, not a correction to the underlying measurement.

Do not silently substitute an arbitrary small positive number for zero or negative data. That can suggest a value or trend that the measurements do not support.

Set each axis independently

If you want more control over plotting or need different scale options on the two axes, plot normally and set the scales separately:

fig, ax = plt.subplots()
ax.plot(x, y, marker="o")
ax.set_xscale("log")
ax.set_yscale("linear")
ax.set_xlabel("x (log scale)")
ax.set_ylabel("y (linear scale)")
plt.show()

Use ax.set_xscale("log") for a logarithmic x axis and ax.set_yscale("log") for a logarithmic y axis. Leave an axis linear, or set it explicitly to "linear", when logarithmic spacing is not wanted. This axis-by-axis approach is also useful when x and y need different log bases.

The same settings work with an Axes object in multi-panel figures, so each panel can have its own scale choice. The Matplotlib axis-scales guide describes scales as data-to-position transforms and explains how applying a scale also sets appropriate tick locators and formatters.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose a log base and make ticks readable

Base 10 is the default described in Matplotlib’s log-scale guide. Another base, such as 2, can be set with an axis-scale method:

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

When x and y need different bases, set each axis separately with set_xscale and set_yscale. A single loglog call is convenient for putting both axes on log scales, but separate scale settings make independent axis choices explicit.

Log scales configure logarithmic tick placement and formatting defaults. Matplotlib’s ticker API documents LogLocator, which places ticks at multiples of powers of the base, and log formatters such as LogFormatterMathtext and LogFormatterSciNotation. The ticker reference explains how to customize these when the defaults do not communicate the units or intervals clearly. If you specify a formatter and locator manually, keep their bases consistent; the formatter documentation warns that its base should match the locator’s.

Grid lines can help readers follow values across logarithmic intervals. Start with ax.grid(True, which="both") to show grid lines for major and minor ticks, then remove or restyle minor grid lines if they make the figure too dense. Tick and grid choices are presentation decisions: retain the defaults when they are legible, and customize only to make the scale easier to interpret.

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

A quick selection checklist

  • Use semilogx if only x needs logarithmic spacing.
  • Use semilogy if only y needs logarithmic spacing.
  • Use loglog if both axes need logarithmic spacing.
  • Before plotting, check that displayed values on every log axis are positive; choose masking or clipping deliberately if they are not.
  • Keep base 10 if it suits the data, or choose another supported base and configure axes independently when needed.
  • Label axes and scales clearly, and customize ticks or grid lines only when defaults are insufficient.

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
Windows Errors? Fix Them Before They SpreadFree repair scan

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.