def test_bar(): from rootpy.plotting import root2matplotlib as rplt h = Hist(100, -5, 5) h.FillRandom('gaus') rplt.bar(h) # stack h1 = h.Clone() stack = HistStack([h, h1]) rplt.bar(stack) rplt.bar([h, h1], stacked=True) rplt.bar([h, h1], stacked=False) rplt.bar([h, h1], stacked=False, reverse=True)
def barhist(h, **kwargs): """ Draws a ROOT TH1 histogram h as a pyplot line. kwargs: color, lw, fillstyle, label, scaling: number of "normed", rebin: number """ col = kwargs.pop("color", "blue") #b = rplt.errorbar(h, xerr=False, color=col, mec=col, ms=0, ecolor=col, **kwargs) lw = kwargs.pop("lw", 1) fs = kwargs.pop("fillstyle", False) lab = kwargs.pop("label", "") scaling = kwargs.pop("scaling", 1.0) rebin = kwargs.pop("rebin", 1) kwargs_d = dict(kwargs) if scaling == "normed": if h.Integral() > 0: scaling = 1.0 / h.Integral() else: scaling = 0.0 h = h.Clone() h.Scale(scaling) h.Rebin(rebin) h.fillstyle = "hollow" b = rplt.bar(h, lw=0, color="none", ecolor=col, label=None, **kwargs) xs = [] ys = [] for _b in b: xs += [_b.xy[0], _b.xy[0] + _b.get_width()] ys += [_b.xy[1] + _b.get_height(), _b.xy[1] + _b.get_height()] _b.set_hatch(fs) _b.set_color(col) plt.gca().add_patch(_b) #kwargs_d.pop("lw") kwargs_d.pop("edgecolor", "") kwargs_d.pop("stacked", "") #plt.xticks(np.arange(min(xs), max(xs), (max(xs)-min(xs)) / )) plt.plot(xs, ys, color=col, lw=lw, label=lab, **kwargs_d) return b
def barhist(h, **kwargs): """ Draws a ROOT TH1 histogram h as a pyplot line. kwargs: color, lw, fillstyle, label, scaling: number of "normed", rebin: number """ col = kwargs.pop("color", "blue") #b = rplt.errorbar(h, xerr=False, color=col, mec=col, ms=0, ecolor=col, **kwargs) lw = kwargs.pop("lw", 1) fs = kwargs.pop("fillstyle", False) lab = kwargs.pop("label", "") scaling = kwargs.pop("scaling", 1.0) rebin = kwargs.pop("rebin", 1) kwargs_d = dict(kwargs) if scaling == "normed": if h.Integral()>0: scaling = 1.0 / h.Integral() else: scaling = 0.0 h = h.Clone() h.Scale(scaling) h.Rebin(rebin) h.fillstyle = "hollow" b = rplt.bar(h, lw=0, color="none", ecolor=col, label=None, **kwargs) xs = [] ys = [] for _b in b: xs += [_b.xy[0], _b.xy[0]+_b.get_width()] ys += [_b.xy[1]+_b.get_height(), _b.xy[1]+_b.get_height()] _b.set_hatch(fs) _b.set_color(col) plt.gca().add_patch(_b) #kwargs_d.pop("lw") kwargs_d.pop("edgecolor", "") kwargs_d.pop("stacked", "") #plt.xticks(np.arange(min(xs), max(xs), (max(xs)-min(xs)) / )) plt.plot(xs, ys, color=col, lw=lw, label=lab, **kwargs_d) return b
h2.linewidth = 0 stack = HistStack() stack.Add(h1) stack.Add(h2) ratio = h3 / (h1 + h2) ratio = Hist.divide(h3, h1 + h2) # plot with matplotlib plt.figure(figsize=(16, 12), dpi=200) gs = gridspec.GridSpec(2, 1, height_ratios=[5, 1]) axes = plt.axes() ax0 = plt.subplot(gs[0]) ax0.minorticks_on() rplt.bar(stack, stacked=True) rplt.errorbar(h3, xerr=False, emptybins=False) # plt.xlabel('Mass', position=(1., 0.), ha='right', fontsize = 24) plt.ylabel('Events', va='top', fontsize=40) plt.tick_params(**CMS.axis_label_major) plt.tick_params(**CMS.axis_label_minor) plt.legend(numpoints=1) ax1 = plt.subplot(gs[1]) yloc = plt.MaxNLocator(4) ax1.yaxis.set_major_locator(yloc) #ax1.set_ticks([-1, 1, 2, 3]) plt.tick_params(**CMS.axis_label_major) plt.tick_params(**CMS.axis_label_minor) plt.xlabel('Mass [GeV]', position=(1., 0.), ha='right', fontsize=40) plt.ylabel('data/MC', va='top', fontsize=24) rplt.errorbar(ratio, emptybins=False)
gs = mpl.gridspec.GridSpec(2,1,height_ratios=[4,1]) gs.update(wspace=0.00, hspace=0.00) axes = plt.subplot(gs[0]) axes_ratio = plt.subplot(gs[1], sharex=axes) plt.setp(axes.get_xticklabels(), visible=False) # axes = plt.subplot() # rplt.bar(stack, stacked=True, axes=axes, yerr=False, alpha=0.5, rasterized=True, ec='grey', linewidth=1) # rplt.errorbar(hists['Data'], xerr=False, emptybins=False, axes=axes, marker='o', ms=10) try: axes.set_yscale('log') rplt.bar(stack, stacked=True, axes=axes, yerr=False, alpha=0.5, rasterized=True, ec='grey', linewidth=0) rplt.hist(stack.sum, axes=axes, yerr=False, alpha=0.8, rasterized=True, ec='grey', linewidth=1) tmpstack = stack.sum for i in xrange(len(stack)): tmpstack = tmpstack - stack[len(stack)-1-i] rplt.hist(tmpstack, axes=axes, yerr=False, alpha=0.2, rasterized=True, ec='grey', linewidth=1) if hists['Data'].Integral(): rplt.errorbar(hists['Data'], xerr=False, emptybins=False, axes=axes, marker='o', lw=1) except: # # print "no data events..." # # continue pass for signalsample in signalsamples: # print signalsample
h2.linewidth = 0 stack = HistStack() stack.Add(h1) stack.Add(h2) ratio = h3/(h1+h2) ratio = Hist.divide(h3, h1+h2) # plot with matplotlib plt.figure(figsize=(16, 12), dpi=200) gs = gridspec.GridSpec(2, 1, height_ratios=[5,1]) axes = plt.axes() ax0 = plt.subplot(gs[0]) ax0.minorticks_on() rplt.bar(stack, stacked=True) rplt.errorbar(h3, xerr=False, emptybins=False) plt.xlabel('Mass', position=(1., 0.), ha='right', fontsize = 24) plt.ylabel('Events', position=(0., 1.), va='top', fontsize = 24) plt.tick_params(**CMS.axis_label_major) plt.tick_params(**CMS.axis_label_minor) plt.legend(numpoints=1) ax1 = plt.subplot(gs[1]) yloc = plt.MaxNLocator(4) ax1.yaxis.set_major_locator(yloc) #ax1.set_ticks([-1, 1, 2, 3]) plt.tick_params(**CMS.axis_label_major) plt.tick_params(**CMS.axis_label_minor) plt.xlabel('Mass', position=(1., 0.), ha='right', fontsize = 24) plt.ylabel('data/MC', position=(0., 1.), va='top', fontsize = 24) rplt.errorbar(ratio, emptybins=False)
# plot with ROOT set_style('ATLAS') canvas = Canvas(width=700, height=500) stack.Draw() h3.Draw('E1 same') stack.xaxis.SetTitle('Mass') stack.yaxis.SetTitle('Events') legend = Legend(2) legend.AddEntry(h1, 'F') legend.AddEntry(h2, 'F') legend.AddEntry(h3, 'P') legend.Draw() canvas.Modified() canvas.Update() # plot with matplotlib fig = plt.figure(figsize=(7, 5), dpi=100, facecolor='white') axes = plt.axes([0.15, 0.15, 0.8, 0.8]) axes.xaxis.set_minor_locator(AutoMinorLocator()) axes.yaxis.set_minor_locator(AutoMinorLocator()) axes.tick_params(which='major', labelsize=15, length=8) axes.tick_params(which='minor', length=4) rplt.bar(stack, stacked=True, axes=axes) rplt.errorbar(h3, xerr=False, emptybins=False, axes=axes) plt.xlabel('Mass', position=(1., 0.), ha='right') plt.ylabel('Events', position=(0., 1.), va='top') plt.legend(numpoints=1) if not ROOT.gROOT.IsBatch(): plt.show()
label = ROOT.TText(0.3, 0.8, 'ROOT') label.SetTextFont(43) label.SetTextSize(25) label.SetNDC() label.Draw() canvas.Modified() canvas.Update() # plot with matplotlib set_style('ATLAS', mpl=True) fig = plt.figure(figsize=(7, 5), dpi=100) axes = plt.axes() axes.xaxis.set_minor_locator(AutoMinorLocator()) axes.yaxis.set_minor_locator(AutoMinorLocator()) axes.yaxis.set_major_locator(MultipleLocator(20)) rplt.bar(stack, stacked=True, axes=axes) rplt.errorbar(h3, xerr=False, emptybins=False, axes=axes) plt.xlabel('Mass', position=(1., 0.), va='bottom', ha='right') plt.ylabel('Events', position=(0., 1.), va='top', ha='right') axes.xaxis.set_label_coords(1., -0.20) axes.yaxis.set_label_coords(-0.18, 1.) leg = plt.legend() axes.text(0.3, 0.8, 'matplotlib', verticalalignment='center', horizontalalignment='center', transform=axes.transAxes, fontsize=20) if not ROOT.gROOT.IsBatch():
pad1.cd() # pad1 becomes the current pad rootstack = ROOT.THStack(stack) rootstack.Draw('HIST') rootstack.GetYaxis().SetTitle("Entries") rootstack.GetYaxis().SetTitleSize(20) rootstack.GetYaxis().SetTitleFont(43) rootstack.GetYaxis().SetTitleOffset(1.55) pad1.SetLogy() if plotWithMPL: axes.set_yscale('log') rplt.bar(stack, stacked=True, axes=axes, yerr=False, alpha=0.5, rasterized=True, ec='grey', linewidth=0) tmpstack = stack.sum tmpstack.fillstyle = "none" for i in xrange(len(stack)): myalpha = 0.4 if i > 0 else 1.0 rplt.hist(tmpstack, axes=axes, yerr=False, alpha=myalpha, rasterized=True, ec='grey', linewidth=1, fillstyle="none")
def test_bar(): from rootpy.plotting import root2matplotlib as rplt h = Hist(100, -5, 5) h.FillRandom('gaus') rplt.bar(h)
# normalize the histograms h1 /= h1.Integral() h2 /= h2.Integral() # set visual attributes h1.SetFillStyle('solid') h1.SetFillColor('green') h1.SetLineColor('green') h2.SetFillStyle('solid') h2.SetFillColor('red') h2.SetLineColor('red') stack = HistStack() stack.Add(h1) stack.Add(h2) # plot with ROOT h1.SetTitle('Histogram of IQ: #mu=100, #sigma=15') stack.Draw() h1.GetXaxis().SetTitle('Smarts') h1.GetYaxis().SetTitle('Probability') # plot with matplotlib rplt.bar(stack, alpha=0.75, stacked=True) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$') plt.show()
gs = mpl.gridspec.GridSpec(2,1,height_ratios=[4,1]) gs.update(wspace=0.00, hspace=0.00) axes = plt.subplot(gs[0]) axes_ratio = plt.subplot(gs[1], sharex=axes) plt.setp(axes.get_xticklabels(), visible=False) # axes = plt.subplot() try: axes.set_yscale('log') rplt.bar(stack, stacked=True, axes=axes, yerr=False, alpha=0.5, ) if hists['Data'].Integral(): rplt.errorbar(hists['Data'], xerr=False, emptybins=False, axes=axes) except: # print "no data events..." # continue pass for signalsample in signalsamples: skip = 1 if any([thissig in signalsample for thissig in plottedsignals]): skip=0 if skip: continue signalfile = root_open("hists/rundir_signal/"+signalsample) try:
stack.Add(tmphist) gs = mpl.gridspec.GridSpec(2, 1, height_ratios=[4, 1]) gs.update(wspace=0.00, hspace=0.00) axes = plt.subplot(gs[0]) axes_ratio = plt.subplot(gs[1], sharex=axes) plt.setp(axes.get_xticklabels(), visible=False) # axes = plt.subplot() try: axes.set_yscale('log') rplt.bar( stack, stacked=True, axes=axes, yerr=False, alpha=0.5, ) if hists['Data'].Integral(): rplt.errorbar(hists['Data'], xerr=False, emptybins=False, axes=axes) except: # print "no data events..." # continue pass for signalsample in signalsamples: skip = 1