Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use semilogx when only x should be logarithmic, semilogy when only y should be logarithmic, and loglog when both axes should be logarithmic. These functions are shortcuts for plotting with the corresponding axis scale. A log axis requires positive values: Matplotlib states that “Non-positive values cannot be displayed on a log scale.”
Choose the function that matches the axes
A logarithmic axis spaces values according to their ratios rather than their differences. On a base-10 axis, for example, the distance from 1 to 10 is the same as the distance from 10 to 100. This can make data spanning several orders of magnitude easier to inspect, but it changes how distances on that axis should be interpreted.
| Function | Logarithmic axis | Typical use |
|---|---|---|
ax.semilogx(x, y) |
x only | Compare x values across multiplicative ranges while keeping y linearly spaced. |
ax.semilogy(x, y) |
y only | Show y values spanning multiplicative ranges against a linear x-axis. |
ax.loglog(x, y) |
x and y | Show multiplicative ranges on both axes. |
These are convenience methods on an Axes object. The equivalent approach is to plot normally and set the scale of each axis you want to change:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(x, y, marker="o")
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel("x (log scale)")
ax.set_ylabel("y (log scale)")
ax.grid(True, which="both")
Set only the x scale for a semilog-x plot, only the y scale for semilog-y, or both for a log-log plot. This axis-by-axis method is useful when the two axes need different settings. Matplotlib documents the convenience methods and scale approach in its log-scale gallery and axis-scales guide.
#1 Best Overall
Check the data domain before plotting
Values on a logarithmic axis must be positive. Zero and negative values do not have ordinary positions on that scale; switching an axis to log does not make them valid. Matplotlib’s log-scale example discusses two ways to handle non-positive values: mask them so they are ignored, or clip them to a small positive value.
- Mask values when excluding them is appropriate for the visualization. A masked point is omitted; gaps in a line or error bars may therefore disappear.
- Clip values only when you have a defensible display rule. Clipping can put a point or error-bar extent at the edge of the plot, but it does not correct the underlying measurement.
Do not silently replace zero or negative measurements with an arbitrary epsilon. If you preprocess data, make the choice visible in the code and explain the effect in the figure or its caption. See the official log-scale examples for the masking and clipping behavior.
Rank #2
Set the logarithm base when needed
Matplotlib’s documented default log base is 10. You can choose another base, such as 2, by setting the scale directly:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsax.set_yscale("log", base=2)
When both axes need logarithmic scaling but different bases, set each axis separately with set_xscale and set_yscale. That makes the choice for each axis explicit rather than relying on a single convenience call. The base changes tick positions and labels; it does not change which values are positive or the underlying data.
Read and customize log-axis ticks
Applying a log scale also selects logarithmic tick-location and formatting defaults. The axis-scales guide describes defaults that include LogLocator and a logarithmic formatter, with scientific notation on decades. Start with these defaults and adjust them only if the resulting labels or intervals are hard to read.
LogLocator places ticks at multiples of powers of its base; its subs setting can add locations between those powers. Matplotlib also provides log formatters including LogFormatterMathtext and LogFormatterSciNotation. If assigning a locator and formatter manually, keep their bases consistent: the formatter documentation warns that its base should match the base used by the locator.
For a plot where both major and minor log ticks help communicate scale, enable both grid levels:
ax.grid(True, which="both")
Minor grid lines are a styling choice. A dense grid can compete with the data, so use it only when it clarifies the intervals. See Matplotlib’s axis-scales guide and ticker API reference for scale, locator, and formatter details.
Best Value
A complete log-log example
For data that are positive on both axes, a reusable object-oriented example looks like this:
import matplotlib.pyplot as plt
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 with ax.semilogx or ax.semilogy if only one axis should be logarithmic. Labeling the scale on the axes helps readers distinguish multiplicative spacing from linear spacing.
Quick Recap
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.

