# Plotting multiple histograms in same canvas

**URL:** <https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877>\
**Category:** Newbie\
**Created:** [October 7, 2022, 9:56am UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877 "2022-10-07T09:56:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![roriana9](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@roriana9](https://root-forum.cern.ch/u/roriana9)\
**Post date:** [October 7, 2022, 9:56am UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877/1 "2022-10-07T09:56:56Z")

</div>

I have two root files and want to plot the histograms from each one together in the same canvas;  
I used this code, but what I get is only the last plot and it is ignore the first plot completely, it didn’t give me any error messages.

```
TFile *f1 = TFile::Open("160primary.root");
TH1D *h1= (TH1D*)f1->Get("Position_X__cm_");
h1->Draw("hist l");
h1->SetLineWidth(3);
h1->SetLineColor(2);

TFile *f2 = TFile::Open("130primary.root");
TH1D *h2 = (TH1D*)f2->Get("Position_X__cm_");
h2->SetLineWidth(2);
h2->SetLineColor(4);
h2->Draw("hist l same");

```

any help will be deeply appreciated.

---

<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:** [October 7, 2022, 10:01am UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877/2 "2022-10-07T10:01:37Z")

</div>

Welcome to the ROOT forum

Weird, that code should produce the two plots on the same canvas as you plot the 2nd one with the option “same”. The range along X and Y axis is defined by the first plot. Can you provide a code we can run ?

---

<div class="post-metadata">

**Author:** ![roriana9](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@roriana9](https://root-forum.cern.ch/u/roriana9)\
**Post date:** [October 7, 2022, 10:12am UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877/3 "2022-10-07T10:12:24Z")

</div>

thanks for your help  
I am trying to upload the file but it refused.

---

<div class="post-metadata">

**Author:** ![roriana9](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@roriana9](https://root-forum.cern.ch/u/roriana9)\
**Post date:** [October 7, 2022, 10:13am UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877/4 "2022-10-07T10:13:27Z")

</div>

I got a message saying new users can not send links can you advise me how to upload it ?

---

<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:** [October 7, 2022, 10:18am UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877/5 "2022-10-07T10:18:21Z")

</div>

An other way is to use THStack.

```auto
TFile *f1 = TFile::Open("160primary.root");
TH1D *h1= (TH1D*)f1->Get("Position_X__cm_");
h1->SetLineWidth(3);
h1->SetLineColor(2);

TFile *f2 = TFile::Open("130primary.root");
TH1D *h2 = (TH1D*)f2->Get("Position_X__cm_");
h2->SetLineWidth(2);
h2->SetLineColor(4);

auto hs = new THStack();
hs->Add(h1,"HIST L");
hs->Add(h2,"HIST L");
hs->Draw("NOSTACK");

```

---

<div class="post-metadata">

**Author:** ![roriana9](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@roriana9](https://root-forum.cern.ch/u/roriana9)\
**Post date:** [October 7, 2022, 10:26am UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877/6 "2022-10-07T10:26:59Z")

</div>

thanks a lot  
but it give me this message  
\*\*\* Break \*\*\* segmentation violation

[/usr/lib/system/libsystem\_platform.dylib] \_sigtramp (no debug info)

[] (no debug info)

[] (no debug info)

[/opt/local/libexec/root6/lib/root/libCling.6.24.06.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value\*) const (no debug info)

[/opt/local/libexec/root6/lib/root/libCling.6.24.06.so] cling::Interpreter::RunFunction(clang::FunctionDecl const\*, cling::Value\*) (no debug info)

[/opt/l…

---

<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:** [October 7, 2022, 10:28am UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877/7 "2022-10-07T10:28:50Z")

</div>

Which line gives this error ?

---

<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:** [October 7, 2022, 11:28am UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877/9 "2022-10-07T11:28:57Z")

</div>

That’s totally un-clear… “TCanvas instead of TH1D” ?  
have a look at the documentation…

---

<div class="post-metadata">

**Author:** ![roriana9](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@roriana9](https://root-forum.cern.ch/u/roriana9)\
**Post date:** [October 8, 2022, 2:28pm UTC](https://root-forum.cern.ch/t/plotting-multiple-histograms-in-same-canvas/51877/10 "2022-10-08T14:28:46Z")

</div>

thanks a lot for your help  
I managed to solve the problem
