TScatter: user-defined axis range?

Is there a way to affect the axis range of a TScatter? For example, here’s plot that takes advantage of variable colors and marker sizes:

The only problem is that I’d like to set the axis range to (15,15) for both axes, since that’s the size of the entire detector. Right now it’s just showing the sensors that detected the photons from an event.

I tried modifying the underlying histogram:

  auto scatterHist = scatter->GetHistogram();
  scatter->GetXaxis()->SetRangeUser(-15,15);
  scatter->GetYaxis()->SetRangeUser(-15,15);

From this forum post, I gather that doesn’t work, and indeed I got the errors reported in the past:

ROOT 6.32.00: ulast > fXmax, fXmax is used message

I see that I could define a TH2 of my own, and supply it to TScatter with SetHistogram. But it’s not clear how I would specify both the markers and the colors via a TH2F.

Any ideas?


ROOT Version: 6.34.04
Platform: macosxarm64 (Mac OS 15.6.1)
Compiler: clang version 18.1.8


For the TH2 way, try disabling its statistics, then draw it before TScatter, and draw TScatter without the “A” (axis) option:

 {
   auto canvas = new TCanvas();
   canvas->SetRightMargin(0.14);
   gStyle->SetPalette(kBird, 0, 0.6); // define a transparent palette

   const int n = 4;
   double x[n] = {40, 43, 45, 47};
   double y[n] = {70, 72, 75, 77};
   double c[n] = {20, 50, 30, 40};
   double s[n] = {6, 5, 10, 4};

   auto h2 = new TH2F("h2","h2",50,20,70,40,50,90);
   h2->SetStats(0);
   auto scatter = new TScatter(n, x, y, c, s);
   scatter->SetMarkerStyle(20);
   scatter->SetTitle("Scatter plot title;X title;Y title;Z title");

   h2->Draw();
   scatter->Draw();
}

Notice that the title and any x and y axis titles (and labels) will be the ones from the TH2, so set those accordingly.

That did it! Thanks!