# Plot 4D histogram

**URL:** <https://root-forum.cern.ch/t/plot-4d-histogram/26419>\
**Category:** ROOT\
**Created:** [October 2, 2017, 12:52pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419 "2017-10-02T12:52:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arnab7](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arnab7](https://root-forum.cern.ch/u/Arnab7)\
**Post date:** [October 2, 2017, 12:52pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/1 "2017-10-02T12:52:50Z")

</div>

Hi all,  
I am quite new in root. I have a data set containing x,y,z and count. Now I want to plot this data with color scale (I am getting scatter plot with black points which looks pretty boring!!). But I am unable to do it presently. For your convenience I am giving you one example what I want. If you have any idea of making it even beatiful looking please suggest.

Thanks in advance,  
A

 ![41-44054-3D_edep-1](https://root-forum.cern.ch/uploads/default/original/2X/5/58d0b8ba4423d95ffe43c928a7c887759eb4c3ed.png)

---

<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:** [October 2, 2017, 1:14pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/2 "2017-10-02T13:14:27Z")

</div>

The best is to put your variables into an tree or ntuple (simpler tree). Let say you have x, y, z and count into a ntuple you can do:

```auto
ntuple->Draw("count:z:y:x","","COLZ")

```

---

<div class="post-metadata">

**Author:** ![Graipher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/graipher/32/11624_2.png) [@Graipher](https://root-forum.cern.ch/u/Graipher)\
**Post date:** [October 2, 2017, 1:33pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/3 "2017-10-02T13:33:06Z")

</div>

Why is this not in the documentation? According to that we can only draw it as (black and white) scatter plot or different variants of box plots. A colored scatterplot is not mentioned:

**[Options supported for 3D histograms:](https://root.cern.ch/doc/master/classTHistPainter.html#HP01d)**

```auto
Option Description
" " Default (scatter plot).
"ISO" Draw a Gouraud shaded 3d iso surface through a 3d histogram. It paints one surface at the value computed as follow: SumOfWeights/(NbinsX*NbinsY*NbinsZ).
"BOX" Draw a for each cell with volume proportional to the content's absolute value. An hidden line removal algorithm is used
"BOX1" Same as BOX but an hidden surface removal algorithm is used
"BOX2" The boxes' colors are picked in the current palette according to the bins' contents
"BOX2Z" Same as "BOX2". In addition the color palette is also drawn.
"BOX3" Same as BOX1, but the border lines of each lego-bar are not drawn.
"LEGO" Same as BOX. 

```

---

<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:** [October 2, 2017, 1:47pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/4 "2017-10-02T13:47:11Z")

</div>

> [@Graipher](#):
>
> Why is this not in the documentation?

Thats tree specific plots (you need 4 variables) so it is in the TTree::Draw doc

---

<div class="post-metadata">

**Author:** ![Graipher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/graipher/32/11624_2.png) [@Graipher](https://root-forum.cern.ch/u/Graipher)\
**Post date:** [October 2, 2017, 2:10pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/5 "2017-10-02T14:10:29Z")

</div>

Well, you have 4 variables with a `TH3?`, x, y, z, count. Note that there _is_ an option to draw boxes which are colored according to the bin content (`"BOX2"`), just not to draw a scatter plot with the same.

---

<div class="post-metadata">

**Author:** ![Graipher](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/graipher/32/11624_2.png) [@Graipher](https://root-forum.cern.ch/u/Graipher)\
**Post date:** [October 2, 2017, 2:13pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/6 "2017-10-02T14:13:46Z")

</div>

> [@couet](#):
>
> ntuple-\>Draw(“count:z:y:x”,“”,“COLZ”)

Also, shouldn’t it be:

```auto
ntuple->Draw("x:y:z:count","","COLZ")

```

When trying it out this seems to work correctly (with `count` being mapped to the color).

---

<div class="post-metadata">

**Author:** ![Arnab7](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arnab7](https://root-forum.cern.ch/u/Arnab7)\
**Post date:** [October 2, 2017, 2:15pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/7 "2017-10-02T14:15:57Z")

</div>

Yes  
ntuple-\>Draw(“x:y:z:count”,"",“COLZ”);  
this works!!

---

<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:** [October 2, 2017, 3:36pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/8 "2017-10-02T15:36:14Z")

</div>

> [@Graipher](#):
>
> Well, you have 4 variables with a TH3?, x, y, z, count.

yes but this in not implement for TH3.

> [@Graipher](#):
>
> Also, shouldn’t it be:
> 
> ntuple-\>Draw(“x:y:z:count”,“”,“COLZ”)

Yes I quickly typed without checking what would be the right order.

---

<div class="post-metadata">

**Author:** ![Arnab7](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arnab7](https://root-forum.cern.ch/u/Arnab7)\
**Post date:** [October 2, 2017, 4:13pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/9 "2017-10-02T16:13:11Z")

</div>

Hi all,  
Two more questions. Now was trying to plot my data.  
There are two problems:  
1. I can not set the minimum count in the color bar. Where I don’t have data still I get ‘sky blue’ color.  
2. I can not write the axis title in X-axis. And Z-axis title comes in two places, along Z-axis and along color bar as shown in the figure.  
I am using the lines below.  
ntuple-\>Draw(“x:y:z:cont\>\>plot4d”,"",“COLZ”);  
plot4d-\>GetXaxis()-\>SetTitle(“X (mm)”);  
plot4d-\>GetYaxis()-\>SetTitle(“Y (mm)”);  
plot4d-\>GetZaxis()-\>SetTitle(“Z (mm)”);  
plot4d-\>GetXaxis()-\>SetTitleOffset(1.5);  
plot4d-\>GetYaxis()-\>SetTitleOffset(1.8);  
plot4d-\>GetZaxis()-\>SetTitleOffset(1.5);  
plot4d-\>GetXaxis()-\>CenterTitle(true);  
plot4d-\>GetYaxis()-\>CenterTitle(true);  
plot4d-\>GetZaxis()-\>CenterTitle(true);  
plot4d-\>SetMinimum(3.);  
Please suggest. Thanks in advance

 ![momenta](https://root-forum.cern.ch/uploads/default/original/2X/c/c8918e0255a719d0bfa973208885144b1fb8bc4e.png)

---

<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:** [October 3, 2017, 7:24am UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/10 "2017-10-03T07:24:03Z")

</div>

Can you tell how you created plot4d ?

---

<div class="post-metadata">

**Author:** ![Arnab7](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arnab7](https://root-forum.cern.ch/u/Arnab7)\
**Post date:** [October 3, 2017, 9:54am UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/11 "2017-10-03T09:54:56Z")

</div>

Hi Olivier,  
I am attaching the code I am using right now. Now I have able to remove the ‘sky-blue’ (where the count is zero) by putting a condition while writing the ntuple (if(count\>0)). But of course I am still not able to vary the limit of the color scale.  
Secondly, I found there is an additional problem. When I am creating the plot from ntuple it looks like some modification is happening due to binning. I am attaching both the plots for your convenience. Please suggests.  
Regards,  
Arnab

[3Dscat.C](https://root-forum.cern.ch/uploads/default/original/2X/3/33936a8d4a63df74fdf90162bc0a3e916cf04898.C) (2.8 KB)

 ![momenta2](https://root-forum.cern.ch/uploads/default/original/2X/d/d7f8a2d2740f8fad1bedb62b454bd556e5e5021c.png) ![momenta1](https://root-forum.cern.ch/uploads/default/original/2X/f/fbe1b3d7cf641c9f9783302f73a68241dea71720.png)

---

<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:** [October 3, 2017, 11:02am UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/12 "2017-10-03T11:02:33Z")

</div>

Your macro does not work for me:

```auto
$ root basic.C
   -----------------------------------------------------------------
  | Welcome to ROOT 6.11/01 http://root.cern.ch |
  | (c) 1995-2017, The ROOT Team |
  | Built for macosx64 |
  | From heads/master@v6-09-02-2768-g3348aeb, Sep 29 2017, 17:54:00 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   -----------------------------------------------------------------

root [0] 
Processing basic.C...
In file included from input_line_14:1:
/Users/couet/roottest/basic.C:86:4: error: use of undeclared identifier 'plot4d'
   plot4d->GetXaxis()->SetTitle("X-Momentum (au)");
   ^
/Users/couet/roottest/basic.C:87:4: error: use of undeclared identifier 'plot4d'
   plot4d->GetYaxis()->SetTitle("Y-Momentum (au)");
   ^
/Users/couet/roottest/basic.C:88:4: error: use of undeclared identifier 'plot4d'
   plot4d->GetZaxis()->SetTitle("Z-Momentum (au)");
   ^
/Users/couet/roottest/basic.C:89:4: error: use of undeclared identifier 'plot4d'
   plot4d->GetXaxis()->SetTitleOffset(1.5);
   ^
/Users/couet/roottest/basic.C:90:4: error: use of undeclared identifier 'plot4d'
   plot4d->GetYaxis()->SetTitleOffset(1.8);
   ^
/Users/couet/roottest/basic.C:91:4: error: use of undeclared identifier 'plot4d'
   plot4d->GetZaxis()->SetTitleOffset(1.5);
   ^
/Users/couet/roottest/basic.C:92:4: error: use of undeclared identifier 'plot4d'
   plot4d->GetXaxis()->CenterTitle(true);
   ^
/Users/couet/roottest/basic.C:93:4: error: use of undeclared identifier 'plot4d'
   plot4d->GetYaxis()->CenterTitle(true);
   ^
/Users/couet/roottest/basic.C:94:4: error: use of undeclared identifier 'plot4d'
   plot4d->GetZaxis()->CenterTitle(true);
   ^
root [1] 

```

can you fix it ?

---

<div class="post-metadata">

**Author:** ![Arnab7](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arnab7](https://root-forum.cern.ch/u/Arnab7)\
**Post date:** [October 3, 2017, 12:52pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/13 "2017-10-03T12:52:26Z")

</div>

Can you check if the new one is working for you?  
The previous one also working fine for me. May be as I am using Windows!!  
I am attaching the output root file as well if it helps.  
Best regards,  
Arnab

[b.root](https://root-forum.cern.ch/uploads/default/original/2X/b/b194927efd7f75f024ea3f2dc3e6eec8ba916815.root) (146.8 KB)  
[3Dscat.C](https://root-forum.cern.ch/uploads/default/original/2X/0/07d9f4c503478263a8793fa47da1cb5d18b5bae3.C) (3.1 KB)

---

<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:** [October 3, 2017, 12:55pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/14 "2017-10-03T12:55:22Z")

</div>

```auto
root [0] 
Processing basic.C...
could not open the input file...
root [1] 

```

---

<div class="post-metadata">

**Author:** ![Arnab7](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arnab7](https://root-forum.cern.ch/u/Arnab7)\
**Post date:** [October 3, 2017, 1:01pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/15 "2017-10-03T13:01:01Z")

</div>

The input file is here. And run this macro as I have changed the input file name.  
[3Dscat.C](https://root-forum.cern.ch/uploads/default/original/2X/8/83b3c18a70df969a6846cac7559b077bc714700a.C) (3.1 KB)  
[DreiD\_Data12.txt](https://root-forum.cern.ch/uploads/default/original/2X/6/6172678acb247eac6e3d7528f223968f55b0eada.txt) (1.6 MB)

---

<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:** [October 3, 2017, 1:10pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/16 "2017-10-03T13:10:12Z")

</div>

You never draw h3 … can I remove the code related to it ?

---

<div class="post-metadata">

**Author:** ![Arnab7](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arnab7](https://root-forum.cern.ch/u/Arnab7)\
**Post date:** [October 3, 2017, 1:13pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/17 "2017-10-03T13:13:41Z")

</div>

Sure!!  
Actually I was drawing it before to see the black and white scatter plot.

---

<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:** [October 3, 2017, 1:20pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/18 "2017-10-03T13:20:15Z")

</div>

COLZ option for 4D plot in ntuple ignores the histogram. You should do:

```auto
void basic (const Char_t*fdata="DreiD_Data12.txt", const Char_t*froot="b.root")
{

    gStyle->SetOptStat(0);

   TCanvas *c1 = new TCanvas("c1","aa",0,0,600,600);

   c1->SetRightMargin(0.15);
   c1->SetBottomMargin(0.2);
   c1->SetLeftMargin(0.2);
   ifstream ReadData;

   ReadData.open(fdata);

   if (!ReadData.is_open()){
      cout<<"could not open the input file..."<<endl;
      return -1;
   }

   double x,y,z,count;
   int nlines = 0;
   TFile *f = new TFile(froot,"RECREATE");

   TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","x:y:z:cont");

   while (ReadData.good()) {
      ReadData >> x >> y >>z >> count;
      if (ReadData.good()) {
         if(count>0) ntuple->Fill(x,y,z,count);
         nlines++;
      }
   }

   cout<<"number of lines: "<<nlines<<endl;

   ntuple->Draw("x:y:z:cont","","COLZ");

   ReadData.close();
   f->Write();
   delete f;
}

```

---

<div class="post-metadata">

**Author:** ![Arnab7](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/b2d939/32.png) [@Arnab7](https://root-forum.cern.ch/u/Arnab7)\
**Post date:** [October 3, 2017, 1:41pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/19 "2017-10-03T13:41:28Z")

</div>

Alright.  
But the binning problem still remains there. Can we do something with that?

 ![momenta](https://root-forum.cern.ch/uploads/default/original/2X/3/31aa72da9bfcaf2b443e2e59819c0e29a3293a8b.png)

---

<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:** [October 3, 2017, 1:50pm UTC](https://root-forum.cern.ch/t/plot-4d-histogram/26419/20 "2017-10-03T13:50:23Z")

</div>

With COLZ and 4 variables there is no binning. A dot is drawn at the position x y x and the color of the dot is computed according to the 4th variable. If you want a 3D histogram you can fill it as you did before and then draw it with one the the 3D histogram ploting options listed [here](https://root.cern/doc/master/classTHistPainter.html#HP25).

[Next page](https://root-forum.cern.ch/t/plot-4d-histogram/26419.md?page=2)
