# Draw scatterplot graph on top of 2D histogram

**URL:** <https://root-forum.cern.ch/t/draw-scatterplot-graph-on-top-of-2d-histogram/53242>\
**Category:** Newbie\
**Tags:** root\
**Created:** [January 24, 2023, 3:23pm UTC](https://root-forum.cern.ch/t/draw-scatterplot-graph-on-top-of-2d-histogram/53242 "2023-01-24T15:23:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nouvilas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/aeb1de/32.png) [@Nouvilas](https://root-forum.cern.ch/u/Nouvilas)\
**Post date:** [January 24, 2023, 3:23pm UTC](https://root-forum.cern.ch/t/draw-scatterplot-graph-on-top-of-2d-histogram/53242/1 "2023-01-24T15:23:24Z")

</div>

Hi, I’m new to Root, and I’m trying to draw a set of points ((x,y) coordinates in a .txt) on top (as in, in the same graph) of a 2D histogram (a TH2C I’ve managed to draw on a canvas, with “colz”). I’ve seen the TMultiGraph class, but from what I gather I don’t think it can be used with a TH2C.  
How could I achieve this?

This is the code I have so far:

```auto
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINES 65536

int prueba()
{
    FILE* fp;
    char buffer[1000];
    double x[MAXLINES];
    double y[MAXLINES];
    int counts[MAXLINES];
    size_t lines = 0;
    size_t i;

    // Open file in reading mode
    fp = fopen("anger-integral.txt", "r");
    // Check if the file has been opened
    if (NULL == fp) {
        printf("file can't be opened \n");
        return 1;
    }
    // Read line by line and save the values in corresponding variables
    while (fgets(buffer, sizeof buffer, fp) != NULL) {
        if (sscanf(buffer, "%lf%lf%i",
                   &x[lines], &y[lines], &counts[lines]) == 3) {
            ++lines;
        }
    }
    // Close the file
    fclose(fp);

    // Histogram size and limits
    int binN = 256;
    int xmin = -1, xmax = 1;
    int ymin = -1, ymax = 1;

    // Create root canvas
    TCanvas* c_1 = new TCanvas("c_1","Llenado de campo", 1000,0, 1500,1500);
    // Create histogram
    TH2C* htwoD = new TH2C("Histograma","Llenado de campo", binN, xmin, xmax, binN, ymin, ymax);
    // Fill histogram with read data
    for (i = 0; i < lines; i++) {
      htwoD->Fill(x[i],y[i],counts[i]);
    }
    // Draw histogram to canvas
    htwoD->Draw("colz");
    
    return 0;
}

```

Thank you in advance.

* * *

_ROOT Version:_ 6.26/10  
_Platform:_ Ubuntu 22.04  
_Compiler:_ Not Provided

* * *

---

<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 24, 2023, 4:14pm UTC](https://root-forum.cern.ch/t/draw-scatterplot-graph-on-top-of-2d-histogram/53242/2 "2023-01-24T16:14:26Z")

</div>

Welcome to the ROOT forum.

You want something like the 2nd example here:  
[https://root.cern/doc/v626/classTHistPainter.html#HP15](https://root.cern/doc/v626/classTHistPainter.html#HP15)

?

---

<div class="post-metadata">

**Author:** ![Nouvilas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/aeb1de/32.png) [@Nouvilas](https://root-forum.cern.ch/u/Nouvilas)\
**Post date:** [January 24, 2023, 4:41pm UTC](https://root-forum.cern.ch/t/draw-scatterplot-graph-on-top-of-2d-histogram/53242/3 "2023-01-24T16:41:47Z")

</div>

No, I want to draw the points themselves, not the coordinates in text. I want to draw the points (as crosses for example) at the coordinates loaded from a .txt file (different from the .txt source of the histogram data). Something like the following (I’ve painted the crosses manually to illustrate):

 ![histo+points](https://root-forum.cern.ch/uploads/default/original/3X/6/a/6acb2758f5c8724698f5a73bc473b035151fa3cb.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 24, 2023, 5:03pm UTC](https://root-forum.cern.ch/t/draw-scatterplot-graph-on-top-of-2d-histogram/53242/4 "2023-01-24T17:03:47Z")

</div>

I see. Put the (x,y) values in a TGraph and draw it on top of the color plot:

```auto
h->Draw("col");
g->SetMarkerStyle(2);
g->Draw("P");

```
