Plot 4D histogram

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,

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:


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:

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. 

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

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.

Also, shouldn’t it be:


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

this works!!

yes but this in not implement for TH3.

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

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.
plot4d->GetXaxis()->SetTitle(“X (mm)”);
plot4d->GetYaxis()->SetTitle(“Y (mm)”);
plot4d->GetZaxis()->SetTitle(“Z (mm)”);
Please suggest. Thanks in advance

Can you tell how you created plot4d ?

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.

3Dscat.C (2.8 KB)

Your macro does not work for me:

$ root basic.C
  | Welcome to ROOT 6.11/01            |
  |                                    (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'
/Users/couet/roottest/basic.C:90:4: error: use of undeclared identifier 'plot4d'
/Users/couet/roottest/basic.C:91:4: error: use of undeclared identifier 'plot4d'
/Users/couet/roottest/basic.C:92:4: error: use of undeclared identifier 'plot4d'
/Users/couet/roottest/basic.C:93:4: error: use of undeclared identifier 'plot4d'
/Users/couet/roottest/basic.C:94:4: error: use of undeclared identifier 'plot4d'
root [1] 

can you fix it ?

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,

b.root (146.8 KB)
3Dscat.C (3.1 KB)

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

The input file is here. And run this macro as I have changed the input file name.
3Dscat.C (3.1 KB)
DreiD_Data12.txt (1.6 MB)

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

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

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

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


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

   ifstream ReadData;;

   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);

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


   delete f;

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

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.