# Scatter plot of vectors

**URL:** <https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708>\
**Category:** Newbie\
**Created:** [June 10, 2025, 4:20pm UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708 "2025-06-10T16:20:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![phylmath](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phylmath/32/38400_2.png) [@phylmath](https://root-forum.cern.ch/u/phylmath)\
**Post date:** [June 10, 2025, 4:20pm UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/1 "2025-06-10T16:20:28Z")

</div>

_ROOT Version:_ 6.32.04  
_Platform:_ WSL2 Ubuntu

Hello! Is it possible to make a scatter plot of two float vectors? Thanks in advance. Cheers.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [June 10, 2025, 8:11pm UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/2 "2025-06-10T20:11:43Z")

</div>

You can put them in a TGraph (see the [constructor with 2 float vectors](https://root.cern/doc/master/classTGraph.html#a22bc73dfa5cba2692a67a4840bb282a7)) and then draw the graph (see the [draw options](https://root.cern/doc/master/classTGraphPainter.html)). Probably you’ll want to [Sort](https://root.cern/doc/master/classTGraph.html#a6192cff25c7ab28933c91f2869020cce) the graph before drawing it to avoid issues.  
An alternative, a bit more elaborate, could be to put the data (either as vectors, or each pair of points as an entry) in a tree (e.g. as branches “x” and “y”) and then draw the tree as scatter plot (e.g. `tree->Draw("y:x")`.

---

<div class="post-metadata">

**Author:** ![phylmath](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phylmath/32/38400_2.png) [@phylmath](https://root-forum.cern.ch/u/phylmath)\
**Post date:** [June 11, 2025, 10:57am UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/3 "2025-06-11T10:57:41Z")

</div>

Thanks for the response. This is a gist of my current code.

```auto
// define vectors
TVectorF vec1(size), vec2(size);

// loop fill vectors
for(i) {
    vec1[i] = data;
    vec1[i] = data;
}

// define scatter graph
TGraph *graph= new TGraph(vec1, vec2);

```

However, this gives a null output when I try to draw this graph. Am I missing something?

---

<div class="post-metadata">

**Author:** ![phylmath](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phylmath/32/38400_2.png) [@phylmath](https://root-forum.cern.ch/u/phylmath)\
**Post date:** [June 11, 2025, 11:14am UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/4 "2025-06-11T11:14:19Z")

</div>

And for your second solution (which would be easier for me since the data exists in a tree branch already), I tried this code:

```auto
#include "TFile.h"
#include "TTree.h"
#include "TGraph.h"

// Code
void plot()
{
	TFile *input = new TFile("cut_FCC365.root", "READ");
	TTree *trees = (TTree*)input ->Get("tree_cut");

	TCanvas *canvas = new TCanvas("myCanvas", "Scatter Plot", 800, 600);
	trees->Draw("vec1:vec2");
}

```

However, this gives an empty canvas. I can also confirm the vectors are not empty from TBrowser. Any thoughts?

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [June 11, 2025, 11:16am UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/5 "2025-06-11T11:16:12Z")

</div>

Try the 4th constructor (“4/12”) on the page I linked above:

```auto
{
  vector<float> v1, v2;

  for (int i=0; i<3; ++i) {
    v1.push_back(i);
    v2.push_back(2*i);
  }
  TGraph *gr= new TGraph(3, &v1[0], &v2[0]);
  gr->Draw("a*");
}

```

---

<div class="post-metadata">

**Author:** ![phylmath](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phylmath/32/38400_2.png) [@phylmath](https://root-forum.cern.ch/u/phylmath)\
**Post date:** [June 11, 2025, 11:40am UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/6 "2025-06-11T11:40:20Z")

</div>

Hi. It still gives this error:

```auto
warning: null passed to a callee that requires a non-null argument [-Wnonnull]

```

For more context, I have two separate scripts. The first conditions data and writes histograms and graphs into a ROOT file:

```auto
void script1() {
  
  TFile *output = new TFile("output.root", "RECREATE");
  TTree *tree= new TTree("tree", "tree");
  
  vector<float> v1, v2;

  for () {
    v1.push_back(data);
    v2.push_back(data);
  }

  TGraph *graph = new TGraph(3, &v1[0], &v2[0]);
  tree->Branch("graph", &graph, "graph");
  
  output->Write();
  output->Close();

}

```

The second reads this ROOT file and draws the plots:

```auto
void script2() {

  TFile *input = new TFile("output.root", "READ");
  TTree *tree= (TTree*)input ->Get("tree");

  TGraph *graph= (TGraph*)input ->Get("graph");
  graph->Draw("a*");

}

```

Sorry for the complexity. I am unable to merge it into the same code since the first runs Pythia/FastJet and I get an error when running the first script with the `root script1.cc` command.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 11, 2025, 11:48am UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/7 "2025-06-11T11:48:40Z")

</div>

```auto
void simpleTGraph() {
    const int n = 2000;
    double x[n], y[n];

    TRandom rand;
    for (int i = 0; i < n; ++i) {
        x[i] = i;
        y[i] = rand.Gaus(0, 1);
    }

    TGraph *graph = new TGraph(n, x, y);
    graph->SetTitle("Simple TGraph");
    graph->GetXaxis()->SetTitle("X Axis");
    graph->GetYaxis()->SetTitle("Y Axis");
    graph->SetMarkerStyle(1);

    graph->Draw("AP");
}

```

 ![Screenshot 2025-06-11 at 13.47.16](https://root-forum.cern.ch/uploads/default/original/3X/a/8/a8b6c3deec6081af1396659c24954a80a4a81944.jpeg)

---

<div class="post-metadata">

**Author:** ![phylmath](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phylmath/32/38400_2.png) [@phylmath](https://root-forum.cern.ch/u/phylmath)\
**Post date:** [June 11, 2025, 12:01pm UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/8 "2025-06-11T12:01:43Z")

</div>

@couet Thanks for the code. Is it possible to do the same with float vectors?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 11, 2025, 12:10pm UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/9 "2025-06-11T12:10:44Z")

</div>

Yes, just replace:

```auto
    double x[n], y[n];

```

by

```auto
    float x[n], y[n];

```

---

<div class="post-metadata">

**Author:** ![phylmath](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phylmath/32/38400_2.png) [@phylmath](https://root-forum.cern.ch/u/phylmath)\
**Post date:** [June 11, 2025, 12:54pm UTC](https://root-forum.cern.ch/t/scatter-plot-of-vectors/63708/10 "2025-06-11T12:54:52Z")

</div>

Thanks!
