Displaying Z axis graduation in Colz option

This has been an issue for as long as I’ve used ROOT. It is even worse when you add a label to the color bar. It almost is never visible without making modifications to the margins. The middle pad has a label that is nearly hidden, and the right pad has the margin set as @couet suggested. In addition, the stats box is always in the way on TH2 histograms.

#include <vector>

#include "TCanvas.h"
#include "TF2.h"
#include "TH2D.h"

{
   std::vector< TH2D* > hists;

   TF2 *f = new TF2("xygaus","xygaus", -2, 2, -2, 2);
   f->SetParameters(1,0,1,0,1);

   for (int i=0;i<3;i++) {
      hists.push_back(new TH2D(Form("h%d",i),"H", 100, -2, 2 ,100, -2 ,2));
      hists.back()->FillRandom("xygaus",5000);
   }

   TCanvas *c = new TCanvas("c","C");
   c->Divide(3,1);

   //Set the labels on the Z-axis
   hists.at(1)->GetZaxis()->SetTitle("A Z label");
   hists.at(2)->GetZaxis()->SetTitle("A Z label with Margin");

   //Turn of the stats so we can see the label.
   hists.at(2)->SetStats(false);
   //Change the right margin of the last pad tso the label is visible
   c->cd(3)->SetRightMargin(0.18);

   //Draw the hists
   for (int i=0; i<=2;i++) {
      c->cd(i+1);
      hists.at(i)->Draw("COLZ");
   }
}

Hi All,
Thanks for replying this one “c->cd(3)->SetRightMargin(0.18);” works thanks so much.
So what about the other issue? because I can’t see any change that could be the cause of the issue in the second code.
Thanks in advance.
Cheers

What do you mean by “it does not draw” ? you see an empty pad ? do you get some error message ?

In that particular example (as often) it is very hard to define a place where the stat box will not hide something: the palette, the X axis, The Y axis of the histogram itself which occupied the whole plotting space in case of option COL. We can consider using the tool recently developed for TLegend to find automatically an empty space where to put the stats. But I fear tha,t in that case, there is none and it is up to the user to choose what he wants to hide.

I understand that is true. At the very least it would be nice if it didn’t cover the color bar and was set slightly further left for TH2. (The TH1 placement seems not to get in the way often.) My default these day is to place the stats box slightly left and then reduce the alpha so that some of the data can be seen.

I’ve been using this little routine if this is useful for anyone:

/**Updates the statistics box for a given histogram. User may provide the
 * statics options, fit options, a vector of positions for the box, the fill
 * color, and alpha.
 * \note The statistics box may not be created until the histogram is drawn and
 * the canvas containing it is updated. It is suggested to mark the pad as
 * modified after updating the statistics box. For example:
 *
 * \code
 * gPad->Update();
 * UpdateStats(hist);
 * gPad->Modified();
 * \endcode
 * 
 * @author Karl Smith
 * \param[in] hist Pointer to the histogram to work on.
 * \param[in] optStat The option code for the statistics information.
 * \param[in] optFit The option code for the fit information.
 * \param[in] posNdc A vector of coordinates defining the box position. They
 *    should be in the following order: X1, Y1, X2, Y2.
 * \param[in] fillColor The fill color for the statistics box.
 * \param[in] fillAlpha The alpha value for the fill rangin from 0
 *    (transparent) to 1 (opaque).
 * \return The pointer to the stats box, null if an error occurred.
 */
const TPaveStats *UpdateStats(const TH1 *hist,
      const int &optStat = 1111, const int &optFit = 111,
      const std::vector< double > posNdc = std::vector< double >(),
      const Color_t &fillColor = kWhite, const float &fillAlpha = 1 )
{
   if (! hist) return nullptr;

   TPaveStats *stats = (TPaveStats*) hist->GetListOfFunctions()->FindObject("stats");
   if (stats) {
      stats->SetOptStat(optStat);
      stats->SetOptFit(optFit);
      if ( posNdc.size() == 4 ) {
         stats->SetX1NDC(posNdc.at(0));
         stats->SetY1NDC(posNdc.at(1));
         stats->SetX2NDC(posNdc.at(2));
         stats->SetY2NDC(posNdc.at(3));
      }
      else if ( ! posNdc.empty() ) {
         std::cerr << "ERROR: Invalid number of positions, " << posNdc.size() << ", provided!\n";
      }
      else if (dynamic_cast< const TH2* >(hist)) {
         stats->SetX1NDC(stats->GetX1NDC() - 0.1);
         stats->SetX2NDC(stats->GetX2NDC() - 0.1);
      }
      stats->SetFillColorAlpha(fillColor, fillAlpha);
   }

   return stats;
}

