# Two-dimensional scatter plot fitting

**URL:** <https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630>\
**Category:** ROOT\
**Tags:** root\
**Created:** [December 22, 2023, 1:37am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630 "2023-12-22T01:37:12Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![401zjf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/4/0ea827/32.png) [@401zjf](https://root-forum.cern.ch/u/401zjf)\
**Post date:** [December 22, 2023, 1:37am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/1 "2023-12-22T01:37:12Z")

</div>

Hi Rooters,

We have a 2D scattering plot, as attached here; we want to fit the 1 to 3 sigma confidence interval contours on the selected data population (for instance, the bottom right data population), similar to the attached picture. Can somebody please tell me an appropriate example code to refer to? I searched around but couldn’t find one. Thank you for your help.

our scattering plot：

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/6/7/67f4c072229e7e059c1b014ab718aabf740a7e76.png)

example scattering plot：  
 ![53d1f6bbcaa7eef3e935f782f1749ca](https://root-forum.cern.ch/uploads/default/original/3X/d/1/d132b96bc8ec2da4b2dd1808298a0fa2381fe9df.png)

\_ROOT Version:6.24  
\_Platform:Centos7.7  
\_Compiler:g++

* * *

---

<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:** [December 22, 2023, 7:41am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/2 "2023-12-22T07:41:37Z")

</div>

> [@401zjf](#):
>
> I searched around but couldn’t find one

Did you look at [this page](https://root.cern/doc/master/group __tutorial__ fit.html) ?

---

<div class="post-metadata">

**Author:** ![401zjf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/4/0ea827/32.png) [@401zjf](https://root-forum.cern.ch/u/401zjf)\
**Post date:** [December 22, 2023, 11:07am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/3 "2023-12-22T11:07:05Z")

</div>

I’ve looked at this page and found some examples in it, but they don’t quite apply to our data. We want to fit the contour lines 1σ, 2σ, and 3σ, but we don’t know what kind of fitting function is suitable.Can you recommend some examples of fitting methods suitable for our data? Thank you very much for your help! !

---

<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:** [December 22, 2023, 11:57am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/4 "2023-12-22T11:57:12Z")

</div>

May be [this thread](https://root-forum.cern.ch/t/confidence-levels-in-root/31602) is close to what you are looking for ?

---

<div class="post-metadata">

**Author:** ![401zjf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/4/0ea827/32.png) [@401zjf](https://root-forum.cern.ch/u/401zjf)\
**Post date:** [December 27, 2023, 8:49am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/5 "2023-12-27T08:49:01Z")

</div>

I’m trying to plot a scatter plot into a 2D histogram.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/0/5/05ef28b5e5c208cacc97c319d8a91116e7cdc7ce.png)

When setting the x-axis to logarithmic coordinates, there is a problem with the histogram display.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/e/d/ed6a37d7d6446883976429f92e3344d392578e4b.png)

When I use the CONT style to draw a histogram, the result is as follows. It seems that all the data with x less than 0.5 have disappeared.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/f/f/ff341a291ed1d40e1ef847ed91873c5aedf9172a.png)  
why is this happening？

here is my code

```auto
   TCanvas *c2 = new TCanvas("canvas", "2D Histogram", 800, 600);
   TH2F *histogram = new TH2F("histogram", "2D Histogram", 100, 1e-6, 10, 100, 0, 40000);
for (int i = 0; i < 4721; ++i) {
        double x = Graph_fx2[i];
        double y = Graph_fy2[i];
        histogram->Fill(x, y);
    }

   c2->cd();
  
   histogram->Draw("CONT1");

```

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [December 27, 2023, 9:42am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/6 "2023-12-27T09:42:03Z")

</div>

Hi,  
To obtain the contour lines of a set of 2d points, you can use for example the `TRobustEstimator` class in ROOT.  
I attached here an example drawing the contour probability at 50, 90 and 99%.

Best regards

Lorenzo  
[example\_CovarianceEstimator.C](https://root-forum.cern.ch/uploads/short-url/m3xjMsKTJjRAnptl5ORmkyDNiwd.C) (2.2 KB)

---

<div class="post-metadata">

**Author:** ![401zjf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/4/0ea827/32.png) [@401zjf](https://root-forum.cern.ch/u/401zjf)\
**Post date:** [December 27, 2023, 9:58am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/7 "2023-12-27T09:58:59Z")

</div>

hi ， Moneta  
thanks for your reply. I have tried your code, but there is some wrong with it. Do I need to add any header files when using this class?

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/4/d/4dd645b9e3b09f91de89706e546ede19c966ae2a.png)

my root version is 6.24

---

<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:** [December 27, 2023, 10:51am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/8 "2023-12-27T10:51:59Z")

</div>

TScatter needs ROOT \>= 6.30. In this example you can use TGraph instead, or update your ROOT.

---

<div class="post-metadata">

**Author:** ![401zjf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/4/0ea827/32.png) [@401zjf](https://root-forum.cern.ch/u/401zjf)\
**Post date:** [December 27, 2023, 11:55am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/9 "2023-12-27T11:55:57Z")

</div>

thanks a lot !!

---

<div class="post-metadata">

**Author:** ![401zjf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/4/0ea827/32.png) [@401zjf](https://root-forum.cern.ch/u/401zjf)\
**Post date:** [December 27, 2023, 12:12pm UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/10 "2023-12-27T12:12:10Z")

</div>

When I use this line of code to set the opacity, the transparency of the ellipse does not change. Is this also a problem with my root version?  
`ellipse3->SetFillColorAlpha(4, 0.1);`

---

<div class="post-metadata">

**Author:** ![401zjf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/4/0ea827/32.png) [@401zjf](https://root-forum.cern.ch/u/401zjf)\
**Post date:** [December 27, 2023, 1:16pm UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/11 "2023-12-27T13:16:28Z")

</div>

hi，moneta

I have tired your example.I replacing the data in the example with my data, the resulting ellipse looks like this.  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/5/4/540e931393a5e09dc5b3d638eb90263567a7e938.png)  
These ellipses look weird in log coordinates

![image](https://root-forum.cern.ch/uploads/default/original/3X/9/9/99079bded85102efea8e5ddbf55ce376aecaab6f.png)

How can I draw such ellipse in log coordinates?

---

<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:** [December 29, 2023, 4:34pm UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/12 "2023-12-29T16:34:27Z")

</div>

Try to set the Line Width ellipses to 0:

```auto
   ellipse[1-3]->SetLineWidth(0);

```

---

<div class="post-metadata">

**Author:** ![401zjf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/4/0ea827/32.png) [@401zjf](https://root-forum.cern.ch/u/401zjf)\
**Post date:** [January 3, 2024, 7:33am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/13 "2024-01-03T07:33:03Z")

</div>

All ellipses disappeared when I set the Linew Width ellipses to 0

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/7/b/7b90df0dc6c89416fb6ac32f97f5615092c0cb4a.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:** [January 9, 2024, 8:10am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/14 "2024-01-09T08:10:48Z")

</div>

I used the example posted by @moneta and I get this with line width 0:

 ![Screenshot 2024-01-09 at 09.10.19](https://root-forum.cern.ch/uploads/default/original/3X/b/5/b5f68eb3581afa1fdf425165162ec99e0ba01edc.png)

---

<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:** [January 23, 2024, 8:10am UTC](https://root-forum.cern.ch/t/two-dimensional-scatter-plot-fitting/57630/15 "2024-01-23T08:10:51Z")

</div>

Closed on January 23, 2024, 8:10am UTC

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