# Matplotlib log log plot

In this Python tutorial, we will discuss the Matplotlib log log plot in python to plot the graph with the log scale, and we will also cover the following topics:

• Matplotlib log log plot
• Matplotlib loglog log scale base 2
• Matplotlib loglog log scale negative
• Matplotlib log log scatter
• Matplotlib log log histogram
• Matplotlib log log plot error bars
• Matplotlib log log grid
• Matplotlib loglog log scale
• Matplotlib loglog log scale ticks
• Matplotlib loglog log scale y
• Matplotlib loglog log scale x
• Matplotlib loglog log scale minor ticks
• Matplotlib loglog log scale colorbar

## Matplotlib log log plot

In python, matplotlib provides a function loglog that makes the plot with log scaling on both of the axis (x-axis and y-axis).

``matplotlib.pyplot.loglog(x, y[, linewidth, color, basex, basey, ...])``

In the above syntax,

• x specifies the x-axis values to be plotted.
• y specifies the y-axis values to be plotted.
• We can specify any of the parameters that is supported by a basic plot in matplotlib like linewidth, color, linestyle, label, etc.
• And in addition to the basic plot parameters, the following parameters can also be specified:
• basex and basey: We can specify the base of the log scale for the x-axis and y-axis by using the basex and basey respectively. The default base is 10.
• subsx and subsy: We can specify the location of the minor x and y ticks on the graph using the subsx and subsy. If it is None, the reasonable locations are automatically selected based on the number of decades in the plot.
• nonposx and nonposy: We can either mask the the non-positive values in the x and y as invalid, or clip them to a very small positive number. By default, it is masked.

Now, let’s implement our understanding through an example:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plot
x = np.arange(0.0, 10.0, 0.005)
y = np.exp(2.3 * x + 3.7)

# Creating figure and axes
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=[7, 11])

# Plotting the graph without using loglog
ax1.plot(x, y, ':b', linewidth=2, label='e ^ (2.3 * x + 3.7)')
ax1.set_title('Exponential plot', fontsize=15)
ax1.set_xlabel('x-axis', fontsize=13)
ax1.set_ylabel('y-axis', fontsize=13)
ax1.legend()

# Plotting the graph with Log ticks at x and y axis using loglog
ax2.loglog(x, y, '--r', linewidth=2, label='e ^ (2.3 * x + 3.7)')
ax2.set_title('loglog exponential plot', fontsize=15)
ax2.set_xlabel('log(x)', fontsize=13)
ax2.set_ylabel('log(y)', fontsize=13)
ax2.legend()

plt.tight_layout()
plt.show()``````

## Matplotlib loglog log scale base 2

We can change the base of the log scale of the axes of the graph by specifying the arguments basex and basey for the x-axis and y-axis respectively, in the matplotlib.pyplot.loglog() function. Hence, change the base to 2 for any of the axes of the graph

Let’s implement the concept through an example, and change the base of the x-axis to 2:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plot
x = np.arange(0.0, 10.0, 0.005)
y = np.exp(2.3 * x + 3.7)

# Resizing the figure
plt.figure(figsize=[7, 5])

# Plotting the graph with Log ticks at x and y axis using loglog
plt.loglog(x, y, '--r', basex=2, linewidth=2,
label='e ^ (2.3 * x + 3.7)')
plt.title('loglog exponential plot with x-axis log base 2',
fontsize=15)
plt.xlabel('log(x)', fontsize=13)
plt.ylabel('log(y)', fontsize=13)

plt.legend()
plt.show()``````

## Matplotlib loglog log scale negative

Matplotlib handles the negative values for the log scaled axis of the graph by specifying the arguments nonposx and nonposy for the x-axis and y-axis respectively.

We can specify the value ‘mask’ or ‘clip’ to the arguments nonposx and nonposy.

‘mask’ makes the graph to neglect the negative value of the data-point on the axis and treat the negative values as invalid. And ‘clip’ changes the negatively valued data points to the small positive values.

Now, let’s do some hands-on examples to understand the concept:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plots
x = np.linspace(-2.0, 2.0, 20)
y = 2.1 +  3.1 * (x ** 3)

