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

Published: May 26, 2019

To transform an axis in logarithmic scale with Matplotlib, a solution is to use the pyplot functions xscale and yscale:

### Example 1

Let's take for example the exponential function:

````import matplotlib.pyplot as plt`
`import numpy as np`

`x_min = 0`
`x_max = 10.0`

`x = np.arange(x_min, x_max, .01)`
`y = np.exp(x)`

`plt.plot(x,y)`

`plt.xlim(x_min,x_max)`
`plt.ylim(np.exp(x_min),np.exp(x_max))`

`plt.grid(True,which="both", linestyle='--')`

`plt.title('How to add a grid on a figure in matplotlib ?', fontsize=8)`

`plt.savefig("matplotlib_grid_03.png", bbox_inches='tight')`
`plt.close()`
```

To change in logarithmic scale the y-axis, we can add: plt.yscale('log')

````import matplotlib.pyplot as plt`
`import numpy as np`

`x_min = 0`
`x_max = 10.0`

`x = np.arange(x_min, x_max, .01)`
`y = np.exp(x)`

`plt.plot(x,y)`

`plt.xlim(x_min,x_max)`
`plt.ylim(np.exp(x_min),np.exp(x_max))`

`plt.yscale('log')`

`plt.grid(True,which="both", linestyle='--')`

`plt.title('How to add a grid on a figure in matplotlib ?', fontsize=8)`

`plt.savefig("matplotlib_grid_04.png", bbox_inches='tight')`
```

### Example 2

Another example with a Mie phase function (output_mie_code.txt)

````#!/usr/bin/env python`

`import numpy as np`
`import matplotlib.pyplot as plt`

`c1,c2,c3,c4,c5  = np.loadtxt("output_mie_code.txt", skiprows=2, unpack=True)`

`fig = plt.figure()`
`ax = fig.add_subplot(111)`

`plt.plot(c1,c2,'k--')`
`plt.yscale('log')`

`plt.grid(True,which="both")`

`plt.xlabel(r"Scattering Angle \$\Theta\$ (\$^\circ\$)")`
`plt.ylabel(r"\$P_{11}\$")`

`plt.show()`
```

Note: To have the figure grid in logarithmic scale, just add the command plt.grid(True,which="both").

Image

of