0

I defined a function as below to plot multiple histograms by plt.hist(). I claimed 4 datasets and 4 colors to label them. However, it leads to an error:

def check3_pt_bias(x):
    bins=[[25,60,90,120,160,400],[25,40,60,400],[25,45,70,100,160,400],[25,35,55,400]]
    name = ['lead b-top','sub-lead b-top','lead b-Add','sub-lead b-Add']
    biahist=np.array([collection42[2][x],collection42[1][x],collection43[1][x],collection44[1][x]],dtype=object)
    for i in range(len(biahist)):
        print(len(biahist[i]))
    print(len(biahist))
    fig, axs = plt.subplots(2,1, gridspec_kw={'height_ratios': [2, 1]})
    (n, bins, patches)=axs[0].hist(biahist, bins[x],color=['r','g','b','y'], range =[0,400], 
                            density=True, histtype='step',label=['truth','All_var model','dR model','Mass model'])
    axs[0].legend()
    axs[0].set_ylabel('density')
    axs[0].set_yscale('log')
    axs[0].set_xscale('log')
    axs[0].set(xticklabels=[])
    ratio = [[],[],[]]
    div = [0,0,0]
    wdiv = [0,0,0]
    for i in range(len(n[0])):
        for j in range(3):
            ratio[j].append(n[j+1][i]/n[0][i])
            div[j] += (ratio[j][i]-1)**2
            wdiv[j] += ((ratio[j][i]-1)*n[0][i])**2
        axs[1].hlines(y = ratio[0][i], xmin = bins[i], xmax = bins[i+1],color = 'g',label='All_var model')
        axs[1].hlines(y = ratio[1][i], xmin = bins[i], xmax = bins[i+1],color = 'b',label='dR model')
        axs[1].hlines(y = ratio[2][i], xmin = bins[i], xmax = bins[i+1],color = 'y',label='Mass model')
    axs[1].hlines(y = 1, xmin = bins[0], xmax = bins[-1],color = 'r',label='truth')
    axs[1].set_xlabel('Pt ('+name[x]+'), GeV')
    axs[1].set_ylabel('model / truth')
    #plt.show()
    print("deviation of All_var model:",np.sqrt(div[0]))
    print("deviation of dR model:",np.sqrt(div[1]))
    print("deviation of Mass model:",np.sqrt(div[2]))
    print("weighted deviation of All_var model:",np.sqrt(wdiv[0]))
    print("weighted deviation of dR model:",np.sqrt(wdiv[1]))
    print("weighted deviation of Mass model:",np.sqrt(wdiv[2]))
check3_pt_bias(2)

output:

914873
914873
914873
914873
4
ValueError: The 'color' keyword argument must have one color per dataset, but 914873 datasets and 4 colors were provided

I printed the length of dataset and it looks right. You can see that 914873 is the length of one of elements in the dataset, instead of the dataset length. Actually other values of the input variable x worked successfully, making me even more confused by this error. Does anyone have ideas?

Thank you in advance!

GeoLouise
  • 21
  • 2
  • You need to provide 914873 colors, not 4. Or provide 4 datasets, not 914873. – mkrieger1 May 13 '23 at 22:33
  • Try transposing https://stackoverflow.com/questions/6473679/transpose-list-of-lists the `biahist` variable – Caridorc May 13 '23 at 22:35
  • I did provide 4 colors, but not provide 914873 datasets. The 4 in output is the number of datasets. @mkrieger1 – GeoLouise May 14 '23 at 00:49
  • Sorry, what is motivation of transposition? I do not want to change the element order in each nested list, like what your link wants (if I didn't misunderstand).@Caridorc – GeoLouise May 14 '23 at 00:55

0 Answers0