# gStyle.SetPalette issue with numpy

**URL:** <https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738>\
**Category:** Newbie\
**Tags:** python\
**Created:** [April 25, 2022, 1:14pm UTC](https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738 "2022-04-25T13:14:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![enbyphysicist](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/enbyphysicist/32/29442_2.png) [@enbyphysicist](https://root-forum.cern.ch/u/enbyphysicist)\
**Post date:** [April 25, 2022, 1:14pm UTC](https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738/1 "2022-04-25T13:14:45Z")

</div>

* * *

\_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;

```auto
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;

```auto
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)

FI=ROOT.TColor.CreateGradientColorTable(Number,r,g,b,stop,Steps)

print('The FI =', FI)

for i in range(Steps):
 lsflag.append(FI+i)

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

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

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 25, 2022, 1:18pm UTC](https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738/2 "2022-04-25T13:18:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 25, 2022, 1:38pm UTC](https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738/3 "2022-04-25T13:38:14Z")

</div>

Hello,

`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.

---

<div class="post-metadata">

**Author:** ![enbyphysicist](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/enbyphysicist/32/29442_2.png) [@enbyphysicist](https://root-forum.cern.ch/u/enbyphysicist)\
**Post date:** [April 25, 2022, 1:52pm UTC](https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738/4 "2022-04-25T13:52:11Z")

</div>

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.

```auto
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;

```auto
lsflag= np.array([])
#==== code from before ====
for i in range(Steps):
 #print(i)
 np.append(lsflag,FI+i)

```

But I got an absolute mess of an error then,

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

```

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 25, 2022, 1:54pm UTC](https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738/5 "2022-04-25T13:54:56Z")

</div>

Hello,

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?

---

<div class="post-metadata">

**Author:** ![enbyphysicist](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/enbyphysicist/32/29442_2.png) [@enbyphysicist](https://root-forum.cern.ch/u/enbyphysicist)\
**Post date:** [April 25, 2022, 4:47pm UTC](https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738/6 "2022-04-25T16:47:33Z")

</div>

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;

```auto
hresMatrix_graph = ROOT.TCanvas('hresMatrix_graph', 'c1', 500, 500)
hresMatrix_graph.cd()
ROOT.gStyle.SetOptStat(0)

ROOT.gStyle.SetPalette(Steps, lsflag)

hresMatrix.Draw("colz")

hresMatrix_graph.Update()
hresMatrix_graph.Write()

```

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

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [April 25, 2022, 7:06pm UTC](https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738/7 "2022-04-25T19:06:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [April 26, 2022, 6:03am UTC](https://root-forum.cern.ch/t/gstyle-setpalette-issue-with-numpy/49738/8 "2022-04-26T06:03:01Z")

</div>

> [@enbyphysicist](#):
>
> 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;

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.
