Change background color only in a region of a TH1

Hi!

If you run this:

vector<Double_t> x = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<Double_t> y = {1, 2, 3, 4, 3, 6, 7, 0, 0, 4};

void test () {

	TCanvas * c = new TCanvas("name", "window", 800, 600);
	c->SetGrid();
	gStyle->SetOptStat(false);
	
	TH1D * h = new TH1D("name", ";angle [#circ];probability [#]", y.size(), 0, y.size());	
	for (Int_t i = 0; i < y.size(); i++)	h->SetBinContent(i + 1, y[i]);
	
	h->SetFillColor(2);
	h->Draw("HIST BAR");
	
	x.clear();
	y.clear();
}

you get this graph:

but I have the need to highlight an area of the graph, so that i looks like this:

How to do that in root?

Thank you!

One of the possible ways is to add in the end:

	gPad->Modified(); gPad->Update();
	TBox *b =
	  new TBox(7., gPad->GetUymin(),
		   9., (999. * gPad->GetUymax() + gPad->GetUymin()) / 1000.);
	b->SetFillColor(7);
	b->Draw();
	gPad->RedrawAxis(); gPad->RedrawAxis("G");
	gPad->Modified(); gPad->Update();

Of course, maybe @couet knows something more elegant.

2 Likes

Uhm.

I see it works. Quite simple and effective, so that’s ok! :smiley:
Thank you! :slight_smile:

No, it looks fine to me.

@couet It seems to me that there is no way to redraw the “border” lines (that’s why I couldn’t simply use “gPad->GetUymax()” and I had to lower the top edge of the box). Maybe you could add this feature.

Something like that ?

vector<Double_t> x = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
vector<Double_t> y = {1, 2, 3, 4, 3, 6, 7, 0, 0, 4};

void RedrawFrame(){

   auto b = new TBox(gPad->GetUxmin(), gPad->GetUymin(),
                     gPad->GetUxmax(), gPad->GetUymax());
   b->SetFillStyle(0);
   b->SetLineWidth(gPad->GetFrameLineWidth());
   b->SetLineColor(gPad->GetFrameLineColor());
   b->Draw();
}

void ziel () {

   TCanvas * c = new TCanvas("name", "window", 800, 600);
   c->SetGrid();
   gStyle->SetOptStat(false);

   TH1D * h = new TH1D("name", ";angle [#circ];probability [#]", y.size(), 0, y.size());
   for (Int_t i = 0; i < y.size(); i++)   h->SetBinContent(i + 1, y[i]);

   h->SetFillColor(2);
   h->Draw("HIST BAR");
   gPad->Modified(); gPad->Update();

   auto b = new TBox(7., gPad->GetUymin(),
                     9., gPad->GetUymax());
   b->SetFillColor(7);
   b->Draw();
   gPad->RedrawAxis();
   gPad->RedrawAxis("G");
   RedrawFrame();
}
1 Like

So, I’ve been thinking about a new option, something like (note: sometimes “BB” or “FB” drawing options are effective so the complete borders may not need to be redrawn):
gPad->RedrawAxis("B"); // redraw "active" Borders / BoundingBox

1 Like

Yes that’s a idea to put it as a new option in RedrawAxis. I was posting it as an example to check if that is the functionality needed. If yes, that code can go in RedrawAxis…

Uhm, so you basically redraw the main pad too in that additional method RedrawFrame()?

This is a bit more polish since it isn’t affected by the canvas size.

Thanks.

This idea would be to pout that code in RedrawAxis under the option "F"rame or "B"ox

A PR is on the way:

1 Like

This new option is now in master. Thanks for the suggestion.

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