I have my data in an excel file and I want to plot a colored 2D histogram.
Some bins have value equal to 0 that I want to plot with color corresponding to the 0 value in the color scale (blue), not white like the empty bins.
Changing the data to something different than 0 isn’t possible. I read a couple of posts in the forum but I couldn’t fix it.
This is my code so far:
data = pd.read_excel(excel_file, skiprows=3)
print(data.columns)
x = data['bias'].to_numpy()
y = data['mod'].to_numpy()
color = data['Frames with Error(s)'].to_numpy()
hist = ROOT.TH2F("hist", "2D scatter plot with color;X-axis;Y-axis",
100, min(x)-1, max(x)+1,
100, min(y)-1, max(y)+1)
for i in range(len(x)):
print(f"x: {x[i]}, y:{y[i]}, color: {color[i]}")
hist.Fill(x[i], y[i], color[i])
hist.SetMinimum(0)
hist.SetMaximum(max(color))
c = ROOT.TCanvas("c", "Canvas", 800, 600)
c.SetRightMargin(0.15)
hist.Draw("COLZ")
c.Update()
Thank you in advance.
_ROOT Version: 6.32.02
Working with pyROOT in vs code.
Hello, thank you for your answer. hist.Draw("COLZ2") gives me two different results for the root and the png image, the png image now has blue for all of the background and the root image has white. Still the zero value points aren’t different from the background.
I forgot to upload the part that saves the plots, maybe the problem is here.
COL2 is an other, fast, drawing algorithm it is not what you are looking for. If you need empty bins to be coloured as the minimum of the palette the best it to change to background colour of the Pad to this colour.
Hello, thank you for your help. I am not sure if changing the background color would help me because I want the empty bins to be colored as the background and the bins that contain 0 to be colored as the minimum of the palette (If I understood correctly from reading, root considers both options empty).
As alternative, if you don’t want or can’t change the bins filled with zero, you could change the values of the non-filled bins to some negative value (e.g. -1) and use h->SetMinimum(-0.01) (a very small but negative value; not exactly zero); this means you have to know which ones were not filled (to change them after filling), or fill all the bins with this negative value before filling the data, so the ones not filled remain negative. Note, however, that the statistics of the histogram will change and be incorrect, since now you are actually filling all those bins. (Edit: and also, if you use Fill instead of SetBinContent when filling in the actual data, you have to Fill carefully to correct the bin contents for the negative values that you already put in the bins!).
{
TH2F *h = new TH2F("h"," ",4,0,4,4,0,4);
for (int i=1; i<5; ++i){
for (int j=1; j<5; ++j){
h->SetBinContent(i,j,-1);
}
}
h->SetBinContent(1,2,1);
h->SetBinContent(1,3,3);
h->SetBinContent(2,2,2);
h->SetBinContent(2,3,0);
h->SetMinimum(-0.01);
TCanvas *c = new TCanvas("c","");
h->Draw("colz text");
}
And it is the code to change the background color to the first color of the current palette:
{
auto h = new TH2F("h","Option COLor example ",40,-4,4,40,-20,20);
auto c = new TCanvas();
float px, py;
for (Int_t i = 0; i < 25000; i++) {
gRandom->Rannor(px,py);
h->Fill(px,5*py);
}
gStyle->SetPalette(kOcean);
h->Draw("COLZ");
c->SetFrameFillColor(TColor::GetColorPalette(0));
}
Thank you all for your answers. In the end, the simplest solution was to change the points to something different than 0 (even though I rejected it at first).