# Resizing the figure
plt.figure(figsize=[8, 10])

# Plotting the loglog graph with neglecting negative values
plt.subplot(211)
plt.title('loglog neglecting negative values', fontsize=15)
plt.xlabel('log(x)', fontsize=13)
plt.ylabel('log(y)', fontsize=13)

# Plotting the loglog graph with negative values to be changed to
# small positives
plt.subplot(212)
plt.loglog(x, y, 'g', basex=2, basey=2, nonposx='clip', nonposy='clip')
plt.title('loglog with negative values change into small positives', fontsize=15)
plt.xlabel('log(x)', fontsize=13)
plt.ylabel('log(y)', fontsize=13)

plt.tight_layout()
plt.show()``````
READ:  What is the pd.crosstab function in Python [with 2 Examples]

## Matplotlib log log scatter

We can change the scale of a scatter plot to the log scale using the matplotlib.pyplot.loglog() function in python.

We can do so, by either of the below methods:

• The method discussed above, directly using the matplotlib.pyplot.loglog() function and specifying the value ‘.’ to the argument linestyle, for plotting the graph as well as to change the axes scale to log scale.
• Or we can create a scatter plot first, using the matplotlib.pyplot.scatter() function to plot the graph and then specifying the matplotlib.pyplot.loglog() function with required arguments to change the scale of the axis to log scale.

Let’s implement the 2nd method through an example:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plot
x = np.arange(1, 100, 5)
y = 32 * x

# Resizing the figure
plt.figure(figsize=[7, 5])

# Plotting the scatter plot
plt.scatter(x, y, c='g', alpha=0.6)
plt.title('loglog linear plot with y-axis log base 2', fontsize=15)
plt.xlabel('log(x) base 10', fontsize=13)
plt.ylabel('log(y) base 2', fontsize=13)

# Changing to the log ticks at x and y axis using loglog
plt.loglog(basex=10, basey=2)

plt.show()``````

## Matplotlib log log histogram

We can change the scale of the axes of the histogram in python by specifying the value True to the argument log in the matplotlib.pyplot.hist() function.

It will change the scale of the y-axis of the histogram to the log scale with default base 10.

Let’s implement the concept through an example:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plots
np.random.seed(217)
x = np.random.randn(10000)
nbins = 15

# Resizing the figure
plt.figure(figsize=[9, 11])

# Plotting the histogram without log scale
plt.subplot(211)
plt.hist(x, bins=nbins)
plt.title('Histogram', fontsize=15)
plt.xlabel('x-axis (bins)', fontsize=13)
plt.ylabel('y-axis (Heights)', fontsize=13)

# Plotting the histogram on log scale
plt.subplot(212)
plt.hist(x, bins=nbins, log=True, color='g')
plt.title('Histogram on log scale', fontsize=15)
plt.xlabel('x-axis (bins)', fontsize=13)
plt.ylabel('log(y): log(Heights)', fontsize=13)

plt.tight_layout()
plt.show()``````

## Matplotlib log log plot error bars

We can change the scale of the error bars plotted on the graph to the log scale by using the matplotlib.pyplot.loglog() function with specified nonposx and nonposy arguments.

The values for the error bars can be negative, so we have to specify the values of the arguments nonposx and nonposy as ‘clip’, to change the negative values to the small positive values and plot those to the graph.

Let’s take look at the implementation of the above concept as an example:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plots
x = 10.0**np.linspace(0.0, 2.0, 20)
y = 2.1 * (x ** 3) + 3.1 * (x ** 2)
x_err = 0.1 * x
y_err = 7.0 + 0.35 * y

# Resizing the figure
plt.figure(figsize=[8, 10])

# Plotting the errorbars without log scale
plt.subplot(211)
plt.errorbar(x, y, xerr=x_err, yerr=y_err)
plt.title('Errorbars', fontsize=15)
plt.xlabel('x-axis', fontsize=13)
plt.ylabel('y-axis', fontsize=13)

# Plotting the errorbars with log scale
plt.subplot(212)
plt.errorbar(x, y, xerr=x_err, yerr=y_err, color='g')
plt.loglog(nonposx='clip', nonposy='clip')
plt.title('Errorbars on log scale', fontsize=15)
plt.xlabel('log(x)', fontsize=13)
plt.ylabel('log(y)', fontsize=13)

