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:
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.
#1 Best Overall
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.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →- 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.
Rank #3
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.
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 →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:
Best Value
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.
Recommended Free Tools
Quick Recap
A quick selection checklist
- Use
semilogxif only x needs logarithmic spacing. - Use
semilogyif only y needs logarithmic spacing. - Use
loglogif 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.




