gStyle.SetPalette issue with numpy

_ROOT Version: 6.13/02
Platform: Not Provided
Compiler: Not Provided

Hi there! I’ve been messing around with making some custom palettes in root. I’ve gotten it to a point where ROOT.TColor.CreateGradientColorTable works fine, but I’ve been getting the following error;

TypeError: void TStyle::SetPalette(int ncolors = kBird, int* colors = 0, float alpha = 1.) =>
    could not convert argument 2 

Below the relevant portion of my code;

Number = 5;
lsflag= []

r = np.array([0.8, 1.0, 1.0, 0.8, 0.64], float)
g =np.array([ 0.15, 0.6, 1.0, 0.4, 0.0], float)
b=np.array([ .0,   .3, 1.0, 0.65, 0.38], float)
stop = np.array([ 0, 0.2, 0.4, 0.6, 1], float)


print('The FI =', FI)

for i in range(Steps):

#Making the histograms
hresMatrix = MatrixToHist(resMatrix,"hresMatrix", 0.8, 0.2)

hresMatrix_graph = ROOT.TCanvas('hresMatrix_graph', 'c1', 500, 500)
ROOT.gStyle.SetPalette(Steps, lsflag);

It looks like a python problem.
@etejedor should know.


SetPalette expects an array of integers as second argument, but what you are passing is a Python list (and that conversion does not seem to be allowed). You can try with a numpy array of integers instead.

Thank you for the advice, I tired calling it as a numpy array in the set palette like like this ROOT.gStyle.SetPalette(Steps, np.array(lsflag));
With that I got the following error.

ROOT.gStyle.SetPalette(Steps, np.array(lsflag));
AttributeError: void TStyle::SetPalette(int ncolors = kBird, int* colors = 0, float alpha = 1.) =>
    could not convert argument 2 ('numpy.ndarray' object has no attribute 'typecode' and given element size (8) do not match needed (4))

I tried going back and restructuring so that lsflag is an array throughout the code by changing the lines;

lsflag= np.array([])
#==== code from before ====
for i in range(Steps):

But I got an absolute mess of an error then,

*** Break *** floating point exception
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[<unknown binary>] (no debug info)
[/Applications/root/lib/] PyROOT::TMethodHolder::Call(PyROOT::ObjectProxy*&, _object*, _object*, PyROOT::TCallContext*) (no debug info)
[/Applications/root/lib/] PyROOT::(anonymous namespace)::mp_call(PyROOT::MethodProxy*, _object*, _object*) (no debug info)


Can you explicitly specify the integer data type when creating the numpy array? I think the default is float.

If that doesn’t work, I see that you are using a old version of (Py)ROOT, would it be an option to move to a newer version?

I’m hesitant to move to a newer version of pyroot bc just getting pyroot to work in it’s current version took a couple of days worth of troubleshooting that I’d prefer not to repeat. Trying what you said, I defined lsflag to be an integer type array, and that worked to eliminate the errors when running the py program!
But, when looking at the actual histogram that’s graphed, it’s still the same default color palette. This is the last portion of my code where I make the canvas, attempt to change the color palette, and update the root file which the graph is stored in;

hresMatrix_graph = ROOT.TCanvas('hresMatrix_graph', 'c1', 500, 500)

ROOT.gStyle.SetPalette(Steps, lsflag)



Thank you so much for all the help btw, would not have gotten this far with out you!

No problem, I’m happy that we’re making progress!

This now seems something related to graphics and not Python itself, so I’ll ask @couet to give a hand.

Let me try to understand: You have an histogram stored in a file and you what to change its palette when plotting it back ? it should not be a problem, the palette is not stored in the file. When you plot back the histogram it uses the current palette set. I just tried this in C++, it works. I hope I understood what you are doing.