# How to make LEGO plots using my own table data?

**URL:** <https://root-forum.cern.ch/t/how-to-make-lego-plots-using-my-own-table-data/62018>\
**Category:** Newbie\
**Tags:** graphics, hist\
**Created:** [October 25, 2024, 3:14pm UTC](https://root-forum.cern.ch/t/how-to-make-lego-plots-using-my-own-table-data/62018 "2024-10-25T15:14:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WeiZheng](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b77776/32.png) [@WeiZheng](https://root-forum.cern.ch/u/WeiZheng)\
**Post date:** [October 25, 2024, 3:14pm UTC](https://root-forum.cern.ch/t/how-to-make-lego-plots-using-my-own-table-data/62018/1 "2024-10-25T15:14:05Z")

</div>

Hi,  
I have a 2D data set which is saved as .txt file. The dimension is 17x24. When I use the following command lines to load the data and assign the data to the pointer hist like this

```
std::ifstream input_file("mytxt.txt"); // there are 17 x 24 values in the .txt file, the delimiter is " ". 
// one value in the file could be 100.5, or 200, or 12. 
TH2D *hist = new TH2D("hist", "2D Histogram from Text File", 17, 0, 17, 24, 0, 24);

```

the generated histogram is not correct. Since there would be 17x24 grids, for each grid, the height value would be assigned the specific value extracted from the .txt file.

How can I generate such a LEGO

 ![Screenshot from 2024-10-25 11-05-25](https://root-forum.cern.ch/uploads/default/original/3X/1/5/15a038e26cbaec06ccdecb5732ff73aa0185750a.png)  
plot. Since I am a newbie in ROOT, could you please give me more details.

Thank you very much.

Wei

\_ROOT Version:\_6.32  
_Platform:_ ubuntu  
_Compiler:_ g++

---

<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:** [October 26, 2024, 5:40am UTC](https://root-forum.cern.ch/t/how-to-make-lego-plots-using-my-own-table-data/62018/2 "2024-10-26T05:40:56Z")

</div>

Use [SetBinContent(binx,biny,value)](https://root.cern/doc/master/classTH2.html#a88d8a33f395f594543da88d2d1758bd2) to assign each data value from the file to _each bin_. E.g.

```auto
   for (Int_t i = 1; i <= nbinsx; i++) {
      for(Int_t j = 1; j <= nbinsy; j++){
         histo->SetBinContent(i, j, value_for_bin_i_j);
      }
   }

```

Notice that binx and biny are _bin numbers_, not the values seen on the x and y axis labels, and bin numbers in x and y go from 1 to NbinsX and NbinsY, respectively. So you somehow _have to know or determine_ the corresponding bin number in order to send each data to its correct bin.  
Note also, as the name implies, that SetBinContent overwites any existing bin contents on the bin; so if your data happens to contain several entries for one same bin (x,y), it will just be overwritten every time you use SetBinContent on that same bin.