Yes this is a possibility… Note the Alpha is not available with X11…

Interesting, could you place a note to that affect in TAttFill or TAttFill::SetFillColorAlpha?

The opposite is already said (i.e. where it is available):
https://root.cern/doc/master/classTAttFill.html#F1

1 Like

Hi All,
sorry for my late reply, I was in a meeting.
So here is what I got with my last code


Cheers

Is there content in your histograms?
TH2::GetNumEntries

Although I believe the color bar does not show up if the histogram is empty.

More likely that your scaling is wrong.

printf("Scaling: %f\n", scale1);

Hi,
The histograms are not empty for instance if I get rid of gPad->SelLogz the plots are drawn.
if I do “printf(“Scaling: %f\n”, scale1);” I got this value 0.000026
cheers

And the histogram limits?

printf("Min: %f Max: %f\n", hist->GetMinimum(), hist->GetMaximum())

It may be faster if you could post the files.

Hi,
I put the files on my public area: /afs/cern.ch/user/d/diboye/public
here what I got Min: 0.000000 Max: 0.603430 if I do "printf(“Min: %f Max: %f\n”, hist->GetMinimum(), hist->GetMaximum())"
Cheers

Some of us do not have access to afs (including me).

Hi,
Ok I sent them here then.
E12cmfoMtotVsDeltaM_200_GeV.root (112.0 KB)
E12cmfoMtotVsDeltaM_400_GeV.root (114.1 KB)
E12cmfoMtotVsDeltaM_600_GeV.root (110.9 KB)
E12cmfoMtotVsDeltaM_800_GeV.root (110.9 KB)
Cheers

I’ll need the headers as well:

root [0]
Processing main.C...
In file included from input_line_8:1:
main.C:6:10: fatal error: 'AtlasLabels.h' file not found
#include "AtlasLabels.h"
         ^
main.C:7:10: fatal error: 'AtlasStyle.h' file not found
#include "AtlasStyle.h"
         ^
main.C:8:10: fatal error: 'AtlasUtils.h' file not found
#include "AtlasUtils.h"
         ^
main.C:9:10: fatal error: 'AtlasUtils.C' file not found
#include "AtlasUtils.C"
         ^
main.C:28:10: fatal error: 'AtlasStyle.h' file not found
#include "AtlasStyle.h"
         ^
main.C:35:3: error: use of undeclared identifier 'SetAtlasStyle'
  SetAtlasStyle();
  ^
main.C:98:7: error: use of undeclared identifier 'myText'
      myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
      ^
main.C:99:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.75, 1, "Higgs set to 200 GeV");
     ^
main.C:100:6: error: use of undeclared identifier 'ATLASLabel'
     ATLASLabel(0.3,0.85,"Work In Progress");
     ^
main.C:104:9: error: use of undeclared identifier 'myText'
        myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
        ^
main.C:105:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.75, 1, "Higgs set to 400 GeV");
     ^
main.C:106:6: error: use of undeclared identifier 'ATLASLabel'
     ATLASLabel(0.3,0.85,"Work In Progress");
     ^
main.C:113:9: error: use of undeclared identifier 'myText'
        myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
        ^
main.C:114:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.75, 1, "Higgs set to 600 GeV");
     ^
main.C:115:6: error: use of undeclared identifier 'ATLASLabel'
     ATLASLabel(0.3,0.85,"Work In Progress");
     ^
main.C:121:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.65, 1, "#sqrt{s}= 13 TeV");
     ^
main.C:122:6: error: use of undeclared identifier 'myText'
     myText(       0.20,  0.75, 1, "Higgs set to 800 GeV ");
     ^
main.C:123:6: error: use of undeclared identifier 'ATLASLabel'
     ATLASLabel(0.3,0.85,"Work In Progress");
     ^

You need to adjust the minimum:

Hist_E12VsM12_200_GeV->SetMinimum(scale1);
Hist_E12VsM12_400_GeV->SetMinimum(scale2);
Hist_E12VsM12_600_GeV->SetMinimum(scale3);
Hist_E12VsM12_800_GeV->SetMinimum(scale4);

Hi,
Thanks so much it works :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.