# Set title in plot having only canvas

**URL:** <https://root-forum.cern.ch/t/set-title-in-plot-having-only-canvas/49778>\
**Category:** Garfield++\
**Created:** [April 28, 2022, 2:02am UTC](https://root-forum.cern.ch/t/set-title-in-plot-having-only-canvas/49778 "2022-04-28T02:02:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Las\_D\_Nestor](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/las_d_nestor/32/28320_2.png) [@Las\_D\_Nestor](https://root-forum.cern.ch/u/Las_D_Nestor)\
**Post date:** [April 28, 2022, 2:02am UTC](https://root-forum.cern.ch/t/set-title-in-plot-having-only-canvas/49778/1 "2022-04-28T02:02:50Z")

</div>

I am reproducing the example plot of the voltage from the examples of garfield++ pf ´RPCbyneBEM.C´, but I am only retrieving the canvas from the variable and seting the title of the canvas, But how do I save the image of the plot and adding the title ? Do I need to reconstruct the plot in a ´TGraph´ class variable

Here is the code I am using to save the image in ´png´ format. I am only able to retrieve the canvas beacuse ´GetCanvas()´ is defined in ´ViewBase.hh´.

```auto
ViewField fieldViewPot; 
fieldViewPot.SetComponent(&nebem);
fieldViewPot.SetArea(-10, -10, -1, 10, 10, 2); 
fieldViewPot.SetPlane(0, 0, 1, 0, 0, z);
fieldViewPot.PlotContour("v"); 
fieldViewPot.GetCanvas()->SetTitle("Bakelite");
fieldViewPot.GetCanvas()->SaveAs("plots.pdf");

```

---

<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:** [April 28, 2022, 6:44am UTC](https://root-forum.cern.ch/t/set-title-in-plot-having-only-canvas/49778/2 "2022-04-28T06:44:58Z")

</div>

@hschindl should know how to retrieve the TGraph.

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [April 29, 2022, 3:38pm UTC](https://root-forum.cern.ch/t/set-title-in-plot-having-only-canvas/49778/3 "2022-04-29T15:38:55Z")

</div>

Hi,  
apologies for my late reply. Setting the canvas title (text in the title bar of the window) and saving the canvas to file should indeed work the way you did it in this snippet of code.

If you want to set and show the title of the `TF2` function (which is the object used for drawing the contour plot) you could do something like this:

```auto
gStyle->SetOptTitle(1);
auto f2 = (TF2*)gROOT->FindObject("f2D_0");
if (!f2) {
  std::cerr << "Didn't find f2D_0\n";
  return 0;
}
f2->SetTitle("Bakelite");

```

If you want to save the plot to a ROOT file (not sure if this is what you had in mind), something like this should work:

```auto
auto f2 = (TF2*)gROOT->FindObject("f2D_0");
if (!f2) {
  std::cerr << "Didn't find f2D_0\n";
  return 0;
}
auto h2 = f2->GetHistogram();
if (!h2) {
  std::cerr << "Histogram does not exist.\n";
  return 0;
}
TFile outfile("rpc.root", "RECREATE");
h2->Write();
outfile.Close();

```

---

<div class="post-metadata">

**Author:** ![Las\_D\_Nestor](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/las_d_nestor/32/28320_2.png) [@Las\_D\_Nestor](https://root-forum.cern.ch/u/Las_D_Nestor)\
**Post date:** [May 3, 2022, 4:24am UTC](https://root-forum.cern.ch/t/set-title-in-plot-having-only-canvas/49778/4 "2022-05-03T04:24:35Z")

</div>

Thanks, it worked. How it is supposed to work with 3D, with TF3 ? in case of plotting the 3D Geometry. How do come to finding “f2D\_0” object ?

---

<div class="post-metadata">

**Author:** ![hschindl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/h/977dab/32.png) [@hschindl](https://root-forum.cern.ch/u/hschindl)\
**Post date:** [May 3, 2022, 5:32am UTC](https://root-forum.cern.ch/t/set-title-in-plot-having-only-canvas/49778/5 "2022-05-03T05:32:36Z")

</div>

To save the 3D geometry to a file, you can do something like this

```auto
ViewGeometry geomView3d;
geomView3d.SetGeometry(&geo);
geomView3d.Plot();
gGeoManager->Export("rpc.gdml");

```

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 17, 2022, 5:33am UTC](https://root-forum.cern.ch/t/set-title-in-plot-having-only-canvas/49778/6 "2022-05-17T05:33:27Z")

</div>

Closed on May 17, 2022, 5:33am UTC

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