Image as TFrame background

Hi!

I need to set a picture as the background of a graph/hist.
I found this thread TCanvas background image which explains how to add a background to the entire canvas. I only need to set the background of the TFrame only.

Is there a way to make it?
Thank you!

The example https://root.cern/doc/master/img2pad_8C.html shows how to put an image in a Pad. I guess that what you are looking for.

Here is some code to draw the histogram on top of the image. The trick is to add another pad on top of the one used to draw the image. Based on couet’s example:

void imgback()
{
   TImage *img = TImage::Open("$ROOTSYS/tutorials/image/rose512.jpg");
   if (!img) {
      printf("Could not create an image... exit\n");
      return;
   }
   img->SetConstRatio(kFALSE);
   float d = 0.40;
   TCanvas *c1 = new TCanvas("roses", "roses", 500, 500);
   TPad *p1 = new TPad("i1", "i1", 0.05, 0.55, 0.05+d*img->GetWidth()/img->GetHeight(), 0.95);
   TPad *p2 = new TPad("i2", "i2", 0.05, 0.55, 0.05+d*img->GetWidth()/img->GetHeight(), 0.95);
   p2->SetFillColor(0);
   p2->SetFillStyle(0);
   p2->SetFrameFillColor(0);
   p2->SetFrameFillStyle(0);
   p1->Draw();
   p1->cd();
   img->Draw();
   c1->cd();
   p2->Draw();
   p2->cd();
   TH1F *h = new TH1F("h","test",100,-3,3);
   //h->SetFillColor(kCyan);
   h->FillRandom("gaus",5000);
   h->Draw();
   c1->cd();
}

Note that p2 is basically a copy of p1 but made transparent. You draw p1, then the image on p1, then p2 and finally the histogram on p2.
One thing I found is that when saving the canvas to PDF the rose image is not kept, I just get the histogram on a white background (Xubuntu, root 6.14.06). GIF doesn’t work in this case, I guess because of the number of colours; but EPS, JPG and PNG work fine.
roses roses.pdf (14.1 KB)

1 Like

This is awesome but still a bit far from what I need.
A rose is nice, but I need at the end is a black&white picture showing a particular geometry of a model (I get this from another software) in which I superimpose a TH2D drawn using “colz” option.
I have two questions then:

  • how to draw the canvas (and thus the frame/pad) so that the image keeps a constant ratio and the image borders coincide with the tpad “p2”?
  • will the colors of a TH2D drawn using the “colz” option cover the background or is there a way to paint only the white area of the image?

Thank you!

Make sure the pad aspect ration is the same as the image you will put into it.

You can use a semi transparent colour palette

Well… Yeah. But how to do that?
I tried changing the x,y values in the TPad function in the above example, but I can’t set the canvas width and height so that the two tpads coincide.

Can you provide a small example showing where you are now ? (with your specific case)

Sure!

void imgback()
{
   TImage *img = TImage::Open("$ROOTSYS/tutorials/image/rose512.jpg");
   img->SetConstRatio(1);
   float d = 1;
   gStyle->SetOptStat(0);
   TCanvas *c1 = new TCanvas("roses", "roses", img->GetWidth(), img->GetHeight());
   cout << 1*(img->GetHeight())/(img->GetWidth()) << endl;
   TPad *p1 = new TPad("i1", "i1", 0.0, 0.0, 0.0+d*img->GetWidth()/img->GetHeight(), 1);
   TPad *p2 = new TPad("i2", "i2", 0.0, 0.0, 0.0+d*img->GetWidth()/img->GetHeight(), 1);
   p2->SetFillColor(0);
   p2->SetFillStyle(0);
   p2->SetFrameFillColor(0);
   p2->SetFrameFillStyle(0);
   p1->Draw();
   p1->cd();
   img->Draw();
   p2->Draw();
   p2->cd();
   TH1F *h = new TH1F("h","test",100,-3,3);
   h->FillRandom("gaus",5000);
   h->Draw("colz");
   c1->cd();
}

I haven’t done much indeed.
The fact is that the canvas size&ratio affects the pad size&ratio. It looks like that the two ratios are not the same, so if I set the canvas width and height as shown in the example, the tpads will have another ratio…

void imgbck()
{

   TImage *img = TImage::Open("$ROOTSYS/tutorials/image/rose512.jpg");

   TCanvas *c1 = new TCanvas("rose512", "example of image background", img->GetWidth(), img->GetHeight());
   TPad *p1 = new TPad("p1","p1",0.1, 0.1, .9, .9);
   p1->Draw();
   p1->cd();
   img->Draw("xxx");

   c1->cd();
   TPad *p2 = new TPad("p2","p2",0., 0., 1., 1.);
   p2->SetFillStyle(4000);
   p2->SetFrameFillColor(0);
   p2->SetFrameFillStyle(0);
   p2->Draw();
   p2->cd();

   gStyle->SetPalette(kBird,0,0.7);
   TF2 *f3 = new TF2("f3","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3);
   f3->Draw("col");
}

1 Like

That’s weird…
I copy and paste your code -> run it -> the superimposed map is not transparent, but 100% opaque, I can’t see the background image. :thinking:
Could that be the ROOT version? Mine:

gROOT->GetVersion()
(const char *) "6.14/02"

Anyway, I only slightly understand the logic behind your choice for x,y coordinates in both TPad constructors. I’d call it magic by now.

transparency is not available with X11. See: :https://root.cern/doc/master/classTColor.html#C07

The transparency is available on all platforms when the flag OpenGL.CanvasPreferGL is set to 1 in $ROOTSYS/etc/system.rootrc , or on Mac with the Cocoa backend. On the file output it is visible with PDF, PNG, Gif, JPEG, SVG … but not PostScript.

1 Like

the p1 pad coordinate are such to take into account the main in pad p2.

1 Like

Thank you! :wink:
Now it works fine.

Still a bit magic, but it doesn’t really matter. I need to play with those coordinates a bit and then I can maybe better understand that.

Thank you couet! :slight_smile:
This boosts my workflow a lot!

That’s just numbers … no magic behind. When you draw a histogram in a pad there is axis and margins around. That’s a TFrame. These margins are equal to .1 by default. that is why I set p1 as I did in order to align the image on the axis.

I’m not confident with both TPads and that “cd()” command, that’s why I need to dig (into the online manual) a little bit before I can fully understand what’s happening in this macro. :slight_smile:

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