# TGraph2D Edges Being Cut Off

**URL:** <https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776>\
**Category:** ROOT\
**Created:** [February 2, 2018, 2:24am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776 "2018-02-02T02:24:44Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbrown93](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/7feea3/32.png) [@gbrown93](https://root-forum.cern.ch/u/gbrown93)\
**Post date:** [February 2, 2018, 2:24am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/1 "2018-02-02T02:24:44Z")

</div>

I’m trying to draw a TGraph2D with the CONT1Z option, but the edges get cut off. When I set the axes to log scale, the issue is then amplified. Here’s two images showing linear vs log scale.

 ![Linear vs Log axes](https://i.imgur.com/XfQSe7Y.png)

This is the code I’m using.

```auto
TCanvas* canvas = new TCanvas("c1","canvas",1024,576);
//canvas->SetLogx();
//canvas->SetLogy();
canvas->SetLogz();

gStyle->SetPalette(55);
gStyle->SetNumberContours(100);

TGraph2D* graph2 = new TGraph2D(masses.size(),masses.data(),mixings.data(),chisqr.data());
graph2->SetTitle(title2.c_str());
graph2->Draw("CONT1Z");

std::vector<TMarker*> markers;
for(int i = 0; i<masses.size(); ++i)
{
    TMarker* m = new TMarker(masses[i], mixings[i], 0);
    m->SetMarkerStyle(7);
    m->Draw("");
    markers.push_back(m);
}
canvas->Update();
canvas->SaveAs("Contours.png");

```

---

<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:** [February 2, 2018, 9:19am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/2 "2018-02-02T09:19:14Z")

</div>

Hi,

Can you post a running reproducer ? when I run your macro I get error messages like:

```auto
In file included from input_line_14:1:
/Users/couet/roottest/gbrown.C:10:36: error: use of undeclared identifier 'masses'
   TGraph2D* graph2 = new TGraph2D(masses.size(),masses.data(),mixings.data(),chisqr.data());
                                   ^

```

Thanks.

---

<div class="post-metadata">

**Author:** ![gbrown93](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/7feea3/32.png) [@gbrown93](https://root-forum.cern.ch/u/gbrown93)\
**Post date:** [February 2, 2018, 6:31pm UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/3 "2018-02-02T18:31:33Z")

</div>

Here’s one I made using some tutorial code.

```auto
void graph2d()
{
   TCanvas *c1 = new TCanvas("c1");

   c1->SetLogx();
   c1->SetLogy();
   c1->SetLogz();

   Double_t rnd, x, y, z;
   Double_t e = 0.3;
   Int_t nd = 500;

   TRandom r;
   TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6);
   f2->SetParameters(1,1);
   TGraph2D *dte = new TGraph2D(nd);

   // Fill the 2D graph
   for (Int_t i=0; i<nd; i++) {
      f2->GetRandom2(x,y);
      rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
      z = f2->Eval(x,y)*(1+rnd);
      dte->SetPoint(i,x,y,z);
   }

   dte->Draw("CONT1Z");
}

```

---

<div class="post-metadata">

**Author:** ![gbrown93](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/7feea3/32.png) [@gbrown93](https://root-forum.cern.ch/u/gbrown93)\
**Post date:** [February 3, 2018, 7:26am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/4 "2018-02-03T07:26:17Z")

</div>

I found some information explaining the problem, but I am still not sure what the solution is.

[https://sft.its.cern.ch/jira/si/jira.issueviews:issue-html/ROOT-6977/ROOT-6977.html](https://sft.its.cern.ch/jira/si/jira.issueviews:issue-html/ROOT-6977/ROOT-6977.html)

Maybe I need to rebin the TGraph2D’s histogram?

---

<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:** [February 5, 2018, 9:39am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/6 "2018-02-05T09:39:03Z")

</div>

When you use histograms drawing option for TGraph2D the TGraph2D is not directly drawn but instead a 2D histogram is created from an interpolated on the 2D graph and then this histogram is plotted. The native option P, P0, or TRI plot the 2D graph directly. I will check your example .

---

<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:** [February 5, 2018, 1:36pm UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/7 "2018-02-05T13:36:28Z")

</div>

The CONT1 option draws the contours lines connecting the bins’ centers. With log scale the X and Y do not have all the same surface like in linear scale. The cut you see comes from that but it is not related to TGraph2D. Any TH2 will be the same. May be draw your histogram with option COL instead.

---

<div class="post-metadata">

**Author:** ![gbrown93](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/7feea3/32.png) [@gbrown93](https://root-forum.cern.ch/u/gbrown93)\
**Post date:** [February 6, 2018, 2:45pm UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/8 "2018-02-06T14:45:03Z")

</div>

I made an attempt at a solution by binning the underlying histogram with log scaled bins. It fixes the cutoff and the bins are spaced evenly now in the log scaled canvas, however it seems to have a lot of issues with the interpolation of the TGraph2D. I’ve found the SetNpx and SetNpy help a lot with the cutoff issue so I’ll stick with that for now, but here’s the code of my attempt. I don’t know if it is viable or not. Thanks for your time and help though.

```auto
void graph2d()
{
    TCanvas *c1 = new TCanvas("c1");

    c1->SetLogx();
    c1->SetLogy();
    //c1->SetLogz();

    Double_t rnd, x, y, z;
    Double_t e = 0.3;
    Int_t nd = 500;

    TRandom r;
    TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",-6,6,-6,6);
    f2->SetParameters(1,1);
    TGraph2D *dte = new TGraph2D(nd);

    // Fill the 2D graph
    for (Int_t i=0; i<nd; i++) {
        f2->GetRandom2(x,y);
        rnd = r.Uniform(-e,e); // Generate a random number in [-e,e]
        z = f2->Eval(x,y)*(1+rnd);
        dte->SetPoint(i,x,y,z);
    }

    double nbins = 60;
    std::vector<double> xbins(nbins+1), ybins(nbins+1);
    {
        double minx = 1;//dte->GetXmin();
        double maxx = dte->GetXmax();
        double miny = 1;//dte->GetYmin();
        double maxy = dte->GetYmax();
        double xstep = std::pow(maxx/minx, 1.0/nbins);
        double ystep = std::pow(maxy/miny, 1.0/nbins);
        for(int i = 0; i<=nbins; ++i)
        {
            xbins[i] = minx * std::pow(xstep, i);
            ybins[i] = miny * std::pow(ystep, i);
        }
    }
    TH2D* bins = new TH2D("bins", "", nbins, (double*)xbins.data(), nbins, (double*)ybins.data());
    bins->SetStats(false);
    dte->SetHistogram(bins);
    
    //dte->Draw("CONT1Z");
    dte->Draw("COL");
}

```

---

<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:** [February 6, 2018, 2:51pm UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/9 "2018-02-06T14:51:29Z")

</div>

If I draw the Delaunay triangles corresponding to you TGraph2D (drawing option TRI1) I get the following picture As you can see they are far to cover the whole range. All the white zones are impossible to interpolate.

 ![30](https://root-forum.cern.ch/uploads/default/original/2X/7/73689fe3fc65dceaf425a73463a4306ef8cc16ff.jpg)

---

<div class="post-metadata">

**Author:** ![gbrown93](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/7feea3/32.png) [@gbrown93](https://root-forum.cern.ch/u/gbrown93)\
**Post date:** [February 6, 2018, 3:19pm UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/10 "2018-02-06T15:19:46Z")

</div>

My data points (marked as black dots) do cover the whole area.

---

<div class="post-metadata">

**Author:** ![gbrown93](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/7feea3/32.png) [@gbrown93](https://root-forum.cern.ch/u/gbrown93)\
**Post date:** [February 14, 2018, 1:03am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/11 "2018-02-14T01:03:14Z")

</div>

I’ve decided to look for different software for the contour plot. Root’s just isn’t working well enough.

---

<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:** [February 14, 2018, 9:56am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/12 "2018-02-14T09:56:22Z")

</div>

As I said it just fill the bins below the triangles doing a linear interpolation on the triangles. Mays be that’s not what you need.

---

<div class="post-metadata">

**Author:** ![antodl](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3da27b/32.png) [@antodl](https://root-forum.cern.ch/u/antodl)\
**Post date:** [February 14, 2018, 10:55am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/13 "2018-02-14T10:55:19Z")

</div>

Hello,

a workaround I used in the past for a similar issue (I was interested only in the x axis) is to populate the TGraph2D with log(xval), log(yval), zvalue, then draw the graph and set the plot axes so that they are not visible

```auto
aGraph->GetXaxis()->SetLabelSize(0);
aGraph->GetXaxis()->SetTickLength(0);

```

finally add logscale TGaxis (adjust values according to your need)

```auto
TGaxis *anewLogaxis = new TGaxis(1E-2, 1E-10, 7.0, 1E-10, 1E-2, 1E1, 510, "G");
anewLogaxis->Draw()

```

Greetings,  
A.

---

<div class="post-metadata">

**Author:** ![gbrown93](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/7feea3/32.png) [@gbrown93](https://root-forum.cern.ch/u/gbrown93)\
**Post date:** [February 15, 2018, 5:06am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/14 "2018-02-15T05:06:38Z")

</div>

Thanks, antodl. This workaround worked perfectly.

I had some difficulty hiding the axes, but after a lucky guess, I found that this worked.

```auto
graph2->GetHistogram()->GetXaxis()->SetLabelSize(0);
graph2->GetHistogram()->GetXaxis()->SetTickLength(0);

```

I’m getting a much nicer result now.

 ![result](https://i.imgur.com/BUK1LL5.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 15, 2018, 7:29am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/15 "2018-02-15T07:29:22Z")

</div>

> [@Problem with logarithmic color axis/colz with TNtuple](https://root-forum.cern.ch/t/problem-with-logarithmic-color-axis-colz-with-tntuple/14058/4):
>
> This seems to be a bug in ROOT (the numbers on the “z” scale, i.e. on the color palette, are wrong in “Logz” mode) and it seems to be closely related to [How plot only fit result of the TGraph?](https://root-forum.cern.ch/t/how-plot-only-fit-result-of-the-tgraph/14012/15)

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 1, 2018, 7:29am UTC](https://root-forum.cern.ch/t/tgraph2d-edges-being-cut-off/27776/16 "2018-03-01T07:29:29Z")

</div>

Closed on March 1, 2018, 7:29am UTC

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