Using gStyle to configure different sized plots

I’m trying to write a utility which will allow me to quickly switch styles as I generate multiple plots via multiple TCanvas objects generated in my root macro. the problem is that the font size is not correct when I try and put the plot in .png form into a word document. What I want to do is change the size of the font to suit the size of the graph .png file so when I insert it into a word document the axis labels and titles are legible. My problem is that if I change gstyle settings between drawing on canvases, the latest gstyle carries over to the previous TCanvas object. This is very poorly defined.

Here’s my code.

// Size comes in integer units of 1, 2, 3
// where 1 is the size for a full width plot
//       2 is the size for two size by side plots
//       3 is the size for three side by side plots
void SetStyleSize(int Size)
{
   int SizeIdx;
   double FontSizeLUT[3] = {0.02,0.07,0.1};
   double LeftMarginLUT[3] =   {0.1,0.175,0.25};
   double BottomMarginLUT[3] = {0.1,0.15,0.225};
   double TopMarginLUT[3] =    {0.1,0.1,0.125};
   double RightMarginLUT[3] =  {0.1,0.1,0.1};
   double TitleYOffsetLUT[3] = {1.,1.,1.};
   int NDivisionsLUT[3] = {510,510,505};


   if ((Size<1)||(Size>3)) {
      cout << "Size " << Size << " not implemented, setting size to 1";
      Size=1;
   }

   SizeIdx = Size-1;
   gStyle->SetTextSize(FontSizeLUT[SizeIdx]);
   gStyle->SetTitleSize(FontSizeLUT[SizeIdx],"xyz");
   gStyle->SetTitleSize(FontSizeLUT[SizeIdx],"a");
   gStyle->SetLabelSize(FontSizeLUT[SizeIdx],"xyz");
   gStyle->SetLegendTextSize(FontSizeLUT[SizeIdx]);
   
   gStyle->SetTitleOffset(TitleYOffsetLUT[SizeIdx],"y");

   gStyle->SetNdivisions(NDivisionsLUT[SizeIdx],"xyz");

   gStyle->SetPadLeftMargin(LeftMarginLUT[SizeIdx]);
   gStyle->SetPadRightMargin(RightMarginLUT[SizeIdx]);
   gStyle->SetPadBottomMargin(BottomMarginLUT[SizeIdx]);
   gStyle->SetPadTopMargin(TopMarginLUT[SizeIdx]);

}

So the idea is as follows, I call my SetStyleSize(1) function and then I create a TCanvas object and draw my graph, the fonts and styles are just what I wanted. If I then call SetStyleSize(2), then create a new TCanvas, what I draw in the new TCanvas will have the proper font size as I’ve defined using SetStyleSize(2). the problem is that the first graph in the first TCanvas object now has the new size. I want to preserve the font sizes and styles in the first TCanvas object.

Does this make any sense? I hope so. Anyway, any help in this is greatly appreciated.

Yes this is expected because the style defined by gStyle is the current style defined for all the open canvases. So the new settings you apply when you call you function with parameter “2” will also apply to the 1st canvas when it will be redrawn or print. If you want to have several canvases with different settings you should address the attributes specifically for each canvas.

Is there a TStyle TCanvas::getStyle() type function which I can use to then modify the traits for the specific canvas? Any ideas of how best to set these attributes? Or can I just do the follwoing

pCanvas->SetTextSize(10);

for example.

Thanks. Steve.

P.S. I’m being very lazy and not looking up all the class Independence…

As I said the Style is something unique for alla canvases . If you want to change the Axis label size of the histogram title size of a specific histogram in one canvas you will need to act on the individual histogram plot itself. If you are laze you can plot an histogram, doff the size you want, save the results in a .C macro and you will see which attributes need to be changed. Note that is you put your settings in a TExec you may use gStyle. A bit like in the two palette example given here:
https://root.cern/doc/master/classTColor.html#C05

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