How to select binning and how to center the values for the axis values?

Hi,
I have two parameters I would like to plot against each other in a 2D map.
The x-axis consists of 7 values and the z-axis consists of 5 values.
x = -4, -2, 0, 6 and 12.
z = -3, -2, -1, 0, 1, 2, 3
But instead of measuring 35 values, I only have 29.

However, when I fill the map it looks like a higher binning then it should, for example x=2 was never measured but it appears as it would have.
Furthermore, how can I ensure that “a binning is filled centered with the corresponding axis values”? In other words: x=-2, z=0 looks fine, but x=-4, z=0 does not. Also x=-4, z=-2 not. And so on.

{
   TCanvas *c = new TCanvas("c","Graph2D example",0,0,700,700);

   TGraph2D *dt = new TGraph2D();
   dt->SetTitle("APD ratio map; z / mm; x / mm; ");

   Double_t x[29]={-4, -4, -2, -2, -2, -2, -2, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 12, 12, 12, 12, 12, 12, 12};
   Double_t z[29]={-1, 1, -2, -1, 0, 1, 2, -3, -2, -1, 0, 1, 2, 3, -3, -2, -1, 0, 1, 2, 3, -3, -2, -1, 0, 1, 2, 3};
   Double_t ratio[29]={(1.1994), (1.36), (1.0446),
   (1.0223), (1.1799), (1.316), (1.227), (0.9449),
   (1.1245), (1.0256), (1.032), (0.97), (1.0338), (1.02), (1.086),
   (1.052), (1.08), (1.065), (1.05), (1.0681), (1.05), (1.0475),
   (0.987), (1.13036), (1.0235), (1.38), (1.071), (1.096), (1.07)};

   for(Int_t N=0; N<29; N++){
      Double_t ratio_x = x[N];
      Double_t radius = z[N];
      Double_t degree = ratio[N];
      dt->SetPoint(N, radius, ratio_x, degree);
   }

   dt->GetXaxis()->SetRangeUser(-1,13);
   dt->GetYaxis()->SetRangeUser(-4,4);
   dt->GetZaxis()->SetLabelOffset(1.8);
   dt->SetNpx(7);
   dt->SetNpy(5);
   gStyle->SetPalette(1);
   dt->Draw("colz");
}

When you draw with the COLZ option an histogram is filled via an interpolation on the Delaunay’s triangles. Try dt->Draw("TRI1 P0"); . It should be ok. Is it ?
Now, if you want a ‘COLZ’ plot you better use directly an histogram than having an intermediate TGraph2D

1 Like

Thank you! These triangles are working but it’s not what I’m looking for.
I’m trying to do it with a histogram now, as you adviced me to do. But I’m making something wrong. I’ve done something quite similar and it’s working there but though it’s quite short and simple it seems it’s been a while ago… at least I don’t get the issue… Can you please have a look?

	{
	TCanvas *c = new TCanvas("c","Graph2D example",0,0,700,700);

        int x[29]={-4, -4, -4, -2, -2, -2, -2, -2, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 12, 12, 12, 12, 12, 12, 12};
	int z[29]={-1, 0, 1, -2, -1, 0, 1, 2, -3, -2, -1, 0, 1, 2, 3, -3, -2, -1, 0, 1, 2, 3, -3, -2, -1, 0, 1, 2, 3};
	Float_t ratio[29]={(1.1994), (1.36), (1.0446), 
			    (1.0223), (1.1799), (1.316), (1.227), (0.9449), 
			    (1.1245), (1.0256), (1.032), (0.97), (1.0338), (1.02), (1.086),
			    (1.052), (1.08), (1.065), (1.05), (1.0681), (1.05), (1.0475),
			    (0.987), (1.13036), (1.0235), (1.38), (1.071), (1.096), (1.07)};

     	TH2D* Hist=new TH2D("Hist","",7,-3,3,5,-4,12);

	for(int i=0; i<29; i++)
	{
		cout << "x: " << x[i] << "   z: " << z[i] << "   ratio: " << ratio[i] << endl;
		Hist->Fill(z[i], x[i], ratio[i]);
	}

	Hist->GetXaxis()->SetTitle("z / mm");
	Hist->GetYaxis()->SetTitle("x / mm");
	Hist->SetTitle("Apd ratios");
	Hist->Draw("A, colz");

	return 0;
	}

returns to

Yes I will. Also notice the right way to post code in the forum. The way you are doing it now forces to do editing to reformat the macro correctly.

1 Like

Sorry. Edited it with the " ```" tags. But it looks the same?
Thanks a lot in advance!

It seems it’s up to the binning:

	{
	TCanvas *c = new TCanvas("c","Graph2D example",0,0,700,700);

        int x[29]={-4, -4, -4, -2, -2, -2, -2, -2, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 12, 12, 12, 12, 12, 12, 12};
	int z[29]={-1, 0, 1, -2, -1, 0, 1, 2, -3, -2, -1, 0, 1, 2, 3, -3, -2, -1, 0, 1, 2, 3, -3, -2, -1, 0, 1, 2, 3};
	Float_t ratio[29]={(1.1994), (1.36), (1.0446), 
			    (1.0223), (1.1799), (1.316), (1.227), (0.9449), 
			    (1.1245), (1.0256), (1.032), (0.97), (1.0338), (1.02), (1.086),
			    (1.052), (1.08), (1.065), (1.05), (1.0681), (1.05), (1.0475),
			    (0.987), (1.13036), (1.0235), (1.38), (1.071), (1.096), (1.07)};

     	TH2D* Hist=new TH2D("Hist","",7, -3.5 ,3.5 ,16, -4.5, 12.5);

	for(int i=0; i<29; i++)
	{
		cout << "x: " << x[i] << "   z: " << z[i] << "   ratio: " << ratio[i] << endl;
		Hist->Fill(z[i], x[i], ratio[i]);
	}

	Hist->GetXaxis()->SetTitle("z / mm");
	Hist->GetYaxis()->SetTitle("x / mm");
	Hist->SetTitle("Apd ratios");
	Hist->Draw("A, COLZ");

	return 0;
	}

guess is what I was looking for.
So all I’m missing now are the axis :slight_smile:

No … not at all… it is much better.
I am looking at you macro. The first thing I see i that in the Fill() you inverted x and z .
Now I have an other question: Are the values in the x and z values the center of the bins ?

Hist->Draw("COLZ");

Thanks for looking it up!
And yes, I inverted it because I mixed it up. Sorry for that! I also changed the binning and I’m quite sure this is what I was looking for.
But the axis won’t show up.

remove option “A” in the the Draw()

1 Like

:slight_smile:
Alright, thanks a lot for the support! Sorry for bothering you with these quite simple stuff. I’ve spent some time in other languages so I probably forgot these basic commands…
But again, thank you!

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