Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib log axis: display powers of 10 only

I have a log-log plot with range of x-axis from 10^9 to 10^12. (This is my first time posting so I am unable to post an image of my plot)

I would like to change the x and y axes so that only the powers of 10 are displayed. Something like 9, 10, 11, 12 on the x-axis.

I used matplotlib.ticker.LogFormatterExponent(base=10.0, labelOnlyBase=True) but it does not quite do the job. Any suggestions?

like image 604
Aayush Saxena Avatar asked Feb 13 '23 12:02

Aayush Saxena


1 Answers

LogFormatterExponent(base=10.0, labelOnlyBase=True) works as expected.

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

x = 10**np.linspace(8.5,12.6)
y = np.sin(x)

fig,ax = plt.subplots()
ax.scatter(x,y)
ax.set_xscale('log')
ax.set_xlabel("Quantity [$10^{x}]$")

logfmt = matplotlib.ticker.LogFormatterExponent(base=10.0, labelOnlyBase=True)
ax.xaxis.set_major_formatter(logfmt)

plt.show()

enter image description here

like image 73
ImportanceOfBeingErnest Avatar answered Apr 02 '23 22:04

ImportanceOfBeingErnest