plt.tight_layout()
plt.show()``````

## Matplotlib log log grid

We can specify the gridlines in the log scaled graphs by adding the statement matplotlib.pyplot.grid() with the graph statements.

Let’s practice an example to make the concepts more clear. In the example given below, we have created 4 subplots with different types of graphs with gridlines for each graph:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Resizing the figure
plt.figure(figsize=[11, 10])

# Preparing the data for the plot
x1 = np.arange(0.0, 10.0, 0.005)
y1 = np.exp(2.3 * x1 + 3.7)

# Plotting the graph with Log ticks at x and y axis using loglog
plt.subplot(221)
plt.loglog(x1, y1, '--r', basex=2, linewidth=3)
plt.title('loglog exponential plot', fontsize=15)
plt.xlabel('log(x) base 2', fontsize=13)
plt.ylabel('log(y) base 10', fontsize=13)
plt.grid()

# Preparing the data for the plot
x2 = np.arange(1, 100, 5)
y2 = 32 * x2

# Plotting the scatter plot
plt.subplot(222)
plt.scatter(x2, y2, c='y', alpha=0.6)
plt.title('loglog linear plot', fontsize=15)
plt.xlabel('log(x) base 10', fontsize=13)
plt.ylabel('log(y) base 2', fontsize=13)
# Changing to the log ticks at x and y axis using loglog
plt.loglog(basex=10, basey=2)
plt.grid()

# Preparing the data for the plots
np.random.seed(217)
x3 = np.random.randn(10000)
nbins = 15

# Plotting the histogram on log scale
plt.subplot(223)
plt.hist(x3, bins=nbins, log=True, color='g')
plt.title('Histogram on log scale', fontsize=15)
plt.xlabel('x-axis (bins)', fontsize=13)
plt.ylabel('log(y): log(Heights)', fontsize=13)
plt.grid()

# Preparing the data for the plots
x4 = 10.0**np.linspace(0.0, 2.0, 20)
y4 = 2.1 * (x4 ** 3) + 3.1 * (x4 ** 2)
x_err = 0.1 * x4
y_err = 7.0 + 0.35 * y4

# Plotting the errorbars with log scale
plt.subplot(224)
plt.errorbar(x4, y4, xerr=x_err, yerr=y_err, color='c')
plt.loglog(basex=2, nonposx='clip', nonposy='clip')
plt.title('Errorbars on log scale', fontsize=15)
plt.xlabel('log(x) base 2', fontsize=13)
plt.ylabel('log(y) base 10', fontsize=13)
plt.grid()

plt.tight_layout()
plt.show()``````

## Matplotlib loglog log scale

We can scale up and down the log scale on the graph by changing the base of the log scale to the required value (the minimum it can be 2).

• We can change the base of the log scales by specifying the arguments basex and basey in the matplotlib.pyplot.loglog() function.
• Or we can use the functions matplotlib.pyplot.xscale() and matplotlib.pyplot.yscale() with a position argument ‘log’ to change the scale of the axes to log scale. And also specifying the arguments basex and basey to specify the base of the log scale of the x-axis and y-axis respectively. The syntax is as follows:
``````matplotlib.pyplot.xscale('log', [basex, subsx, nonposx])

matplotlib.pyplot.yscale('log', [basey, subsy, nonposy])``````

In the above syntax, the positional argument here is ‘log’, we can specify it as ‘linear’, ‘symlog’, ‘logit’, etc., that specifies the type of scale we want. And the rest of the arguments are already discussed in the first topic.

Let’s implement the 2nd method to change the scale of the axes of the graph, through an example:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plots
x = np.linspace(-2.0, 2.0, 20)
y = 2.1 +  3.1 * (x ** 3)

# Resizing the figure
plt.figure(figsize=[8, 10])

plt.plot(x, y, 'g', linewidth=3)
plt.xscale('log', basex=2, nonposx='clip')
plt.yscale('log', basey=2, nonposy='clip')
plt.title('Matplotlib log scale using xscale and yscale', fontsize=15)
plt.xlabel('log(x) base 2', fontsize=13)
plt.ylabel('log(y) base 2', fontsize=13)

