Normalising 2D histogram in python

I have a 2D histogram h1 with var1 on the x axis and var2 on the y axis, which I’ve plotted from a dataframe. I have normalised it as I want in c++ but now need to do the same in python and am struggling with how to get and set bin content. The idea is to remove the effect of having more events in one part of the distribution than in another and only leave the correlation between var1 and var2.

Working Code in c++:

double norm = h1->GetEntries()/h1->GetNbinsX();
 
    int nbins = h1->GetNbinsX();
    for(int i = 1; i< nbins+1; i++)
      {
        double nevents = 0.;
        for(int iy = 1; iy< h1->GetNbinsY()+1; iy++)
          {
          float bincont = h1->GetBinContent(i,iy);
          nevents+=bincont;
          }

        for(int iy = 1; iy< h1->GetNbinsY()+1; iy++) 
        {
          float bincont = h1->GetBinContent(i,iy);
          float fact = norm/nevents;
          float value = bincont*fact;
          h1->SetBinContent(i,iy,value);
        }
      }

Attempt for code in python:

plt.hist2d(var1, var2, bins=(11100, 1030), cmap=plt.cm.BuPu)

norm = 10
for i in var1:
    nevents = 0.
    for j in var21:
        plt.GetBinContent(i,j)
        nevents+=bincont

    for j in var2:
        plt.GetBinContent(i,j)
        fact = norm/nevents
        value = bincont*fact

        plt.SetBinContent(i, j, value)

Any help would be greatly appreciated. :slight_smile:

Hi,

My first question would be: are var1, var21 and var2 iterable in Python? What are their types?

On the other hand, do you see an error in the call to SetBinContent?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.