# 3D Histogram with Logarithmic Color Bar in PyRoot

**URL:** <https://root-forum.cern.ch/t/3d-histogram-with-logarithmic-color-bar-in-pyroot/55211>\
**Category:** Newbie\
**Tags:** graphics, rdataframe\
**Created:** [June 6, 2023, 8:27pm UTC](https://root-forum.cern.ch/t/3d-histogram-with-logarithmic-color-bar-in-pyroot/55211 "2023-06-06T20:27:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Matava](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/william_matava/32/33353_2.png) [@William\_Matava](https://root-forum.cern.ch/u/William_Matava)\
**Post date:** [June 6, 2023, 8:27pm UTC](https://root-forum.cern.ch/t/3d-histogram-with-logarithmic-color-bar-in-pyroot/55211/1 "2023-06-06T20:27:01Z")

</div>

Hi all,  
I’ve made a 3d histogram from an RDataFrame, drawn with the lego2 style along with a colorbar to emphasize the scale of the “fourth dimension”. However, I’d like to make this colorbar’s scale logarithmic.

Here’s the highlights of my (functioning) PyRoot Code:

> > import ROOT  
> > c1 = ROOT.TCanvas( ‘EnergySlice’, ‘Energy Deposition in Horizontal Slice’, 200, 10, 850, 700 )  
> > gr = dffilt.Histo3D((“test”, " Title ",50, -25.0, 25.0, 50, -25.0, 25.0, 38, -19.0, 19.0),“rx”, “ry” ,“rz”, “Edep”)  
> > gr.Draw(“LEGO2Z”)

The documentation I’ve found online for similar issues recommends the method c1.SetLogz(), and while this works for a 2D histogram, it seems to interfere with both the z-axis and the colormap in the 3D case.

Thanks!

---

<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:** [June 7, 2023, 4:43am UTC](https://root-forum.cern.ch/t/3d-histogram-with-logarithmic-color-bar-in-pyroot/55211/2 "2023-06-07T04:43:21Z")

</div>

“Interferes” how? Can you show what you get and what is wrong with it (what do you want to get)? A minimal working reproducer that we can run would also help.  
By the way, note that if there are empty bins, the log scale may look strange, so you may want to do `histo->SetMinimum(1)` or some other small number, depending on your histogram. E.g.

```auto
{
   auto c = new TCanvas("c","c",1000,400);
   //c->SetLogz(1);
   c->Divide(2,1);
   gStyle->SetOptStat(kFALSE);

   auto h = new TH3F("h","h",5,-2,2,5,-2,2,4,0,4);
   double x, y, z;
   for (Int_t i=0;i<50000;i++) {
      gRandom->Rannor(x, y);
      z = x*x + y*y;
      h->Fill(x,y,z);
   }

   c->cd(1);
   gPad->SetLogz(1);
   h->DrawCopy("lego2z");

   c->cd(2);
   gPad->SetLogz(1);
   h->SetMinimum(1);
   h->Draw("lego2z");
}

```

 ![c](https://root-forum.cern.ch/uploads/default/original/3X/5/c/5c679911599836ec9b128ee60714fa9358aa1d4b.png)

---

<div class="post-metadata">

**Author:** ![William\_Matava](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/william_matava/32/33353_2.png) [@William\_Matava](https://root-forum.cern.ch/u/William_Matava)\
**Post date:** [June 7, 2023, 7:04pm UTC](https://root-forum.cern.ch/t/3d-histogram-with-logarithmic-color-bar-in-pyroot/55211/3 "2023-06-07T19:04:27Z")

</div>

Good catch on the singularity of log for empty binsl That actually partially solves the issue, but another persists. Below is my PyROOT code, and attached a small data file:  
[example.root](https://root-forum.cern.ch/uploads/short-url/czDEUcfvDN7O7YClPC1LVULHh5Z.root) (1.6 MB)

```auto
import ROOT
#File name
rootFile = "example.root"

#Defining Canvas
c1 = ROOT.TCanvas( 'c1', 'A Simple Example', 200, 10, 700, 700 )
    
#Importing Root file
df = ROOT.RDataFrame("events", rootFile)

#Casting vectors back to strings, so that filtering works properly 
df = df.Redefine("volume", "std::string(volume.data())")
df = df.Redefine("particle", "std::string(particle.data())")
df = df.Redefine("nextVolume", "std::string(nextVolume.data())")
df = df.Redefine("process", "std::string(process.data())")
df = df.Redefine("rz", "rz + 100")
#Defining the 3D histogram
gr = df.Histo3D(("graph", "Total Energy Deposition ",50, -25.0, 25.0, 50, -25.0, 25.0, 38, 80, 120),"rx", "ry" ,"rz", "Edep")
#Drawing
gr.Draw("LEGO2Z")

gr.GetXaxis().SetTitle( 'X (mm)' )
gr.GetYaxis().SetTitle( 'Y (mm)' )
gr.GetZaxis().SetTitle( 'Z (mm)' )
   
# c1.SetLogz()
c1.Update()

```

The two plots below are generated based on whether the line ‘’‘c1.SetLogz()’‘’ is left uncommented. Note that in the second plot, both the z axis and the color bar’s scales are logarithmic, despite the two axes conveying entirely different information. My goal is to have only the colorbar’s axis be log.

 ![Screenshot 2023-06-07 at 11.39.57 AM](https://root-forum.cern.ch/uploads/default/original/3X/7/e/7e0c311bb95345a14248ad1cbb39aeb8e3050108.jpeg)  
 ![Screenshot 2023-06-07 at 11.39.00 AM](https://root-forum.cern.ch/uploads/default/original/3X/e/8/e8205efae33d398ba087271a25cc9aca67239577.jpeg)

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 8, 2023, 8:14pm UTC](https://root-forum.cern.ch/t/3d-histogram-with-logarithmic-color-bar-in-pyroot/55211/4 "2023-06-08T20:14:11Z")

</div>

Hi,

if I understand correctly this is less about RDF and more about graphics. Hopefully @couet can help, let’s ping him.

Cheers,  
Enrico

---

<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:** [June 9, 2023, 7:11am UTC](https://root-forum.cern.ch/t/3d-histogram-with-logarithmic-color-bar-in-pyroot/55211/5 "2023-06-09T07:11:00Z")

</div>

I’ll look but as @dastudillo said your Z axis starts at 0, which is not define in log scale. What about if you set a povisitive minimum ? does the white area in the color palette disapear ?