plt.grid()
plt.show()``````

## Matplotlib loglog log scale ticks

By changing the base of the log scaled axis we can get the ticks to be changed as well. And we have already discussed the method to change the base of the log scaled axis.

So, let’s implement the concept and do some hands-on practice through an example:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plots
x = np.linspace(-2.0, 2.0, 20)
y = 3.1 * (x ** 4) + 0.3 * (x ** 2) + 0.7 * x

# Resizing the figure
plt.figure(figsize=[7, 11])

plt.subplot(311)
plt.plot(x, y, 'r', linewidth=3)
plt.loglog(basex=10, basey=10, nonposx='clip', nonposy='clip')
plt.title('Matplotlib loglog scale tick plot 1', fontsize=15)
plt.xlabel('log(x) base 10', fontsize=13)
plt.ylabel('log(y) base 10', fontsize=13)
plt.grid(linewidth=2)

plt.subplot(312)
plt.plot(x, y, 'g', linewidth=3)
plt.loglog(basex=5, basey=5, nonposx='clip', nonposy='clip')
plt.title('Matplotlib loglog scale tick plot 2', fontsize=15)
plt.xlabel('log(x) base 5', fontsize=13)
plt.ylabel('log(y) base 5', fontsize=13)
plt.grid(linewidth=2)

plt.subplot(313)
plt.plot(x, y, 'c', linewidth=3)
plt.loglog(basex=2, basey=2, nonposx='clip', nonposy='clip')
plt.title('Matplotlib loglog scale tick plot 3', fontsize=15)
plt.xlabel('log(x) base 2', fontsize=13)
plt.ylabel('log(y) base 2', fontsize=13)
plt.grid(linewidth=2)

plt.tight_layout()
plt.show()``````

## Matplotlib loglog log scale y

We can change only the scale of the y-axis to the log scale by using the matplotlib.pyplot.semilogy() function. It works the same as the matplotlib.pyplot.loglog() function, but provide only the y-axis scale arguments (basey, subsy, and nonposy). The syntax is as follows:

``matplotlib.pyplot.semilogy(x, y, [, color, linestyle, basex, subsx, nonposx, ...])``

In the above syntax, all the arguments are already discussed in the first topic.

Let’s implement the above concept through an example:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plots
x = np.linspace(0.01, 100, 1000)
y = 7.1 * np.exp(-x) + 2.5 * x

# Resizing the figure
plt.figure(figsize=[9, 7])

# log y axis in the plot
plt.semilogy(x, y, basey=2)
plt.title('Matplotlib log scale y', fontsize=15)
plt.xlabel('x-axis', fontsize=13)
plt.ylabel('log(y) base 2', fontsize=13)
plt.grid()

plt.show()``````

## Matplotlib loglog log scale x

We can change only the scale of the x-axis to the log scale by using the matplotlib.pyplot.semilogx() function. It works the same as the matplotlib.pyplot.loglog() function, but provide only the x-axis scale arguments (basex, subsx, and nonposx). The syntax is as follows:

``matplotlib.pyplot.semilogx(x, y, [, color, linestyle, basex, subsx, nonposx, ...])``

In the above syntax, all the arguments are already discussed in the first topic.

Let’s implement the above concept through an example:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plots
x = np.linspace(0.01, 100, 1000)
y = np.cos(np.pi * x)

# Resizing the figure
plt.figure(figsize=[9, 7])

# log x axis in the plot
plt.semilogx(x, y, basex=10)
plt.title('Matplotlib log scale x for cos function', fontsize=15)
plt.xlabel('log(x) base 10', fontsize=13)
plt.ylabel('y-axis', fontsize=13)
plt.grid()

plt.show()``````

## Matplotlib loglog log scale minor ticks

We can scale the minor ticks of the log scaled axis by specifying the list of the values to the value of the arguments subsx and subsy in the matplotlib.pyplot.loglog() function for the x-axis minor ticks and y-axis minor ticks respectively.

