Draw TPIE with numpy array

Hi,
I am new to ROOT so this might be an easy question. I am trying to draw a pie chart using python.
After calculating my datas, I store them in an array (named Indexes_array) and try to plot a pie. Here is the part of my code concerned :

colors_array = np.array(colors, np.int);
Indexes_array = np.array(Indexes, np.float);
cpie = ROOT.TCanvas(“cpie”,“Tpie Test”, 700, 700);
pie1 = ROOT.TPie(“pie”, “First Indexes”, nA, Indexes_array, colors_array);

The error given is :

TPie::TPie(const char*, const char*, Int_t, Double_t*, Int_t* cols = 0, const char** lbls = 0) =>
could not convert argument 5 (‘numpy.ndarray’ object has no attribute ‘typecode’ and given element size (8) do not match needed (4))
TPie::TPie(const char*, const char*, Int_t, Float_t*, Int_t* cols = 0, const char** lbls = 0) =>
could not convert argument 4 (‘numpy.ndarray’ object has no attribute ‘typecode’ and given element size (8) do not match needed (4))

I tried to change the syntax and to use a list but nothing works. Does anyone know what the problem is ?

Thanks for any help

Sylvain

I am not a python expert but I I do:

from ROOT import TCanvas, TPie, TLegend
from array import array

vals = array ( 'd', [.2,1.1,.6,.9,2.3])
colors = array ( 'd', [2,3,4,5,6])

It seems ok … I let experts confirm

Thank you couet.
I tried to use your way to declare array but I obtain a similar error :

TPie::TPie(const char*, const char*, Int_t, Double_t*, Int_t* cols = 0, const char** lbls = 0) =>
could not convert argument 5
TPie::TPie(const char*, const char*, Int_t, Float_t*, Int_t* cols = 0, const char** lbls = 0) =>
could not convert argument 4

Types need to match, so the second array should be of int type (to get an int*).

from ROOT import * from array import array vals = array ( 'd', [.2,1.1,.6,.9,2.3]) colors = array ( 'i', [2,3,4,5,6]) cpie = TCanvas("cpie","Tpie Test", 700, 700) pie1 = TPie("pie", "First Indexes", len(vals), vals, colors) pie1.Draw()

Same for numpy. You can use C float (np.float) as there is an overload for that (Olivier’s example uses double, though), but the integers need to be a 4-byte int (np.int32, not np.int).

The buffer interface in PyROOT sucks, and so do the error messages. Most of that was designed in the days of Python2.2, i.e. before the numpy buffer interface (or even numpy) existed.

It’s working !
My pies are drawn, Thank you wlav !

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