# How to define histogram with variable bin width in python

**URL:** <https://root-forum.cern.ch/t/how-to-define-histogram-with-variable-bin-width-in-python/27110>\
**Category:** ROOT\
**Created:** [November 30, 2017, 6:02am UTC](https://root-forum.cern.ch/t/how-to-define-histogram-with-variable-bin-width-in-python/27110 "2017-11-30T06:02:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![saswatinandan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/839c29/32.png) [@saswatinandan](https://root-forum.cern.ch/u/saswatinandan)\
**Post date:** [November 30, 2017, 6:02am UTC](https://root-forum.cern.ch/t/how-to-define-histogram-with-variable-bin-width-in-python/27110/1 "2017-11-30T06:02:18Z")

</div>

Hi,

I am trying to define histogram with variable bin width in python in following way:

> > > import numpy  
> > > x=numpy.array([2,4,8,9])  
> > > import ROOT as r  
> > > h=r.TH1F(‘’,‘’,3,x)  
> > > h  
> > > \<ROOT.TH1F object at 0x4f3bf30\>

but the width of each bin is zero

> > > h.GetXaxis().GetBinWidth(1)  
> > > 0.0  
> > > h.GetXaxis().GetBinWidth(2)  
> > > 0.0  
> > > h.GetNbinsX()  
> > > 3

Could anyone please tell me the correct way to define the histogram with variable bin width in python?

Thanks in advance -:))

Saswati

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 30, 2017, 7:51am UTC](https://root-forum.cern.ch/t/how-to-define-histogram-with-variable-bin-width-in-python/27110/2 "2017-11-30T07:51:11Z")

</div>

See for example:

> [@Python macro and histogram with variable bin width](https://root-forum.cern.ch/t/python-macro-and-histogram-with-variable-bin-width/8417/2):
>
> Angi, the TH1::FillN() isn’t going to work with just two arguments, but to show how to pass a list through a double\* pointer:[code]from ROOT import \* from array import array binLowE = [2,4,9,15,20,22] uniformTau = TH1D(“uniformTau”,"",5,array(‘d’,binLowE)) #uniformTau.FillN(5,10) uniformTau.Draw()[/code] I.e. just use an array from module array. The buffer interface does the rest. HTH, Wim

(searching on the forum is sometimes useful…) 😉

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [November 30, 2017, 4:55pm UTC](https://root-forum.cern.ch/t/how-to-define-histogram-with-variable-bin-width-in-python/27110/3 "2017-11-30T16:55:30Z")

</div>

In defense of TS, the code won’t work with a list per the other topic but should do with a numpy array.

There was a recent thread and bug report about incorrect array converters being selected: the selection is on element size, not element type as numpy arrays don’t carry a typecode as an attribute (but do as part of the buffer info). See:

[https://sft.its.cern.ch/jira/browse/ROOT-9040](https://sft.its.cern.ch/jira/browse/ROOT-9040)  
[pyROOT TMultiGraph Segmentation Fault](https://root-forum.cern.ch/t/pyroot-tmultigraph-segmentation-fault/26263)

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [November 30, 2017, 5:17pm UTC](https://root-forum.cern.ch/t/how-to-define-histogram-with-variable-bin-width-in-python/27110/4 "2017-11-30T17:17:29Z")

</div>

As @wlav has pointed out this is a bug about types. ROOT interprets your array as one with floats, even though it contains integers and you should have received an error. Specifying `dtype` when defining your array will resolve the issue until the bug is resolved.

```auto
import numpy as np
import ROOT

#Must explicit declare variable type
bin_edges = np.array([2, 4, 8, 9], dtype='float64')

h = ROOT.TH1F("","",len(bin_edges)-1, bin_edges)

for bin_num in range(len(bin_edges)-1):
    print h.GetBinWidth(bin_num)

```

---

<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 14, 2017, 5:17pm UTC](https://root-forum.cern.ch/t/how-to-define-histogram-with-variable-bin-width-in-python/27110/5 "2017-12-14T17:17:30Z")

</div>

Closed on December 14, 2017, 5:17pm UTC

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