For instance, for a log scaled axis with base 10, we can give the value to the subsx/subsy as [2, 3, 4, 5, 6, 7, 8, 9], which will locate these list values in between the values of the major ticks [100, 101, 102, …]. Similarly, for a log scaled axis with base 5, we can give [2, 3, 4] or [1.5, 2, 2.5, 3, 3.5, 4, 4.5].

Let’s go for an example to implement the above concept:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt

# Preparing the data for the plots
x = np.linspace(-2.0, 2.0, 20)
y = 3.1 * (x ** 3) + 0.3 * (x ** 2) + 0.7 * x

# Resizing the figure
plt.figure(figsize=[7, 11])

plt.subplot(211)
plt.plot(x, y, 'g', linewidth=3)
plt.loglog(basex=2, basey=10, nonposx='clip', nonposy='clip',
subsx=[0.5, 1.0, 1.5])
plt.title('Matplotlib log scale scale minor ticks Plot 1', fontsize=15)
plt.xlabel('log(x) base 2', fontsize=13)
plt.ylabel('log(y) base 10', fontsize=13)
plt.grid(b=True, which='major', linewidth=2, linestyle='-')
plt.grid(b=True, which='minor', linewidth=1, linestyle='--')

plt.subplot(212)
plt.plot(x, y, 'g', linewidth=3)
plt.loglog(basex=2, basey=10, nonposx='clip', nonposy='clip',
subsx=[0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75],
suby=[2, 3, 4, 5, 6, 7, 8, 9])
plt.title('Matplotlib log scale scale minor ticks Plot 2', fontsize=15)
plt.xlabel('log(x) base 2', fontsize=13)
plt.ylabel('log(y) base 10', fontsize=13)
plt.grid(b=True, which='major', linewidth=2, linestyle='-')
plt.grid(b=True, which='minor', linewidth=1, linestyle='--')

plt.tight_layout()
plt.show()``````

## Matplotlib loglog log scale colorbar

We can also change the scale of the colorbar to the log scale in python using matplotlib. We can use the colors sub-module from the matplotlib module/library of python to specify the log scaled color values.

We can specify the value of the norm argument in the image (or the colorgrid or the colormesh) defined (or created) for which we are defining the colorbar, to be color.LogNorm() which will scale the normalized color values into a log scale.

Let’s illustrate the above concept through an example. In the example, we will be creating a colorgrid using pcolormesh() and then define the log scaled colorbar for the colorgrid created:

``````# Importing necessary libraries
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import colors as colors

# Preparing the data for the pcolormesh
np.random.seed(20000)
Z = np.random.rand(7, 15)

x = np.arange(-0.75, 15.0, 1)
y = np.arange(5.0, 15.0, 1.5)

# Resizing the figure
plt.figure(figsize=[7, 11])

# Plotting the pcolormesh with colorbar
plt.subplot(211)
plt.pcolormesh(x, y, Z)
plt.title('Matplotlib pcolormesh with colorbar', fontsize=15)
plt.xlabel('x-axis', fontsize=13)
plt.ylabel('y-axis', fontsize=13)
plt.colorbar()

# Plotting the pcolormesh with log scale colorbar
plt.subplot(212)
plt.pcolormesh(x, y, Z, norm=colors.LogNorm())
plt.title('Matplotlib pcolormesh with log scale colorbar', fontsize=15)
plt.xlabel('x-axis', fontsize=13)
plt.ylabel('y-axis', fontsize=13)
plt.colorbar()

plt.tight_layout()
plt.show()``````

Also, check the following articles on Matplotlib.

In this Python tutorial, we have discussed the Matplotlib log log plot in python to plot the graph with a log scale, and we have also covered the following topics:

• Matplotlib log log plot
• Matplotlib log log scatter
• Matplotlib log log histogram
• Matplotlib log log plot error bars
• Matplotlib log log grid
• Matplotlib loglog log scale
• Matplotlib loglog log scale ticks
• Matplotlib loglog log scale y
• Matplotlib loglog log scale x
• Matplotlib loglog log scale minor ticks
• Matplotlib loglog log scale colorbar
• Matplotlib loglog log scale base 2
• Matplotlib loglog log scale negative