Python Plot Logarithmic Axes — Easy Bitcoin Example

Quick Answer: To print a logarithmic x-axis or y-axis (base 10) without a Matplotlib axis object use plt.xscale('log') or plt.yscale('log'). To set different bases or switch back to a linear scale, use {"linear", "log", "symlog", "logit"} or set the basex and basey arguments (e.g., plt.yscale('log', basey=2) for log base 2).

Next, we’ll have a look at the challenge and different ways to solve this problem.

Challenge: How to Plot Logarithmic Axes?

If you plot an exponentially increasing trend line, you often don’t see patterns at the beginning of the trend as the exponential trend output is dominated by the end of a timeframe.

This is how the Bitcoin price data plot looks like with linear axes:

πŸ’¬ Challenge: How to print logarithmic x-axes and y-axes in Python Matplotlib?

First, I show you the way I don’t personally use with axes objects. After that, I’ll show you how to accomplish the same goal but without axes objects—and in a more concise way.

Let’s get started! πŸ‘‡

Logarithmic y-Axis Using Axis Object

To print a logarithmic y-axis in Matplotlib, get the axis object using ax = fig.add_subplot(1, 1, 1) and then set the y-axis to a logarithm of power 10 using set_yscale('log').

Here’s an example on Bitcoin price data:

import matplotlib.pyplot as plt


years = [year for year in range(2013, 2023)]
bitcoin = [13, 754, 314, 434, 998, 13657, 3843, 7200, 29374, 47686]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

line, = ax.plot(years, bitcoin, '-o')

ax.set_yscale('log')

ax.set_ylabel('BTC Price (USD$)')
ax.set_xlabel('Year')

plt.grid()
plt.show()

Logarithmic y-Axis Without Axis Object

Personally, I don’t like using axes objects in Matplotlib. It feels clunky, and I don’t think the object-oriented representation is really natural—what does an “axis object” mean anyway. And look at the strange code to get the object in the first place! πŸ€•

To print a logarithmic y-axis without a Matplotlib axis object use a simple one-liner plt.yscale('log') and you get a logarithmic y-axis with basis 10. You can set different bases or switch back to a linear scale using any of the following specifiers {"linear", "log", "symlog", "logit"}.

It cannot get any simpler!

import matplotlib.pyplot as plt


years = [year for year in range(2013, 2023)]
bitcoin = [13, 754, 314, 434, 998, 13657, 3843, 7200, 29374, 47686]

plt.plot(years, bitcoin, '-o')

plt.yscale('log')

plt.ylabel('BTC Price (USD$)')
plt.xlabel('Year')

plt.grid()
plt.show()

Logarithmic x-axis and y-axis Without Axis Object

To print a logarithmic y-axis (basis 10) without a Matplotlib axis object, add two lines plt.xscale('log') and plt.yscale('log').

Here’s an example on BTC price data (yeah, doesn’t make much sense, right?): πŸ‘‡

import matplotlib.pyplot as plt


years = [year for year in range(2013, 2023)]
bitcoin = [13, 754, 314, 434, 998, 13657, 3843, 7200, 29374, 47686]

plt.plot(years, bitcoin, '-o')

plt.xscale('log')
plt.yscale('log')

plt.ylabel('BTC Price (USD$)')
plt.xlabel('Year')

plt.grid()
plt.show()

Set Logarithmic Axis with Base 2

To set a logarithmic axis in a Matplotlib plot, you can specify the base=2 argument of the plt.yscale('log', base=2) function.

Here’s an example:

import matplotlib.pyplot as plt


years = [year for year in range(2013, 2023)]
bitcoin = [13, 754, 314, 434, 998, 13657, 3843, 7200, 29374, 47686]

plt.plot(years, bitcoin, '-o')

plt.yscale('log', base=2)

plt.ylabel('BTC Price (USD$)')
plt.xlabel('Year')

plt.grid()
plt.show()

Set Logarithmic Axis with Base x

To set a logarithmic axis in a Matplotlib plot, you can specify the base=x argument of the plt.yscale('log', base=x) function. Just replace x with your desired basis such as x=2, x=16, or x=10.

Here’s an example for base 3:

import matplotlib.pyplot as plt


years = [year for year in range(2013, 2023)]
bitcoin = [13, 754, 314, 434, 998, 13657, 3843, 7200, 29374, 47686]

plt.plot(years, bitcoin, '-o')

plt.yscale('log', base=3)

plt.ylabel('BTC Price (USD$)')
plt.xlabel('Year')

plt.grid()
plt.show()

Learn More About Matplotlib

πŸ‘‰ Recommended Tutorial: Matplotlib β€” A Simple Guide with Videos

Also, if you want to keep learning and improving your Python coding skills, feel free to join our email academy—we have cheat sheets too!