How to Plot Logarithmic Axes in Matplotlib
Last Updated :
22 May, 2025
Logarithmic axes help visualize data that spans several orders of magnitude by scaling the axes logarithmically instead of linearly. In Matplotlib, you can easily set logarithmic scales for the x-axis, y-axis, or both using simple methods. Let’s explore straightforward ways to apply logarithmic scales in Matplotlib.
Using plt.xscale('log') and plt.yscale('log')
This is the most straightforward methods to set logarithmic scales on the x-axis and y-axis. You first create a plot normally and then explicitly convert each axis to a log scale.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 10, 100)
y = np.exp(x)
plt.plot(x, y)
plt.xscale('log')
plt.yscale('log')
plt.show()
Output:
Using plt.xscale('log') and plt.yscale('log')Explanation: x is a linear space array, y is exponential values. The plot is initially linear, but then both axes are changed to logarithmic scales using plt.xscale and plt.yscale, which helps visualize data spanning multiple orders of magnitude.
Using ax.set_xscale('log') and ax.set_yscale('log')
This method is the object-oriented equivalent of the first and provides more flexibility when working with multiple subplots or complex figures. Instead of using the pyplot state-machine interface, you use the Axes object to set the scale.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 10, 100)
y = np.exp(x)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xscale('log')
ax.set_yscale('log')
plt.show()
Output
Using ax.set_xscale('log') and ax.set_yscale('log')Explanation: fig, ax = plt.subplots() creates a figure and axes for plotting. ax.plot(x, y) plots the data as a line graph. ax.set_xscale('log') and ax.set_yscale('log') change the x and y axes to logarithmic scales.
Using plt.loglog()
This method combines plotting and setting both axes to a logarithmic scale in one step. It’s a very concise way to generate plots where both x and y axes are logarithmic.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 10, 100)
y = np.exp(x)
plt.loglog(x, y)
plt.show()
Output
Using plt.loglog()Explanation: plt.loglog(x, y) creates a plot with both x and y axes set to logarithmic scales in one step, plotting the data as a line graph. plt.show() displays the plot.
Using plt.semilogx() or plt.semilogy()
If only one axis requires logarithmic scaling, these functions are ideal. semilogx applies log scale on the x-axis, while semilogy applies it on the y-axis.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 10, 100)
y = np.exp(x)
plt.semilogx(x, y) # Log x-axis only
plt.show()
Output:
Using plt.semilogx() or plt.semilogy()Explanation: plt.semilogx(x, y) plots the data with the x-axis on a logarithmic scale while keeping the y-axis linear. plt.show() displays the resulting plot.
Similar Reads
How to Plot Logarithmic Axes in Matplotlib? Logarithmic axes help visualize data that spans several orders of magnitude by scaling the axes logarithmically instead of linearly. In Matplotlib, you can easily set logarithmic scales for the x-axis, y-axis, or both using simple methods. Letâs explore straightforward ways to apply logarithmic scal
2 min read
How to put the y-axis in logarithmic scale with Matplotlib ? 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 real
3 min read
How to change axes limits in matplotlib? Sometimes, when you create a plot default axes X and Y may not show the full picture you want. Changing the axes limits helps you focus on specific data ranges or improve how your plot looks. There are two methods available in Axes module tochange the limits:matplotlib.axes.Axes.set_xlim(): It is us
2 min read
Matplotlib.pyplot.axes() in Python axes() method in Matplotlib is used to create a new Axes instance (i.e., a plot area) within a figure. This allows you to specify the location and size of the plot within the figure, providing more control over subplot layout compared to plt.subplot(). It's key features include:Creates a new Axes at
3 min read
Matplotlib.axes.Axes.plot() in Python Axes.plot() method in Matplotlib is used to plot data on a set of axes. It is primarily used for creating line plots but can be extended for other types of plots, including scatter plots, bar plots, and more. When using this function, you typically provide the x and y coordinates of the data points
3 min read
How to Reverse Axes in Matplotlib Matplotlib allows you to reverse X-axis, Y-axis or both using multiple approaches. This is useful when we want to visualize data from a different view like reversing the order of time in a time-series or switching the direction of the axes according to our needs.Method 1: Using invert_xaxis() and in
3 min read