Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log x-axis for histogram

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?

like image 230
student1818 Avatar asked Dec 06 '16 16:12

student1818


People also ask

What is log histogram?

The intended use of the log-histogram is to examine the fit of a particular density to a set of data, as an alternative to a histogram with a density curve. For this reason, only the log-density histogram is implemented, and it is not possible to obtain a log-frequency histogram.

What is log X axis scale?

A logarithmic X axis is useful when the X values are logarithmically spaced. The X axis usually plots the independent variable – the variable you control. If you chose X values that are constant ratios, rather than constant differences, the graph will be easier to view on a logarithmic axis.


1 Answers

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.

like image 100
ImportanceOfBeingErnest Avatar answered Oct 19 '22 19:10

ImportanceOfBeingErnest