Scatter Plot with Colorbar

Hi,

I was trying to draw a scatter plot (2D Graph) with a color bar (the z-axis) with the draw option g_temp->Draw("x:y:z","colz"); following the earlier post
https://root-forum.cern.ch/t/scatter-contour-plots/18755?u=saumyenk
However, it says “too many arguments” to the “Draw” function as in below:

/Scatter.C:13:24: error: too many arguments to function call, expected at most single argument 'option',
      have 2 arguments
  g_temp->Draw("x:y:z","colz"); 
  ~~~~~~~~~~~~         ^~~~~~
/usr/local/etc/../include/TGraph2D.h:95:4: note: 'Draw' declared here
   virtual void          Draw(Option_t *option="P0");

Only the "colz" working fine, but I want a scatter plot instead.

I have attached the example macro and a data file. Could kindly guide me what option should I give?
data.txt (63.5 KB)
Scatter.C (353 Bytes)


ROOT Version: 6.16/00
Platform: linuxx8664gcc
Compiler: g++ 7.5.0


That’s for drawing trees or ntuples, not for graphs (or TGraph2D in your case). Anyway, I don’t think TGraph2D is appropriate for what you want; just use a TH2 (e.g. TH2D) and draw it with “colz”
https://root.cern/doc/master/classTHistPainter.html#HP14

If you use a newer ROOT version, you can use New class TScatter - ROOT

1 Like

Thanks a lot, @dastudillo and @ferhue .
It seemed easier with the TScatter class. However, in another machine with root 6.24/08 I tried. It doesn’t have the class. Could you tell me from which version the TScatter class is available?

6.30 at least is needed

Okay, I see. Thanks a lot, @ferhue . Let me try to install it and try.

If you are going to install a newer version, get the latest (currently 6.32), unlesss you have a very specific reason to avoid it,

1 Like

Okay, I will do that @dastudillo .

I just needed a clarification and an advice.

If I install two ROOT – 1. with snap and 2. by building from source; will they create a conflict anyhow? For other ROOT-dependent packages (e.g. Delphes) which root they will consider?

And how do you think is the best way to install ROOT – in a specified installation directory or in the /usr/local.

Thanks a lot.
Regards,

Maybe a developer can give better advice, but I’d say it’s ok as long as you keep the installs in different folders; remember that before using any ROOT you have to source the file /path/to/that/root/bin/thisroot.sh (for bash shells), so that will define which ROOT is used at any time.

I confirm what @dastudillo said. I have several ROOT versions installed on my machine, and I can easily switch between each of them using thisroot.sh.

I think it is better to download the binary release, rather than building from source.

Download different versions, and unzip them under /opt/root632 and /opt/root630 to easily switch between them. And uninstall the snap version (in my opinion).

Thanks a lot guys, @dastudillo @couet @ferhue . I will try to build locally in a specific directories and source everytime. Binary dists, I don’t know if that’ll be suitable for me as I have a some modified library files.

But in the meantime I’m facing a issue putting a axis title for the colorbar. I tried with the attached macro. But the Z-axis title won’t appear. I saw to access the axes one has to call the GetHistogram() function. But still… Although, the function I’m using is not returning any error. Also the limits of the Z-axis cannot be changed. Is it anyway possible?

Thanks for any help.
Regards,

scatter.C (3.5 KB)

I think these two features you are mentioning might not yet be implemented, as it is a fairly new class. Maybe @couet or @linev can confirm this.

Concerning binaries, you need to have the list of prerequisites installed, listed on the webpage, for them to work.

1 Like
void scatter()
{
   auto canvas = new TCanvas();
   gStyle->SetPalette(kRainBow, 0, 0.5); // define a transparent palette


   const int n = 100;
   double x[n];
   double y[n];
   double c[n];
   double s[n];

   // Define four random data set
   auto r  = new TRandom();
   for (int i=0; i<n; i++) {
      x[i] = 100*r->Rndm(i);
      y[i] = 200*r->Rndm(i);
      c[i] = 300*r->Rndm(i);
      s[i] = 400*r->Rndm(i);
   }

   auto scat = new TScatter(n, x, y,c,s);
   scat->SetMarkerStyle(20);
   scat->SetMarkerColor(kRed);
   scat->SetTitle("Scatter plot;X;Y");
   scat->Draw("A");
   canvas->Modified();
   canvas->Update();
   TPaletteAxis *palette = (TPaletteAxis*)scat->GetGraph()->GetListOfFunctions()->FindObject("palette");
   palette->SetX1NDC(0.86);
   palette->SetX2NDC(0.90);
   palette->SetTitle("Palette Title");
   canvas->Modified();
   canvas->Update();
}

Thank you so much @couet . The color bar title is set properly.

Regards,
Saumyen

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