# Legend to the right of the graph

**URL:** <https://root-forum.cern.ch/t/legend-to-the-right-of-the-graph/48554>\
**Category:** Newbie\
**Tags:** root\
**Created:** [January 25, 2022, 3:15pm UTC](https://root-forum.cern.ch/t/legend-to-the-right-of-the-graph/48554 "2022-01-25T15:15:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![michiD](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@michiD](https://root-forum.cern.ch/u/michiD)\
**Post date:** [January 25, 2022, 3:15pm UTC](https://root-forum.cern.ch/t/legend-to-the-right-of-the-graph/48554/1 "2022-01-25T15:15:12Z")

</div>

_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

```auto
  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?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 25, 2022, 3:31pm UTC](https://root-forum.cern.ch/t/legend-to-the-right-of-the-graph/48554/2 "2022-01-25T15:31:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 25, 2022, 4:03pm UTC](https://root-forum.cern.ch/t/legend-to-the-right-of-the-graph/48554/3 "2022-01-25T16:03:31Z")

</div>

> [@michiD](#):
>
> `TLegend *leg = new TLegend (0.8, 0.7, 1.2, 0.9);`

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):

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

```

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

---

<div class="post-metadata">

**Author:** ![michiD](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@michiD](https://root-forum.cern.ch/u/michiD)\
**Post date:** [January 26, 2022, 10:59am UTC](https://root-forum.cern.ch/t/legend-to-the-right-of-the-graph/48554/4 "2022-01-26T10:59:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 26, 2022, 11:07am UTC](https://root-forum.cern.ch/t/legend-to-the-right-of-the-graph/48554/5 "2022-01-26T11:07:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michiD](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/a9a28c/32.png) [@michiD](https://root-forum.cern.ch/u/michiD)\
**Post date:** [January 26, 2022, 1:45pm UTC](https://root-forum.cern.ch/t/legend-to-the-right-of-the-graph/48554/6 "2022-01-26T13:45:05Z")

</div>

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](https://root.cern.ch/doc/master/classTGraphPainter.html#GP05)  
I only list the relevant changes here:

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

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 26, 2022, 3:51pm UTC](https://root-forum.cern.ch/t/legend-to-the-right-of-the-graph/48554/7 "2022-01-26T15:51:15Z")

</div>

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