How to move the z-axis (colour palette) on a TH2 Histogram

I am currently creating a TH2F Histogram using the COLZ feature to draw it.

The z-axis is logarithmic with negative powers (10^-7, 10^-8 etc…) as such these values do not all appear on the canvas and I do not know how to shift the z-axis colour palette to the left a bit to resolve this problem (see attached file for an example of what I am trying to describe).


If anyone knows a solution, or why this is happening, i.e. I can see all it would be greatly appreciated.

Thanks in advance.

Joshua

After the Draw() do

   gPad->Update();
   TPaletteAxis *palette = (TPaletteAxis*)my2dhisto->GetListOfFunctions()->FindObject("palette");

   // the following lines move the palette. Choose the values you need for the position.
   palette->SetX1NDC(0.9);
   palette->SetX2NDC(0.95);
   palette->SetY1NDC(0.2);
   palette->SetY2NDC(0.8);
   gPad->Modified();
   gPad->Update();
1 Like

I have a similar issue, and I tried the code you provided. However, I now get a segmentation fault. (Of note, I am using ROOT as libraries, rather than coding in the ROOT interpreter.)

I tried this: (my TH2 is called hist2)

[code] gPad->Update();
TPaletteAxis palette = (TPaletteAxis)hist2->GetListOfFunctions()->FindObject(“palette”);

	// the following lines move the palette. Choose the values you need for the position.
	palette->SetX1NDC(0.9);
	palette->SetX2NDC(0.92);
	palette->SetY1NDC(0.2);
	palette->SetY2NDC(0.8);
	gPad->Modified();
	gPad->Update();[/code]

and I got this:

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================

Thread 2 (process 79603):
#0  0x00007fff8607dc0a in kevent ()
#1  0x00007fff8607fadd in _dispatch_mgr_invoke ()
#2  0x00007fff8607f7b4 in _dispatch_queue_invoke ()
#3  0x00007fff8607f2de in _dispatch_worker_thread2 ()
#4  0x00007fff8607ec08 in _pthread_wqthread ()
#5  0x00007fff8607eaa5 in start_wqthread ()

Thread 1 (process 79603):
#0  0x00007fff860e2b28 in wait4 ()
#1  0x00007fff860f70e6 in system ()
#2  0x000000010166e87c in TUnixSystem::StackTrace ()
#3  0x000000010166ba21 in TUnixSystem::DispatchSignals ()
#4  <signal handler called>
#5  0x00000001000064d2 in main ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x00000001000064d2 in main ()
===========================================================

Any ideas why this might happen?

Just a stupid question … did you draw your “hist2” with an appropriate drawing option (e.g. “CONTZ”) that created the “palette” before you tried to run the code that gave you that “segmentation violation”?

I did not.

You were supposed to …

That fixes it. Doh. Now I see it - the object doesn’t exist until created. Duh.

To add… it’s not sufficient to draw the histogram in my case. I need to save the canvas first, say into a pdf, and only then the the histogram has the palette function.

Not sure if this is expected, but would like to mention it for others facing a similar problem.

You need:

gPad->Modified();
gPad->Update();

That was my first thought indeed. Didn’t work, it was only after the SaveAs line that the segfault goes away.

I also tried RedrawAxis(), but that failed too.

Do you have a small reproducer ?

After investigating further it was because I ran my macro in the -b mode. If I ran it without -b, indeed just gPad->Modified(); gPad->Update(); is sufficient, but with -b the canvas needs to be saved.

I tried to follow up on this discussion to move/resize the colour palette.
It ended up giving a warning: null passed to a callee that requires a non-null argument [-Wnonnull]
palette->SetY2NDC(0.7);

Could you kindly say what could be a problem here:

graph_3D->Draw("lego2Z");	      
	      c_3D[i_file]->Update();
	      TPaletteAxis *palette = (TPaletteAxis*)graph_3D->GetListOfFunctions()->FindObject("palette");	      
	      palette->SetY2NDC(0.7);

_ROOT Version:6.08.00
_Compiler:x86_64_gcc48_sl6

graph_3D->Draw("lego2Z"); // it's a "graph", not a "histogram"
gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
TPaletteAxis *palette =
  (TPaletteAxis*)graph_3D->GetHistogram()->GetListOfFunctions()->FindObject("palette");
if (palette) {
  palette->SetY2NDC(0.7);
  gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn
}

how to change this to python?

I tried:

palette = hist_band_invt_mu_cut.GetListOfFunctions().FindObject("palette")                           
palette.SetX1NDC(0.9)

but this is not working as:

    palette.SetX1NDC(0.9)
AttributeError: 'TObject' object has no attribute 'SetX1NDC'

sorry my bad, I forgot to add

gPad.Update()

at the beginning.