# Scatter plots with different cuts

**URL:** <https://root-forum.cern.ch/t/scatter-plots-with-different-cuts/55527>\
**Category:** Newbie\
**Tags:** root\
**Created:** [July 3, 2023, 3:43pm UTC](https://root-forum.cern.ch/t/scatter-plots-with-different-cuts/55527 "2023-07-03T15:43:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![asanchezcastillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/asanchezcastillo/32/32921_2.png) [@asanchezcastillo](https://root-forum.cern.ch/u/asanchezcastillo)\
**Post date:** [July 3, 2023, 3:43pm UTC](https://root-forum.cern.ch/t/scatter-plots-with-different-cuts/55527/1 "2023-07-03T15:43:05Z")

</div>

Hello,

I am trying to plot two scatter plots with different cuts from the same TTree. The relevant part of my code is the following:

```auto
tree->SetMarkerStyle(1);
tree->Draw("X:Z",cut1);
TGraph *mygraph1 = (TGraph*)gPad->GetPrimitive("Graph");
tree->SetMarkerStyle(2);
tree->Draw("X:Z",cut2);
TGraph *mygraph2 = (TGraph*)gPad->GetPrimitive("Graph");

auto legend = new TLegend(0.1,0.7,0.48,0.9);
legend->SetHeader("title","C"); 
legend->AddEntry("mygraph1","g1","p");
legend->AddEntry("mygraph2","g2","p");
legend->Draw();

```

The main issue comes when plotting the legend. Apparently retrieving the TGraph via gPad is not enough, as both markers are plotted with the same style in the legend. I would like to know if there is a way of obtaining two different graphs, one for each of my cuts (that is, one for each time I do tree-\>Draw(…) ) so I can modify them separately.

---

<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:** [July 4, 2023, 5:41am UTC](https://root-forum.cern.ch/t/scatter-plots-with-different-cuts/55527/2 "2023-07-04T05:41:00Z")

</div>

To create a TGraph from Draw, search for “How to obtain more info from TTree::Draw” in the TTree documentation ([ROOT: TTree Class Reference](https://root.cern/doc/master/classTTree.html)). And to draw 2 or more graphs on the same pad/canvas, it’s safer to add them to a [TMultiGraph](https://root.cern/doc/master/classTMultiGraph.html) to ensure the axis scales are large enough to include all graphs.  
Also, remove the quotes for “mygraph1/2” in `legend->AddEntry()`

```auto
legend->AddEntry(mygraph1,"g1","p"); // same for mygraph2

```

Example:

```auto
    T->Draw("X:Z",cut1,"goff"); // "goff" so it is not actually drawn
    TGraph *mygraph1 = new TGraph(T->GetSelectedRows(),T->GetV2(),T->GetV1());
    T->Draw("X:Z",cut2,"goff");
    TGraph *mygraph2 = new TGraph(T->GetSelectedRows(),T->GetV2(),T->GetV1());

    mygraph1->SetMarkerStyle(1);
    mygraph2->SetMarkerStyle(2);

    TMultiGraph *mg = new TMultiGraph();
    mg->Add(mygraph1);
    mg->Add(mygraph2);
    mg->Draw("ap");

    auto legend = new TLegend(0.1,0.7,0.48,0.9);
    legend->SetHeader("title","C"); 
    legend->AddEntry(mygraph1,"g1","p");
    legend->AddEntry(mygraph2,"g2","p");
    legend->Draw();

```

---

<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:** [July 4, 2023, 8:11am UTC](https://root-forum.cern.ch/t/scatter-plots-with-different-cuts/55527/3 "2023-07-04T08:11:12Z")

</div>

[https://root.cern/doc/master/treegetval\_8C.html](https://root.cern/doc/master/treegetval_8C.html)

---

<div class="post-metadata">

**Author:** ![asanchezcastillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/asanchezcastillo/32/32921_2.png) [@asanchezcastillo](https://root-forum.cern.ch/u/asanchezcastillo)\
**Post date:** [July 4, 2023, 9:47am UTC](https://root-forum.cern.ch/t/scatter-plots-with-different-cuts/55527/4 "2023-07-04T09:47:06Z")

</div>

That indeed worked! Thanks.
