0

I have two histograms (freq dist) and I want to take the average of the frequencies in each corresponding bin of the two histograms and get one histogram that displays the average frequencies.

Can I do this with pylab?

a=[] sites = [23,45,32,56,76,87,45,21,34,23,78,90,23,45,21,32,34,54,67,54,33,12,34] import random j=1 for i in range(1,len(sites)): r = random.choice([0,1]) if r == 1: a.append(sites[i] - sites[i-j]) j=1 else: j+=1 import pylab pylab.hist(a, bins=10) pylab.title("hist") pylab.xlabel("length") pylab.ylabel("Count") pylab.show() 

the snippet code is run several times with different "sites" data to get a several histograms. I want to "average" these histograms in to one.

1
  • Could you please provide a snippet of code showing how you create your histograms? Even better, a small reproducible example? Commented Apr 1, 2012 at 23:57

1 Answer 1

1

This is unreasonable unless both histograms have the same total number of elements, otherwise you must take the weighted average.

To do so, assuming your histograms have the same frequency bins, do this if the histograms are list-like:

[(x1+x2)/2 for x1,x2 in zip(h1,h2)] 

If the histograms are dict-like:

def mergeBins(bin1, bin2): label1,value1 = bin1 label2,value2 = bin2 assert label1==label2 return label1, (value1+value2)/2 dict(mergeBins() in bin1,bin2 zip(h1.items(), h2.items())) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.