Log x-axis for histogram

student1818 picture student1818 · Dec 6, 2016 · Viewed 14.3k times · Source

I'm trying to plot a histogram with a logarithmic x axis. The code I'm currently using is as follows

plt.hist(data, bins=10**(np.linspace(0, 1, 2, 3), normed=1) 
plt.xscale('log')

However, the x axis doesn't actually plot correctly! It just goes from 1 to 100. Ideally I'd like to have tick marks for 1, 10, 100, and 1000. Any ideas?

Answer

ImportanceOfBeingErnest picture ImportanceOfBeingErnest · Dec 6, 2016

The following works.

import matplotlib.pyplot as plt
import numpy as np

data = [1.2, 14, 150 ]
bins = 10**(np.arange(0,4))
print "bins: ", bins
plt.xscale('log')
plt.hist(data,bins=bins) 


plt.show()

In your code the probelm is the bins array. It has only two values, [1, 10], while if you want tickmarks at 1,10,100,and 1000 you need to provide those numbers as bins.