# Plotting multiple 1D profile histograms (TProfile) on one canvas

**URL:** <https://root-forum.cern.ch/t/plotting-multiple-1d-profile-histograms-tprofile-on-one-canvas/38859>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [April 13, 2020, 9:25pm UTC](https://root-forum.cern.ch/t/plotting-multiple-1d-profile-histograms-tprofile-on-one-canvas/38859 "2020-04-13T21:25:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joanna-Gao](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/joanna-gao/32/21539_2.png) [@Joanna-Gao](https://root-forum.cern.ch/u/Joanna-Gao)\
**Post date:** [April 13, 2020, 9:25pm UTC](https://root-forum.cern.ch/t/plotting-multiple-1d-profile-histograms-tprofile-on-one-canvas/38859/1 "2020-04-13T21:25:31Z")

</div>

Hi,

I have three separate 1D profile plots that I want to plot on one canvas, I defined three separate TProfile objects all have empty titles with the same number of bins and the same ranges for x and for y. I also put “SAME” as arguments in the Draw() function for the second and third profile plot. But for some reason, the plot result only shows the first profile plot. The other two plots are definitely being filled as I’m able to extract bin and error bar information from them.

Could someone please tell me what I’m missing here? I’ve attached my code at the end (but not the root data files that I’m reading from)

Many thanks,  
Joanna

\_\_  
\_ROOT Version: 6.20.00  
\_Platform: MacOS

* * *

```auto
#include <string> 

{
  int fileNumber;
  cout << "How many distances do you want to look at? (Max no. 5)" << endl;
  cin >> fileNumber;

  int energyNumber;
  cout << "How many source energy do you want to look at? (Max no. 3)" << endl;
  cin >> energyNumber;

  int distanceList [] = {0, 1, 10, 50, 100};
  int energyList [] = {100, 500, 1};

  // Create a profile plot                                           
  TCanvas *can = new TCanvas("can","Number of Neutrons");

  TProfile * hprof1 = new TProfile("hprof1","",102,-1, 101,0,3000);
  TProfile * hprof2 = new TProfile("hprof2","",102,-1, 101,0,3000); 
  TProfile * hprof3 = new TProfile("hprof3","",102,-1, 101,0,3000);
  hprof1->GetXaxis()->SetTitle("Source Distance from the Water (m)");
  hprof1->GetYaxis()->SetTitle("Number of Neutrons");
  hprof1->SetTitle("No. of Neutrons Deposited Energy in Water");
  
  //hprof1->SetMarkerStyle(1);
  //hprof1->SetMarkerColor(kBlack);
  //hprof2->SetMarkerStyle(2);
  //hprof2->SetMarkerColor(kPink); 
  //hprof3->SetMarkerStyle(3);
  //hprof3->SetMarkerColor(kBlue);

  for (int x=0; x<fileNumber; ++x)
  {
    for (int y=0; y<energyNumber; ++y)
    {
      double distance = (double)distanceList[x];
      double sourceEnergy = (double)energyList[y];

      string fname;
      if (y == 0 || y == 1)
        fname=
          "~/Documents/PhD/Geant4_Projects/MuonAnalysis-build/1000Events"+
std::to_string((int)sourceEnergy)+"GeVmu"+std::to_string((int)distance)+"m.root";
      else
        fname=
          "~/Documents/PhD/Geant4_Projects/MuonAnalysis-build/1000Events"+ 
std::to_string((int)sourceEnergy)+"TeVmu"+std::to_string((int)distance)+"m.root";

      auto file = TFile::Open(fname.c_str());
                                            
      cout << "Plotting " << fname << endl;

      // Get Ntuples from file                      
      TNtuple *InfoNtuple = (TNtuple*)file->Get("ParticleInfo"); 
      std::vector<int> * particleID = 0;

      InfoNtuple->SetBranchAddress("ParticleID", &particleID);

      // Loop through events to count up the number of neutrons
      // and add to the profile plot
      int nEntries = (int)InfoNtuple->GetEntries();
      double counter = 0.;
      double meanCount = 0.;

      for (int i=0; i<nEntries; ++i)
      {
        InfoNtuple->GetEntry(i);
        for (int j=0; j<particleID->size(); ++j)      
        { 
          if ((*particleID)[j] == 2112) ++counter;
        }
        if (y == 0) hprof1->Fill(distance, counter);
        else if (y == 1) hprof2->Fill(distance, counter);
        else if (y == 2) hprof2->Fill(distance, counter); 
        
        meanCount += counter;

        counter = 0;
      }
      meanCount /= 1000;
      cout << "For energy "
           << energyList[y]
           << ", the mean no of neutron is " 
           << meanCount 
           << endl;
    }

  }

  hprof1->Draw();
  hprof2->Draw("SAME"); 
  hprof3->Draw("SAME"); 
 
}

```

---

<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 14, 2020, 9:31am UTC](https://root-forum.cern.ch/t/plotting-multiple-1d-profile-histograms-tprofile-on-one-canvas/38859/2 "2020-04-14T09:31:25Z")

</div>

It should be fine.

Can you try to plot them separately to see if the ranges really match:

```auto
   auto C = ne TCanvas("C","C");
   C->Divide(1,3);
   C->cd(1); hprof1->Draw();
   C->cd(2); hprof2->Draw(); 
   C->cd(3); hprof3->Draw(); 

```

---

<div class="post-metadata">

**Author:** ![Joanna-Gao](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/joanna-gao/32/21539_2.png) [@Joanna-Gao](https://root-forum.cern.ch/u/Joanna-Gao)\
**Post date:** [April 14, 2020, 2:45pm UTC](https://root-forum.cern.ch/t/plotting-multiple-1d-profile-histograms-tprofile-on-one-canvas/38859/3 "2020-04-14T14:45:16Z")

</div>

Thanks for the reply. I tried what you’ve suggested, the range in x-axis is definitely the same, the range in z-axis (the vertical axis) is different because each profile plot has different values.

I spoke to someone who used profile plots before and apparently there’s no way to plot multiple of them in one canvas because they are still histograms. The way I’m using to work around it is to fill the profile histos then extract the information using GetBinContent() and GetBinError() and plot using python… It’s not the most elegant solution but it works.

---

<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 14, 2020, 2:57pm UTC](https://root-forum.cern.ch/t/plotting-multiple-1d-profile-histograms-tprofile-on-one-canvas/38859/4 "2020-04-14T14:57:04Z")

</div>

I do not see why the option SAME does not work in your case. Can you post a small reproducer macro ?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [April 14, 2020, 3:44pm UTC](https://root-forum.cern.ch/t/plotting-multiple-1d-profile-histograms-tprofile-on-one-canvas/38859/5 "2020-04-14T15:44:46Z")

</div>

Try:

```cpp
THStack *hs =
  new THStack("hs",
              "No. of Neutrons Deposited Energy in Water;Source Distance from the Water (m);Number of Neutrons");
hs->Add(hprof1);
hs->Add(hprof2);
hs->Add(hprof3);
hs->Draw("NOSTACK");

```
