Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a log log histogram in python

Tags:

Given an an array of values, I want to plot a log log histogram of these values by their counts. I only know how to log the x values, but not the y values because they are not explicitly created in my program.

like image 592
user984923 Avatar asked Oct 08 '11 01:10

user984923


People also ask

What is log histogram?

A logarithmic axis compresses the range in a non-linear fashion, which means that variable width bins have to be used for histograms and the y-axis represents density (not a count). Taking logs and using the result to plot a histogram usually produces a curve having a distorted shape, not twin peaks.


1 Answers

Check out the pyplot documentation.

  • pyplot.hist can "log" y axis for you with keyword argument log=True
  • pyplot.hist accepts bins keyword argument, but you have to "log" x axis yourself

For example:

#!/usr/bin/python
import numpy
from matplotlib import pyplot as plt

data = numpy.random.gumbel(2 ** 20, 2 ** 19, (1000, ))

bins = range(15, 25)
plt.xticks(bins, ["2^%s" % i for i in bins])
plt.hist(numpy.log2(data), log=True, bins=bins)
plt.show()

This will give you the actual counts of how how many elements fall into each bin, plotted on a log axis (which is what people usually mean by a log plot). I couldn't tell from your wording if you wanted this or the log of the count plotted on a linear axis.

Btw., bins don't even have to be spaced evenly.

like image 175
user670416 Avatar answered Sep 18 '22 09:09

user670416