21

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?

Paul P
  • 3,346
  • 2
  • 12
  • 26
student1818
  • 221
  • 1
  • 2
  • 3
  • Please provide a sample dataset. – figurine Dec 06 '16 at 16:54
  • my data was a list of radiation exposure amounts--let me find the list. data=[13.140,17.520,15.768,10.512,10.512,9.636,10.512, 9.636,11.388,7.884,7.008,7.008,9.636,11.388,7.884,7.88,16.644,42.924,17.520] – student1818 Dec 06 '16 at 17:10
  • Possible duplicate of [How to have logarithmic bins in a Python histogram](https://stackoverflow.com/questions/6855710/how-to-have-logarithmic-bins-in-a-python-histogram) – Cris Luengo Oct 04 '19 at 15:20

1 Answers1

29

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.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712