You are here

How to redraw Axis and plot borders?

Sometimes, when several plots are drawn in the same pad using the option SAME, the axis tick marks and the plot's border lines might be erased by the front plots. It might be useful to redraw them. The following example shows how to proceed.

void redraw()
{
   const int n = 10;
   auto h = new TH2D("h", "", n, 0, 1, n, 0, 1);
   h->SetBit(TH1::kNoStats);
   for (int i=1; i<=n; ++i)
      for (int j=1; j<=n; ++j)
         h->SetBinContent(i, j, 1);

   auto h2 = new TH2D("h2", "", n, 0, 1, n, 0, 1);
   h2->Fill(0.5, 0.5);

   auto c = new TCanvas("c", "c", 0, 0, 600, 300);
   c->Divide(2, 1);

   c->cd(1);
   h2->Draw("col");
   gPad->SetGridy(); gPad->SetGridx();

   c->cd(2);
   h->Draw("col");
   gPad->SetGridy(); gPad->SetGridx();
   redrawBorder();
}

void redrawBorder()
{
   // this little macro redraws the axis tick marks and the pad border lines.
   gPad->Update();
   gPad->RedrawAxis();
   TLine l;
   l.DrawLine(gPad->GetUxmin(), gPad->GetUymax(), gPad->GetUxmax(), gPad->GetUymax());
   l.DrawLine(gPad->GetUxmax(), gPad->GetUymin(), gPad->GetUxmax(), gPad->GetUymax());
}