# Plotting branches with binning

**URL:** <https://root-forum.cern.ch/t/plotting-branches-with-binning/48471>\
**Category:** Newbie\
**Tags:** python\
**Created:** [January 20, 2022, 3:15pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471 "2022-01-20T15:15:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muoniex](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@Muoniex](https://root-forum.cern.ch/u/Muoniex)\
**Post date:** [January 20, 2022, 3:15pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/1 "2022-01-20T15:15:01Z")

</div>

Hi. I have been trying to make a binned plot, but it has not worked…

I have a tree called “data” with two branches: energy and speed, with a total of 1000 events in the tree. I want to make a plot of energy vs speed, but with a bin in the energy so that I only have 10 points. I’ve tried to do this:

file=ROOT.TFile(…)  
data=file.Get(‘data’)

c1=ROOT.TCanvas(‘c1’,‘c1’,400,400)  
hevts=ROOT.TH1D(‘hevts’, ‘Speed’, 10, 0, 100)  
hevts.Draw()  
data.Draw(‘energy:speed\>\>+hevts’)  
c1.Draw()  
c1.SaveAs(“plot.pdf”)

But it’s not working (it draws the whole 1000 points). How can I do this properly?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 20, 2022, 3:47pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/2 "2022-01-20T15:47:02Z")

</div>

```auto
data.Draw(‘energy:speed>>+hevts’)

```

Will draws scatter plot. All the 1000 points are drawn. If you want see the binned plot you need to specify a histogram drawing option like for instance ‘COLZ’.

---

<div class="post-metadata">

**Author:** ![Muoniex](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@Muoniex](https://root-forum.cern.ch/u/Muoniex)\
**Post date:** [January 20, 2022, 4:26pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/3 "2022-01-20T16:26:45Z")

</div>

Thank you for the idea!  
I have tried something like:

c1=ROOT.TCanvas(‘c1’,‘c1’,400,400)  
hevts=ROOT.TH1D(‘hevts’, ‘Speed’, 10, 0, 100)  
hevts.SetOption(“COLZ”)  
hevts.Draw()  
data.Draw(‘energy:speed\>\>+hevts’)  
c1.Draw()  
c1.SaveAs(“plot.pdf”)

But it’s still not working ☹

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 20, 2022, 5:00pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/4 "2022-01-20T17:00:03Z")

</div>

```python
hevts = ROOT.TH2D('hevts', 'Energy vs. Speed', 10, 0., 100., 10, 0., 100.)
data.Draw('energy:speed >> hevts', '', 'colz') # hevts x = speed, y = energy

```

---

<div class="post-metadata">

**Author:** ![Muoniex](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@Muoniex](https://root-forum.cern.ch/u/Muoniex)\
**Post date:** [January 20, 2022, 5:42pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/5 "2022-01-20T17:42:18Z")

</div>

It worked! Thank you. However, the plot style is not so elegant; it’s formed by big squares. Do you know if I can change the format of the plot?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 20, 2022, 5:45pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/6 "2022-01-20T17:45:00Z")

</div>

You create a 10 x 10 bins histogram (hence “big squares”).

---

<div class="post-metadata">

**Author:** ![Muoniex](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@Muoniex](https://root-forum.cern.ch/u/Muoniex)\
**Post date:** [January 20, 2022, 6:02pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/7 "2022-01-20T18:02:28Z")

</div>

Thank you again! And a last question related with this one. Once I have the energy vs speed plot, I also need to make a frequency histogram with the energy of the events (which is basically the number of events in each bin).

However, each event must be weighted by a number stored in a branch called “weight”. So, for example, if in the first bin I have only 2 events weighted with 4 and 2, respectively, the frequency for the first bin should be 6 instead of 2.

Do you know how can I do it? I guess it should be similar to the las issue, but I don’t really know…

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 20, 2022, 6:04pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/8 "2022-01-20T18:04:09Z")

</div>

```python
data.Draw('energy:speed >> hevts', 'weight', 'colz')

```

---

<div class="post-metadata">

**Author:** ![Muoniex](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@Muoniex](https://root-forum.cern.ch/u/Muoniex)\
**Post date:** [January 20, 2022, 6:30pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/9 "2022-01-20T18:30:33Z")

</div>

That helped a lot! And what if I want to scale the whole histogram by a factor stored in the “scale” variable (not a root object)? This would be the final steep. I have tried this:

scale=100.  
data.Draw(‘energy:speed \>\> hevts’, ‘scale\*weight’, ‘colz’)

But it’s not working…

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 20, 2022, 6:32pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/10 "2022-01-20T18:32:06Z")

</div>

After the “Draw” line, add: `hevts.Scale(scale)`

---

<div class="post-metadata">

**Author:** ![Muoniex](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@Muoniex](https://root-forum.cern.ch/u/Muoniex)\
**Post date:** [January 20, 2022, 6:35pm UTC](https://root-forum.cern.ch/t/plotting-branches-with-binning/48471/11 "2022-01-20T18:35:03Z")

</div>

Thanks again!
