Open In App

How to put the y-axis in logarithmic scale with Matplotlib ?

Last Updated : 04 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In graphs, a logarithmic scale helps when numbers grow very fast, like 10, 100, 1000, and so on. Normally, in a graph, numbers increase by the same amount (like 1, 2, 3…), but in a logarithmic scale, they increase by multiplying (like 10, 100, 1000…). This makes it easier to see patterns in really big numbers. Matplotlib allows us to change the y-axis to a logarithmic scale so that even very large numbers can fit well in the graph, making it easier to understand trends. Let’s see some methods by which we can do so.

Using set_yscale(“log”)

set_yscale(“log”) method to convert the y-axis into a logarithmic scale. This is useful when plotting data that grows exponentially or spans multiple orders of magnitude. By applying this method to an Axes object, we ensure that the y-values are displayed on a log scale while maintaining a linear x-axis. Example:

Python
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 100, 1000, 10000, 100000]

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_yscale("log")  # Convert y-axis to logarithmic scale
plt.show()

Output

Output

Using set_yscale(“log”)

Explanation: The x values are sequential, while y grows exponentially. plt.subplots() creates the figure and axis, ax.plot(x, y) plots the data and ax.set_yscale(“log”) applies a logarithmic scale to the y-axis for better visualization.

Using plt.yscale(“log”)

Instead of applying the log scale to a specific axis, we can use plt.yscale(“log”) to modify the entire figure’s y-axis before plotting data. This approach is beneficial when working with simple plots, as it ensures that all y-values in the plot follow a logarithmic scale without explicitly modifying individual axes. Example:

Python
import matplotlib.pyplot as plt

data = [10**i for i in range(5)]

plt.yscale("log")  # Convert y-axis to log scale
plt.plot(data)
plt.show()

Output

Output

Using plt.yscale(“log”)

Explanation: The data list contains exponential values (10^i for i from 0 to 4). plt.yscale(“log”) sets the y-axis to a log scale, making the exponential growth clearer. plt.plot(data) plots the values and plt.show() displays the graph.

Using Logscale from matplotlib.scale

For more control over scaling, Matplotlib provides the LogScale class. This allows us to explicitly define the log scale for an axis using ax.set_yscale(LogScale(ax.yaxis)). This method is useful when we need more customization or when working with multiple subplots where specific axes need logarithmic scaling. Example:

Python
import matplotlib.pyplot as plt
from matplotlib.scale import LogScale

x = [1, 2, 3, 4, 5]
y = [10, 100, 1000, 10000, 100000]

fig, ax = plt.subplots()
ax.set_yscale(LogScale(ax.yaxis))  # Explicitly setting LogScale
ax.plot(x, y)
plt.show()

Output

Output

Using logscale

Explanation: The x values are sequential, while y grows exponentially. plt.subplots() creates a figure and axis and ax.set_yscale(LogScale(ax.yaxis)) explicitly applies a log scale to the y-axis. The ax.plot(x, y) function plots the data

Using loglog() for both axes

The loglog() function converts both x and y axes to a logarithmic scale simultaneously. This is particularly useful for plotting power-law relationships or analyzing data that follows exponential trends in both dimensions. By passing loglog(x, y, marker=”o”), we ensure that both axes follow a logarithmic scale, making visualization more interpretable. Example:

Python
import matplotlib.pyplot as plt

x = [1, 10, 100, 1000]
y = [10, 100, 1000, 10000]

plt.loglog(x, y, marker="o")
plt.show()

Output

Output

using plt.loglog()

Explanation: The plt.loglog(x, y, marker=”o”) function plots the data while applying a logarithmic scale to both axes, making it ideal for visualizing power-law relationships.



Next Article
Article Tags :
Practice Tags :

Similar Reads