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?
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With