# Numpy arrays in variable binned histogram with pyroot

**URL:** <https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070>\
**Category:** ROOT\
**Created:** [March 14, 2017, 12:30pm UTC](https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070 "2017-03-14T12:30:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![devdatta](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/f14d63/32.png) [@devdatta](https://root-forum.cern.ch/u/devdatta)\
**Post date:** [March 14, 2017, 12:30pm UTC](https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070/1 "2017-03-14T12:30:49Z")

</div>

Hello,

I am using root-6.08.02 to make a variable binned histogram with PyROOT.  
I find that this works:

```auto
from array import array
m = [400,450,500,550,600,650,700,750,800,850,900,950,1000,1150,1500]
h = ROOT.TH1D("h",";m [GeV];Events/ 50 GeV;;",len(m)-1,array('d',m))

```

but this doesn’t

```auto
import numpy as np
m = [400,450,500,550,600,650,700,750,800,850,900,950,1000,1150,1500]
h = ROOT.TH1D("h",";m [GeV];Events/ 50 GeV;;",len(m)-1,np.asarray(m))

```

I error I get is

```auto
Error in < TGaxis::PaintAxis >: length of axis is 0

```

when trying to draw the histogram booked using the numpy array.

Is this expected?

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 18, 2017, 10:46am UTC](https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070/2 "2017-03-18T10:46:56Z")

</div>

> [@devdatta](#):
>
> np.asarray(m)

doesn’t give you a double array, `np.asarray(m, 'd')` does. I.e. this works:

```auto
import ROOT
import numpy as np
m = [400,450,500,550,600,650,700,750,800,850,900,950,1000,1150,1500]
h = ROOT.TH1D("h",";m [GeV];Events/ 50 GeV;;",len(m)-1,np.asarray(m, 'd'))
h.Draw()

```

Axel.

---

<div class="post-metadata">

**Author:** ![devdatta](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/f14d63/32.png) [@devdatta](https://root-forum.cern.ch/u/devdatta)\
**Post date:** [March 19, 2017, 11:01am UTC](https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070/3 "2017-03-19T11:01:37Z")

</div>

Thanks for your reply.

I traced the problem back to an unrelated issue with TPad::DrawFrame and not to the histo I was trying to fill. In other words,  
h = ROOT.TH1D(“h”,";m [GeV];Events/ 50 GeV;;",len(m)-1,np.asarray(m))  
works without specifying ‘d’.

I had used TGraph and TGraphAsymErrors with the same kind of initialization and those worked as well.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 19, 2017, 4:41pm UTC](https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070/4 "2017-03-19T16:41:58Z")

</div>

Very surprising. I had to specify the `'d'`; without the `'d'` I got the same error message as you did.

Axel.

---

<div class="post-metadata">

**Author:** ![devdatta](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/f14d63/32.png) [@devdatta](https://root-forum.cern.ch/u/devdatta)\
**Post date:** [March 20, 2017, 9:29am UTC](https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070/5 "2017-03-20T09:29:43Z")

</div>

Not sure if this has got to do with the versions: I’m using python 2.7.10 with numpy 1.9.2  
This [https://docs.scipy.org/doc/numpy-1.9.2/reference/generated/numpy.asarray.html#numpy.asarray](https://docs.scipy.org/doc/numpy-1.9.2/reference/generated/numpy.asarray.html#numpy.asarray)  
says that the data type is optional.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [March 21, 2017, 8:52am UTC](https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070/6 "2017-03-21T08:52:15Z")

</div>

“Optional” doesn’t mean “magically does what you expect” 🙂

Axel.

---

<div class="post-metadata">

**Author:** ![devdatta](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/f14d63/32.png) [@devdatta](https://root-forum.cern.ch/u/devdatta)\
**Post date:** [March 21, 2017, 10:11am UTC](https://root-forum.cern.ch/t/numpy-arrays-in-variable-binned-histogram-with-pyroot/24070/7 "2017-03-21T10:11:40Z")

</div>

I agree. It’s good to be pedantic in this case!
