# How to fill 2D histogram

**URL:** <https://root-forum.cern.ch/t/how-to-fill-2d-histogram/51398>\
**Category:** Newbie\
**Created:** [August 30, 2022, 8:17am UTC](https://root-forum.cern.ch/t/how-to-fill-2d-histogram/51398 "2022-08-30T08:17:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [August 30, 2022, 8:17am UTC](https://root-forum.cern.ch/t/how-to-fill-2d-histogram/51398/1 "2022-08-30T08:17:57Z")

</div>

Hello Experts,

I defined a 2D histogram as:

> `TH2F *h3 = new TH2F("h3", "Hist", 100, 0., 1.E5, 100, 1., 2.E7);`

I have two outputs such as energy and flux. I want to fill energy and flux in X and Y axes respectively. So fill the histogram as:

> `h3->Fill(energy, flux);`

Canvas defined as:

> `TCanvas *c1 = new TCanvas("c1"); c1->cd(); h3->Draw("AP"); `

However It does not fill properly in 2D. Kindly help me how to fill 2D histogram?

Thanks,  
Anil

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 30, 2022, 8:45am UTC](https://root-forum.cern.ch/t/how-to-fill-2d-histogram/51398/2 "2022-08-30T08:45:59Z")

</div>

What do you mean by “does not fill properly”? what do you get? It’s better if you show all the code you are trying and what you get. Anyway, the lines you show seem ok, but:  
1 - Make sure that energy and flux have values within the x and y ranges of the histo (and I suppose they are getting the values inside a loop).  
2 - I don’t think the option “A” is valid when drawing a histogram, so remove that; and instead of “P” try `h3->Draw("*");` to see the data points more clearly at least initially.

---

<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:** [August 30, 2022, 8:58am UTC](https://root-forum.cern.ch/t/how-to-fill-2d-histogram/51398/3 "2022-08-30T08:58:15Z")

</div>

> [@Anil\_Kumar](#):
>
> h3-\>Draw(“AP”);

`AP` are not valid options to draw 2D histograms (I bet you copied that from a TGraph drawing). I would suggest you try:

```auto
h3->Draw("COL");

```

or:

```auto
h3->Draw("BOX");

```

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [August 30, 2022, 9:12am UTC](https://root-forum.cern.ch/t/how-to-fill-2d-histogram/51398/4 "2022-08-30T09:12:06Z")

</div>

Thank you @couet and @dastudillo  
First I used h3-\>Draw(“COL”), but later i wished fill the histogram in line/point format. So i tried to use AP option from TGraph 😀, unfortunately it did not work. The “\*” option also does not clearly visible.  
I like to fill the 2D histogram as:

 ![Screenshot from 2022-08-30 14-39-16](https://root-forum.cern.ch/uploads/default/original/3X/3/5/3549f78d23b526f299df421044f0e419e09e27be.png)

Is there any option available for this in 2D?

---

<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:** [August 30, 2022, 9:25am UTC](https://root-forum.cern.ch/t/how-to-fill-2d-histogram/51398/5 "2022-08-30T09:25:38Z")

</div>

> [@Anil\_Kumar](#):
>
> I like to fill the 2D histogram as:

This kind of plot is not possible with 2D histograms simply because this kind of plot represent unbinned dat whereas 2D histograms are binned data. Use a TGraph instead.

---

<div class="post-metadata">

**Author:** ![Anil\_Kumar](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/anil_kumar/32/30817_2.png) [@Anil\_Kumar](https://root-forum.cern.ch/u/Anil_Kumar)\
**Post date:** [August 30, 2022, 9:41am UTC](https://root-forum.cern.ch/t/how-to-fill-2d-histogram/51398/6 "2022-08-30T09:41:39Z")

</div>

Ok @couet thank you.
