# Convert canvas into histogram in pyroot

**URL:** <https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-pyroot/42425>\
**Category:** ROOT\
**Created:** [November 24, 2020, 4:47pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-pyroot/42425 "2020-11-24T16:47:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mendoza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/mendoza/32/26128_2.png) [@mendoza](https://root-forum.cern.ch/u/mendoza)\
**Post date:** [November 24, 2020, 4:47pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-pyroot/42425/1 "2020-11-24T16:47:02Z")

</div>

Dear Users,

I use PYROOT and I have a TFile with a canvas inside. I need to convert it into an histogram because I will work with it after. I have used some of the topics very related to that but it does not work, maybe because I am using PYROOT.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [December 8, 2020, 12:00pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-pyroot/42425/2 "2020-12-08T12:00:20Z")

</div>

Hi @mendoza,  
sorry for the late reply. What have you tried and what’s the error?  
Can you share the TFile with us?

The way to extract a TH1 object (i.e. a histogram) from a canvas in a file is to first extract the canvas from the file and then extract the histogram from the canvas:

```python
f = ROOT.TFile("file.root")
c = f.Get("canvasname")
histo = c.GetPrimitive("histoname")

```

Cheers,  
Enrico

---

<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:** [December 22, 2020, 12:06pm UTC](https://root-forum.cern.ch/t/convert-canvas-into-histogram-in-pyroot/42425/3 "2020-12-22T12:06:50Z")

</div>

Closed on December 22, 2020, 12:06pm UTC

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