2D scatter plot with a color map

From: Abdurahim Rakhman <rahim_at_jlab.org>
Date: Fri, 9 Mar 2012 01:37:31 -0500


Dear All,

I want to make a 2D scatter plot with a color map. I'm using TGraph2D and don't know the option of getting the color map working.

I would appreciate if someone can show me how.

The example macro I'm using is the following,

void ScatPlotGauss(){
  Int_t i = 0, j = 0, k = 0, n = 0;
  Double_t ux = 0., uy = 0.;
  Double_t Sx = 2., Sy = 2.;
  const Int_t MAXP = 10;
  Double_t a[MAXP+1], b[MAXP+1];
  Double_t X[(MAXP+1)*(MAXP+1)], Y[(MAXP+1)*(MAXP+1)], Z[(MAXP+1)*(MAXP+1)];

  gStyle->SetPalette(1);

	for (Int_t i = 0; i <= MAXP; i++){
	  a[i] = i - MAXP/2.;
	    for (Int_t j = 0; j <= MAXP; j++){
	      b[j] = j - MAXP/2.;
	      X[k] = a[i];
	      Y[k] = b[j];
	      Z[k] = TMath::Exp(-((a[i]-ux)*(a[i]-ux)/(2.*Sx*Sx) + (b[j]-uy)*(b[j]-uy)/(2.*Sy*Sy)));
	      k++;
	    }
	  }

	n = k;
	

  TCanvas* c1 = new TCanvas("Scat","Scat",10,10,500,500);

  TGraph2D *g1 = new TGraph2D(n, X, Y, Z);   g1->Draw("scat4z");

  g1->GetYaxis()->SetTitle("Y");
  g1->GetXaxis()->SetTitle("X");

} Received on Fri Mar 09 2012 - 07:37:53 CET

This archive was generated by hypermail 2.2.0 : Fri Mar 09 2012 - 11:50:01 CET