Matplotlib Log Scale

The axes in all Matplotlib graphs are deterministic by default, as are the yscale() and xscale() functions. The pyplot module would be used to modify the visual scaling of the y-axis or x-axis to an exponential function. The function yscale() or xscale() requires only one argument, which would be the sort of spectrum modification. To change dimensions to a logarithmic scale, just use the “log” term or matplotlib.scale.

To both the yscale and xscale functions, specify the Log Scale module. The logarithmic scale is effective for visualizing data sets with extremely small and sometimes very enormous numbers since it presents the datasets in such a way that we can effectively get most of the numbers even without different sets being squashed too intimately.

We are going to discuss the Matplotlib Log scale in Python in this article. The Matplotlib log scaling is a 10-power scale. We might be using any value for the base, such as 3, or we could have used the number e to represent the value of the natural log. The padding of the depicted components could be confined or expanded by using different origins, enabling visualization clearer.

The Matlplotlib log scale will be utilized to draw axes, scatter graphs, 3D graphs, and more. Let’s examine some alternative log scale samples and their execution.

Adjusting the Scale of the Y-Axis to Matplotlib Log Scale

Specifying logarithmic axes is identical to graphing conventional axes, apart from a single code line that indicates the kind of coordinates as ‘log.’

from matplotlib import pyplot

pyplot.subplot(1, 1, 1)

x = [30**i for i in range(30)]

pyplot.plot(x, color='red', lw=10)


In the preceding case, we integrate the matplotlib.pyplot library. Matplotlib is a package in Python that is utilized to draw different charts and graphs. Next, we initially create the subplot that will be used to visualize the map. We use for loop here to state the value of the x-axis.

Further, we employ the plot() method to draw the line on the graph. We can set the color and width of the line by providing the values to the ‘color’ and ‘lw’ parameters. The powers of ten would then be displayed along with their exponential function. The values presented will further indicate an exponential increase for the logarithmic scale.

As a result, we will need to specify ‘log’ as a parameter to the pyplot.yscale() function to get the y-axis in logarithmic scale. Likewise, we could also utilize pyplot.xscale(‘log’) to modify the scaling of the x-axis to a logarithmic scale.

Log Scale in Matplotlib Utilizing the Methods Semilogx() and Semilogy():

Some other way to make a graph using a logarithmic scale somewhere along X-axis is to use the semilogx() method. The semilogy() method, on the other hand, provides a figure having a logarithmic scale along the Y-axis.

import pandas as pd

import matplotlib.pyplot as plt

x = [200, 2000, 20000, 200000, 2000000]

y = [10, 12 ,14, 16, 18]


fig = plt.figure(figsize=(6,4))








Hereafter importing the libraries, we initialized two arrays that contain random values for the x and y axes. Next, we adjust the size of the figure. To draw the scatter graph, we will apply the plt.scatter() function. Meanwhile, we employ the plot() function to draw the line. The logarithm’s standard value of base is 10. The base could be specified with both the basex and basey arguments to semilogx() and semilogy() methods, accordingly.

The plt.semilogx() method has default base 10 and it is utilized to convert the x-axis to a log scale in this scenario. The plt.semilogy() method, on the other hand, transforms the y-axis from a value of base 3 logarithmic scale. In addition to this, we specify the labels of the axes as ‘x-axis’ and ‘y-axis’ by using plt.label() functions. Similarly, the font size of these labels is also defined here. Now, we use the show() function to present the graph.

Utilizing the loglog() Function

In this step, the loglog() method would be used to create log scaling on either the X-axis or Y-axis.

import pandas as pd

import matplotlib.pyplot as plt

x = [30, 300, 3000, 30000, 300000]

y = [22, 24 ,28, 26, 32]

fig = plt.figure(figsize=(4, 4))




Firstly, we include libraries required for graphic visualizations. Then, we take two variables for storing the arrays. These arrays contain the values of data sets for the x and y axes. The size of the graph is stated by the use of the figsize() function. Here, we want to plot the scatter graph so we employ the scatter() function.

Meanwhile, we draw the line by using the plot() method. Now, we apply the loglog() method here. The value of the base of the log for both the X-axis and Y-axis is determined mostly by basex and basey arguments. The basex = 20 and basey = 4 inputs are provided to the plt.loglog() method, which produces the base 20 log scale-up x-axis in this case.

On the y-axis, base 4 logarithmic scale is being used. Further, the method is used to represent the graph.

Display Negative Numbers on Matplotlib Log Scale

The data sets include mixed positively and negatively numbers sometimes. The logarithmic scaling will not be implemented in these situations because negative values have no logarithmic values.


In this article, we examined how we utilize the Matplotlib log scale in Python. The use of logarithmic scaling is an efficient data visualization approach. We’ve shown a variety of methods for applying logarithmic scale to dimensions. These methods include semilogx() and semilogy(), as well as loglog(). We further explained how to create scatter graphs and histograms by the use of the Matplotlib log scale.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content