How to put the y-axis in logarithmic scale with Matplotlib ?
Last Updated :
04 Apr, 2025
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

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

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

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

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.
Similar Reads
How to Plot Logarithmic Axes in Matplotlib?
Axesâ in all plots using Matplotlib are linear by default, yscale() and xscale() method of the matplotlib.pyplot library can be used to change the y-axis or x-axis scale to logarithmic respectively. The method yscale() or xscale() takes a single value as a parameter which is the type of conversion o
2 min read
How to Set the X and the Y Limit in Matplotlib with Python?
In this article, we will learn how to set the X limit and Y limit in Matplotlib with Python. Matplotlib is a visualization library supported by Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy s
2 min read
How to Rotate X-Axis Tick Label Text in Matplotlib?
Matplotlib is an amazing and one of the most widely used data visualization libraries in Python for plots of arrays. It is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It is much popular because of its customization options as w
3 min read
How to Set X-Axis Values in Matplotlib in Python?
In this article, we will be looking at the approach to set x-axis values in matplotlib in a python programming language. The xticks() function in pyplot module of the Matplotlib library is used to set x-axis values. Syntax: matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs) xticks() functio
2 min read
How to Make a Square Plot With Equal Axes in Matplotlib?
In this article, we are going to discuss how to illustrate a square plot with an equal axis using the matplotlib module. Â We can depict a Square plot using matplotlib.axes.Axes.set_aspect() and matplotlib.pyplot.axis() methods. Make a Square Plot With Equal Axes in MatplotlibThere are various ways t
5 min read
How to change the size of axis labels in Matplotlib?
Matplotlib is a Python library that helps in visualizing and customizing various plots. One of the customization you can do is to change the size of the axis labels to make reading easier. In this guide, weâll look how to adjust font size of axis labels using Matplotlib. Letâs start with a basic plo
2 min read
How to Add a Y-Axis Label to the Secondary Y-Axis in Matplotlib?
Sometimes while analyzing any data through graphs we need two x or y-axis to get some more insights into the data. Matplotlib library of Python is the most popular data visualization library, and we can generate any type of plot in Matplotlib. We can create a plot that has two y-axes and can provide
4 min read
How to Set Axis Ranges in Matplotlib?
Matplotlib sets the default range of the axis by finding extreme values (i.e. minimum and maximum) on that axis. However, to get a better view of data sometimes the Pyplot module is used to set axis ranges of the graphs according to the requirements in Matplotlib. Following is the method used to set
3 min read
Draw Multiple Y-Axis Scales In Matplotlib
Why areMatplotlib is a powerful Python library, with the help of which we can draw bar graphs, charts, plots, scales, and more. In this article, we'll try to draw multiple Y-axis scales in Matplotlib. Why are multiple Y-axis scales important?Multiple Y-axis scales are necessary when plotting dataset
6 min read
How to Change the Figure Size with Subplots in Matplotlib
Matplotlib is a powerful plotting library in Python that allows users to create a wide variety of static, animated, and interactive plots. One common requirement when creating plots is to adjust the figure size, especially when dealing with subplots. This article will guide you through the process o
4 min read