# 3D Histogram - Labeling the Color Bar

**URL:** <https://root-forum.cern.ch/t/3d-histogram-labeling-the-color-bar/35355>\
**Category:** Newbie\
**Created:** [July 25, 2019, 9:03pm UTC](https://root-forum.cern.ch/t/3d-histogram-labeling-the-color-bar/35355 "2019-07-25T21:03:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ethan\_Nguyen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@Ethan\_Nguyen](https://root-forum.cern.ch/u/Ethan_Nguyen)\
**Post date:** [July 25, 2019, 9:03pm UTC](https://root-forum.cern.ch/t/3d-histogram-labeling-the-color-bar/35355/1 "2019-07-25T21:03:56Z")

</div>

Hi,

I’ve enclosed a photo of the 3D histogram. How do I label the colored bar?

Thank you so much!

ROOT v6.18 on MacOS 10.14.5

 ![42%20PM](https://root-forum.cern.ch/uploads/default/original/3X/1/1/115e43ec20b99df2dc92c7edb3ca41e6d8cfcca0.jpeg)

```auto
void sandbox(){

   struct input_t

   {
     Double_t transmittance;
   };

   input_t input;

   FILE *f = fopen("Crystal5478_360.txt","r");
   char line[400];

   Double_t transArray[196];

   Int_t j=0;
   TFile *file = new TFile("test.root","RECREATE");
   for(Int_t i;fgets(&line[0],500,f);i++){  
    if (line[0] == '#'){
      continue;
    }else{
      sscanf(&line[0], "%lf", &input.transmittance);
    }

      transArray[j]=input.transmittance;
      j++;
      
   }
   
  float avg = 0.0; 
  float sum = 0.0;
  int size;
  
  size = sizeof(transArray) / sizeof(transArray[j]);

 for (int i = 0; i < size; ++i){
   sum += transArray[i];
  }
 
  avg = ((float)sum)/size; //or cast sum to double before division

  Double_t dev[size];
  
  for(int i = 0; i < size-1; i ++)
  {
      dev[i]= transArray[i] - avg;
      //cout << "Deviance " << dev[i] << "\n";
  }

   /* for(int i = 0; i < 196; i ++)
  {
      
      cout << transArray[i] << "\n";
      }*/
   
   TH3F *hist = new TH3F("glvoxel", "Optical Transmittance", size, 0., size, 1, 0., 20., 1, 0., 20.);

   //Fill the histogram to create a "sphere". CHANGE THIS!!
   /*for (UInt_t i = 0; i < nZ; ++i) {
      const Double_t z = zMin + i * zStep;
      for (UInt_t j = 0; j < nY; ++j) {
         const Double_t y = yMin + j * yStep;
         for (UInt_t k = 0; k < nX; ++k) {
            const Double_t x = xMin + k * xStep;
            const Double_t val = 20. - (x * x + y * y + z * z);
            hist->SetBinContent(k + 1, j + 1, i + 1, val*100);
         }
      }
      }*/

   for (UInt_t i = 0; i < size; ++i) {
     hist->Fill(i,10,10, dev[i]);
         }
     

   hist->GetXaxis()->SetTitle("Position (mm)");
   hist->GetXaxis()->SetTitleOffset(-.5);
   //hist->GetWaxis()->SetTitle("Optical Transmittane Deviance");
   
   hist->SetTitle("OT");
   gStyle->SetCanvasPreferGL(1);
   hist->Draw("glcolz");

   TPaveLabel *title = new TPaveLabel(-1., 0.86, 1., 0.98,
				      "Sample 5478 (360 nm) - Position vs Optical Transmittance Deviance");
   
   title->SetFillColor(32);
   title->Draw();  
}

```

---

<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:** [July 26, 2019, 5:44am UTC](https://root-forum.cern.ch/t/3d-histogram-labeling-the-color-bar/35355/2 "2019-07-26T05:44:26Z")

</div>

I don’t think there’s an automatic way in this case, but you can add your own text on the canvas and place it next to the scale. For instance, you could add another TPaveLabel, if you want it in a box. For just text you can use TLatex; something like:

```auto
  TLatex *tex = new TLatex(0.85,0.05,"title");
  tex->SetNDC(1);
  tex->SetTextFont(42);
  tex->Draw();

```

after drawing the histogram.

---

<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:** [July 26, 2019, 6:23am UTC](https://root-forum.cern.ch/t/3d-histogram-labeling-the-color-bar/35355/3 "2019-07-26T06:23:59Z")

</div>

Try:

```auto
   TH3F *hist = new TH3F("glvoxel", "OT;Position (mm);;;Optical Transmittane Deviance", size, 0., size, 1, 0., 20., 1, 0., 20.);

```

---

<div class="post-metadata">

**Author:** ![Ethan\_Nguyen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@Ethan\_Nguyen](https://root-forum.cern.ch/u/Ethan_Nguyen)\
**Post date:** [July 26, 2019, 2:51pm UTC](https://root-forum.cern.ch/t/3d-histogram-labeling-the-color-bar/35355/4 "2019-07-26T14:51:00Z")

</div>

Thank you!  
@couet Also, do you all know how to make the 3D histogram opaque?

I tried setting the transparency through the color palette, but that didn’t work.

```auto
 gStyle->SetPalette(1,0,1);

```

---

<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:** [July 26, 2019, 3:49pm UTC](https://root-forum.cern.ch/t/3d-histogram-labeling-the-color-bar/35355/5 "2019-07-26T15:49:31Z")

</div>

> [@Ethan\_Nguyen](#):
>
> @couet Also, do you all know how to make the 3D histogram opaque?

I think this representation is also transparent. @tpochep may confirm .
