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

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

For an existing Matplotlib axes, call ax.set_yscale('log'). The default logarithm base is 10; specify base=2 or another base to change it. Ordinary log scales cannot show zero or negative measurements as themselves. If your data crosses zero, use symlog with a linear region sized by linthresh.

Set a Matplotlib y-axis to a logarithmic scale

Use the object-oriented Axes method after creating the axes and plotting your data:

import matplotlib.pyplot as plt

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

This changes the mapping from data values to vertical positions and applies scale-appropriate tick locators and formatters. The default log base is 10. To use another base, pass it explicitly:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
ax.set_yscale('log', base=2)

Choose a base that suits how you want to read the values; changing the base changes the tick spacing and labels, not the underlying measurements. See Matplotlib’s Axes.set_yscale API and axis scales guide.

What happens to zero and negative values?

A real logarithm is undefined at zero and for negative values. A standard log axis therefore cannot place those measurements as themselves. Matplotlib documents two ways to handle non-positive values when plotting on a log scale:

ax.set_yscale('log', nonpositive='mask')  # mask non-positive values
ax.set_yscale('log', nonpositive='clip')  # clip to a small positive value
  • mask: non-positive values are omitted from the plotted representation. Depending on the artist, this can leave gaps or omit affected elements.
  • clip: non-positive values are clipped to a small positive value for display. This can keep elements such as error bars visible at the lower edge, but it does not make zero or a negative measurement mathematically valid on a log scale.

Which behavior is appropriate depends on what the chart is meant to communicate. Matplotlib demonstrates the difference with error bars that extend below zero in its log-scale example. If zero is meaningful in your data, do not treat clipping as a way to preserve its true value.

Use symlog when values cross zero

When both negative and positive values need to remain visible, use the symmetric logarithmic scale, or symlog. It maps a band around zero linearly and compresses larger magnitudes logarithmically:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
ax.set_yscale('symlog', linthresh=1)

Choose a threshold in the data’s units

linthresh sets the extent of the linear region around zero. Choose it based on the values that need readable linear resolution near zero and on the units of your data. Matplotlib offers a rule of thumb: put the threshold near the minimum absolute value, leaving no points or only a few points in the linear region. That is guidance, not a universal rule; inspect the resulting plot against the purpose of your visualization.

Understand the transition and its controls

The transition between the linear and logarithmic regions has a gradient discontinuity, so visual slope can change at the boundary. The optional linscale parameter changes how much visual space the linear portion receives; base controls the logarithmic part. These settings affect how the plot looks and should be selected with the intended reading of the data in mind. Matplotlib’s symlog guide illustrates the scale and its controls.

Choose the scale that fits the data

Scale Behavior around zero Key control When it may fit
log Non-positive values cannot appear as themselves. base; nonpositive='mask' or 'clip' Values are positive and logarithmic distances are meaningful.
symlog Shows negative and positive values with a linear band around zero. linthresh; optionally linscale and base Values cross zero and logarithmic compression is useful away from zero.
asinh Matplotlib describes a smooth transition through zero. linear_width A smooth gradient transition is desirable; check that the scale communicates the data appropriately.

These transforms are not interchangeable: the choice changes how distances and slopes appear. Matplotlib discusses the available options in its axis scales guide.

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

Check the result against your installed Matplotlib

The examples above follow Matplotlib’s current stable documentation, which identifies the API as Matplotlib 3.11.0 and the log and symlog examples as 3.11.2. Tick appearance and defaults can differ by release, so if your installed version behaves differently, check the documentation for that version. The scale choice still needs to reflect the meaning of your data, particularly where values are zero or negative.

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.

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.