# Two-dimensional scatter plot fitting

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：

example scattering plot：

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

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! !

May be this thread is close to what you are looking for ?

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

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

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.

why is this happening？

here is my code

``````   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");
``````

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 (2.2 KB)

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?

my root version is 6.24

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

thanks a lot !!

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);`

hi，moneta

I have tired your example.I replacing the data in the example with my data, the resulting ellipse looks like this.

These ellipses look weird in log coordinates

How can I draw such ellipse in log coordinates?

Try to set the Line Width ellipses to 0:

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

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

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

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