Vertical shaded area

Please find below an improved version of the Olivier’s example source code.
Two things are fixed:

  1. “y-low” and “y-high” coordinates for the transparent TPad were miscalculated,
  2. “y-low” and “y-high” coordinates for all TLine and the TBox were hard-coded fixed numbers.
    For the completeness of this example, I added a “horizontal” shaded area.
{
   TF2 *f     = new TF2("f", "x*x+y*y", -3, 3, -5, 5);
   f->SetLineWidth(1.0);
   TCanvas *c = new TCanvas("c", "c", 500, 500);
   f->Draw("cont4");
   gPad->Update();
   
   Double_t bm = gPad->GetBottomMargin();
   Double_t lm = gPad->GetLeftMargin();
   Double_t rm = gPad->GetRightMargin();
   Double_t tm = gPad->GetTopMargin();
   Double_t x1 = f->GetXaxis()->GetXmin();
   Double_t y1 = f->GetYaxis()->GetXmin();
   Double_t x2 = f->GetXaxis()->GetXmax();
   Double_t y2 = f->GetYaxis()->GetXmax();
   
   TPad *null=new TPad("null", "null", 0, 0, 1, 1);
   null->SetFillStyle(0); /* a transparent pad */
   null->SetFrameFillStyle(0);
   null->Draw();
   null->cd();
   
#if 0 /* 0 or 1 */
   f->Draw("cont3");
   gPad->Update();
#else /* 0 or 1 */
   null->Range(x1-(x2-x1)*(lm/(1-rm-lm)),
               y1-(y2-y1)*(bm/(1-tm-bm)),
               x2+(x2-x1)*(rm/(1-rm-lm)),
               y2+(y2-y1)*(tm/(1-tm-bm)));
   gPad->Update();
   f->DrawClone("cont3 same");
#endif /* 0 or 1 */
   
   // "vertical" shaded area
   
   Double_t xl = 0.2; // "left" line
   Double_t xm = 0.4; // "middle" line
   Double_t xr = 0.6; // "right" line
   
   TBox *bv = new TBox(xl, y1, xr, y2);
   bv->SetFillColor(1); bv->SetFillStyle(3005);
   bv->Draw();
   
   TLine *lv1 = new TLine(xl, y1, xl, y2); lv1->Draw();
   TLine *lv2 = new TLine(xm, y1, xm, y2); lv2->Draw();
   TLine *lv3 = new TLine(xr, y1, xr, y2); lv3->Draw();
   
   // "horizontal" shaded area
   
   Double_t yb = -0.6; // "bottom" line
   Double_t ym = -0.4; // "middle" line
   Double_t yt = -0.2; // "top" line
   
   TBox *bh = new TBox(x1, yb, x2, yt);
   bh->SetFillColor(1); bh->SetFillStyle(3004);
   bh->Draw();
   
   TLine *lh1 = new TLine(x1, yb, x2, yb); lh1->Draw();
   TLine *lh2 = new TLine(x1, ym, x2, ym); lh2->Draw();
   TLine *lh3 = new TLine(x1, yt, x2, yt); lh3->Draw();
   
   // the "middle" marker
   
   Double_t x0 = 0.0; // marker's "x" position
   Double_t y0 = 0.0; // marker's "y" position
   
   TMarker *m = new TMarker(x0, y0, 2);
   m->SetMarkerSize(2);
   m->Draw();
}
1 Like