DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

How to Set a Logarithmic X-Axis in Matplotlib (and Use xlim)

Use Matplotlib’s set_xscale("log") to make the x-axis logarithmic. Use xlim separately to set its visible range, knowing fixed limits disable autoscaling.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

xlim does not make a Matplotlib axis logarithmic; it sets the visible x-range. Set the scale with ax.set_xscale("log") or plt.xscale("log"), then use set_xlim or xlim only when you want fixed bounds.

Make the x-axis logarithmic

For an object-oriented plot, call set_xscale("log") on the same Axes that contains 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_xscale("log")
plt.show()

With pyplot’s current-axes workflow, the equivalent call is plt.xscale("log"). Matplotlib’s pyplot.xscale reference describes this function as setting the x-axis scale; it also documents that keyword arguments are passed to the selected scale class.

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

Set the visible range separately with xlim

If you want to restrict the displayed range, set positive lower and upper bounds after choosing the logarithmic scale:

ax.set_xscale("log")
ax.set_xlim(0.1, 1000)

Use plt.xlim(0.1, 1000) instead when working with pyplot’s current axes. For more control, pyplot accepts a pair as plt.xlim((left, right)), or lets you change one side with plt.xlim(left=...) or plt.xlim(right=...). The Matplotlib 3.6.0 xlim reference documents these forms and states that setting limits turns x-axis autoscaling off.

Keep autoscaling or fix the bounds

Leave out xlim or set_xlim if Matplotlib should choose the displayed x-range from the plotted data. Once you set explicit limits, the range is fixed rather than autoscaled; remove that call or update its values when you want a different view.

Use positive values on an ordinary log axis

A conventional logarithmic x-axis requires positive x-values and positive displayed bounds. If your data or bounds include zero or negative values, they are not part of an ordinary log domain. Matplotlib’s older 3.3.4 loglog reference discusses masking or clipping nonpositive values, but that page concerns loglog and is not a current guarantee for xscale. Check the documentation for the Matplotlib version installed in your project before relying on scale-specific options.

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

Troubleshoot a scale or range that seems unchanged

  • The axis still appears linear: Set the scale on the Axes that actually holds the plot, or ensure plt.xscale("log") is called while that axes is current.
  • The range does not follow new data: An explicit xlim or set_xlim call disables x-axis autoscaling. Remove it or set new bounds.
  • Only one edge needs adjustment: In pyplot, supply just left= or right= to plt.xlim.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose scale options for your Matplotlib version

The stable pyplot.xscale documentation is labeled for Matplotlib 3.11.1 and shows scale selection through xscale(value, **kwargs). The linked xlim reference is for Matplotlib 3.6.0, while the cited discussion of nonpositive values is from Matplotlib 3.3.4. For non-default log bases or handling nonpositive values, consult the API documentation matching the version you have installed rather than assuming older option defaults still apply.

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
Crashes, No Sound, or Screen Glitches?Free driver 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.