Legend to the right of the graph

ROOT Version:
Platform: Linux
Compiler: Clang 11.0.0 x86_64 / C++


Hello,
I want to plot the legend of the graph I draw to the right of the graph.
If I use

  TGraph *g[matrix_of_cell_values.size ()];
  TMultiGraph *mg = new TMultiGraph ();
  auto mycanvas2 = new TCanvas ();
  TLegend *leg = new TLegend (0.8, 0.7, 1.2, 0.9);
  leg->SetTextSize (0.05);
  leg->SetFillColor (0);
  for (int i = 0; i < int (matrix_of_cell_values.size ()); i++)
    {
      std::cout << matrix_of_cell_values[i].size () << std::endl;
      std::cout << matrix_of_cell_values.size () << std::endl;
      g[i] = new TGraph (int (matrix_of_cell_values[i].size ()), &matrix_of_time_as_TDatime_as_doubles[i][0],
                         &matrix_of_cell_values[i][0]);
      g[i]->SetMarkerStyle (20);
      g[i]->SetMarkerColor (i + 1);
      mg->Add (g[i]);
      leg->AddEntry (g[i], (Form ("Zelle %d", cell_identifier_vector[i])));
    }
  mg->Draw ("ap");
  leg->Draw ();

  mg->SetTitle (Form ("Zelle %d", cell_identifier_vector[0]));
  mg->GetXaxis ()->SetTitle ("Zeit in UTC");
  mg->GetXaxis ()->CenterTitle ();
  mg->GetYaxis ()->SetTitle (cell_attribute.c_str ());
  mg->GetYaxis ()->CenterTitle ();
  mg->GetXaxis ()->SetTimeDisplay (1);
  mg->GetXaxis ()->SetNdivisions (505);
  mg->GetXaxis ()->SetTimeFormat ("%H:%M");
  mg->GetXaxis ()->SetTimeOffset (0, "gmt");
  mycanvas2->SetGrid ();
  mycanvas2->Print ("multi_graph.pdf");

It will draw the legend to the right, but cut it roughly in the middle.
I want the legend to extend beyond the actual canvas or the canvas to be extended, so my whole legend fits to the right.

Is there a way to do that?

I cannot run your script. Can you post the picture you get ?

The coordinates only go from 0 to 1, so you need to shift the x; try something like (0.6, 0.7, 0.99, 0.9) and adjust from there.
To make the legend go “outside” the graph (but still within the canvas), you can increase the right-side margin (space between the canvas edge and the graph frame):

mycanvas2->SetRightMargin(0.2);  // experiment with this value

(and similarly for Left/Top/Bottom margins).

1 Like

Thank you.
I have another question one might be able to answer here. Else, I make a new topic.

g[i]->SetMarkerColor (i + 1);
The colors used with this method are kinda ugly.
Is there a way to iterate through another nicer color table like a rainbow?
Apparently, I can not upload an image as a new account.

May be this:
https://root.cern/doc/master/classTColor.html#C02
or:
https://root.cern/doc/master/classTHistPainter.html#HP061

1 Like

I found a solution using “PMC PLC” at the draw command. Hence the i+1 color part is not needed and the correct colors are used.
https://root.cern.ch/doc/master/classTGraphPainter.html#GP05
I only list the relevant changes here:

  gStyle->SetPalette (kRainBow);
   
// g[i]->SetMarkerColor (i + 1);
 mg->Draw ("ap PLC PMC") ; // 

Yes this is what the 2nd link I sent you suggest.