# Problem in plotting multigraph with data and time in x-axis

**URL:** <https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553>\
**Category:** Newbie\
**Tags:** root\
**Created:** [July 5, 2023, 3:44pm UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553 "2023-07-05T15:44:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [July 5, 2023, 3:44pm UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/1 "2023-07-05T15:44:42Z")

</div>

Hi,

I want to plot different data sets using multi graph as a function of date and time.

Unfortunately I get wrong results.

Attached here is the macro that I use.

Thank you  
[notworking.C](https://root-forum.cern.ch/uploads/short-url/2y2AKxWH0BbGkMIQEQ0Md8qMYYw.C) (2.3 KB)

---

<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 6, 2023, 6:43am UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/2 "2023-07-06T06:43:17Z")

</div>

I get this:

 ![Screenshot 2023-07-06 at 08.42.40](https://root-forum.cern.ch/uploads/default/original/3X/5/b/5b21e65c4c0d98f9ca2290346f02645d04f3f3c0.jpeg)

What is wrong ?

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [July 6, 2023, 2:30pm UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/3 "2023-07-06T14:30:47Z")

</div>

The x-axis values are wrong, it’s not the values that were given in the macro…

The x-axis that I have given in date as 29th June 2019 and the corresponding time… but its not what that it plots… similarly the y-axis points are in the range of 6000…

---

<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 6, 2023, 3:31pm UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/4 "2023-07-06T15:31:36Z")

</div>

The problem comes from the graph `gi`. You define it this way:

```auto
// DATA3

   float c[2], d[2];
   TDatime da9(2019,06,29,14,53,00);

   c[0] = 6314.7;
   d[0] = da9.Convert();

   TGraph *gi = new TGraph(4,d,c); // <<<< 4 points
   gi->SetMarkerColor(2);
   gi->SetMarkerSize(1);
   gi->SetMarkerStyle(21);
   gi->SetLineColor(2);

```

it has 4 points but you define only the first one. The 3 others are undefined. That’s why you get this wrong plot.

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [July 8, 2023, 10:54am UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/5 "2023-07-08T10:54:11Z")

</div>

Thank you!

I had one more question, I would like to plot the errors on the y-axis and the corresponding date and time on the x-axis, but since I have no error on the y-axis, how can I do it?

---

<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 10, 2023, 5:52am UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/6 "2023-07-10T05:52:21Z")

</div>

> [@gini](#):
>
> I would like to plot the errors on the y-axis and the corresponding date and time on the x-axis, but since I have no error on the y-axis, how can I do it?

You should use [TGraphErrors](https://root.cern/doc/master/classTGraphErrors.html).

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [July 10, 2023, 10:49am UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/7 "2023-07-10T10:49:49Z")

</div>

When I use TGraph errors, I have an error showing for arrays

{{

TDatime da1(2008,02,28,15,52,00);  
TDatime da2(2008,02,28,15,53,00);

//---------------------------------- ---------------------------------------  
int n\_3 = 2;  
float x\_3[3], y\_3[3], ex\_3[3], ey\_3[3] ;

//double x\_3[4] = {33.347, 32.859, 26.337, 23.311};  
x\_3[0] = 77.245;  
x\_3[1] = 79.245;  
y\_3[0] = da1.Convert();  
y\_3[1] = da2.Convert();

ex\_3[0] = 0;  
ex\_3[1] = 0;  
ey\_3[0] = 0;  
ey\_3[1] = 0;

auto gau\_tri = new TGraphErrors(2,y\_3,x\_3,ey\_3,ex\_3);  
gau\_tri-\>SetTitle(“{}^{176}Os”);  
gau\_tri-\>SetMarkerColor(2);  
gau\_tri-\>SetMarkerSize(1);  
gau\_tri-\>SetMarkerStyle(8);  
gau\_tri-\>SetLineColor(2);

//---------------------------------- ---------------------------------------

TDatime da5(2019,06,29,9,06,00);// RUN032  
TDatime da6(2019,06,29,11,06,00);// RUN034  
TDatime da7(2019,06,29,12,26,00);// RUN037

int n\_1 = 3;  
float x\_1[4], y\_1[4], ex\_1[4], ey\_1[4] ;

```
y_1[4] = {70.7, 59.1, 47.5};
x_1[4] = {da5.Convert(), da6.Convert(), da7.Convert()};

ex_1[4] = {0, 0, 0};
ey_1[4] = {0,0,0};

```

auto gir = new TGraphErrors(n\_1,x\_1,y\_1,ex\_1,ey\_1);  
gir-\>SetTitle(“{}^{177}Ir”);

gir-\>SetMarkerSize(1.6);  
gir-\>SetMarkerStyle(71);

//---------------------------------- Drawing ---------------------------------------  
TCanvas \*c11 = new TCanvas(“c11”,“c11\_Paper”,800,500);

TMultiGraph \*mg = new TMultiGraph();

mg-\>Add(gau\_tri, “C”);  
mg-\>Add(gir,“P”);

mg-\>SetMinimum(30);  
mg-\>SetMaximum(80);  
mg-\>Draw(“A”);  
gPad-\>Update();

mg-\>GetXaxis()-\>SetTimeDisplay(1);  
mg-\>GetXaxis()-\>SetNdivisions(-503);  
mg-\>GetXaxis()-\>SetTimeFormat(“%Y-%m-%d %H:%M”);  
mg-\>GetXaxis()-\>SetTimeOffset(0,“gmt”);

}

When I plot alone the first set of data it works, but when the second set of data is included I have errors…

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [July 10, 2023, 10:50am UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/8 "2023-07-10T10:50:15Z")

</div>

![image](https://root-forum.cern.ch/uploads/default/original/3X/5/a/5af2740d23cc70634f060d79dfe269d2d705e828.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:** [July 10, 2023, 11:30am UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/9 "2023-07-10T11:30:46Z")

</div>

Your array is size 4 et you provide 3 values.

---

<div class="post-metadata">

**Author:** ![jalopezg](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jalopezg/32/25844_2.png) [@jalopezg](https://root-forum.cern.ch/u/jalopezg)\
**Post date:** [July 10, 2023, 2:16pm UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/10 "2023-07-10T14:16:53Z")

</div>

> [@gini](#):
>
> float x\_1[4], y\_1[4], ex\_1[4], ey\_1[4] ;
> 
> ```auto
> y_1[4] = {70.7, 59.1, 47.5};
> x_1[4] = {da5.Convert(), da6.Convert(), da7.Convert()};
> 
> ex_1[4] = {0, 0, 0};
> ey_1[4] = {0,0,0};
> 
> ```

Actually, you are declaring `x_1`, `y_1`, `ex_1`, and `ey_1` to be arrays of 4 elements of type `float`. OTOH, you are assigning to one of the elements, e.g. `ex_1[4] = ...` (which, BTW, is also out of bounds).

I think you want to assign to the whole array instead (?); still you are missing one element, is that intentional?  
As a suggestion, you can provide values as part of the initializer for the array (note that the missing element will be set to `0.0f`), as in

```c++
float x_1[4]{70.7, 59.1, 47.5};
// [same for the others...]

```

Cheers,  
J.

---

<div class="post-metadata">

**Author:** ![gini](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gini/32/29467_2.png) [@gini](https://root-forum.cern.ch/u/gini)\
**Post date:** [July 11, 2023, 9:45am UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/11 "2023-07-11T09:45:14Z")

</div>

If i change my array size to 3, then I get the following error:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/2/8/2835e23be98c96c349b31e588a8b9d0e445d2b10.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:** [July 11, 2023, 10:36am UTC](https://root-forum.cern.ch/t/problem-in-plotting-multigraph-with-data-and-time-in-x-axis/55553/13 "2023-07-11T10:36:48Z")

</div>

```auto
void gini() {

   TDatime da1(2008,02,28,15,52,00);
   TDatime da2(2008,02,28,15,53,00);

   //---------------------------------- ---------------------------------------
   int n_3 = 2;
   float x_3[3], y_3[3], ex_3[3], ey_3[3] ;

   //double x_3[4] = {33.347, 32.859, 26.337, 23.311};
   x_3[0] = 77.245;
   x_3[1] = 79.245;
   y_3[0] = da1.Convert();
   y_3[1] = da2.Convert();

   ex_3[0] = 0;
   ex_3[1] = 0;
   ey_3[0] = 0;
   ey_3[1] = 0;

   auto gau_tri = new TGraphErrors(2,y_3,x_3,ey_3,ex_3);
   gau_tri->SetTitle("{}^{176}Os");
   gau_tri->SetMarkerColor(2);
   gau_tri->SetMarkerSize(1);
   gau_tri->SetMarkerStyle(8);
   gau_tri->SetLineColor(2);

   //---------------------------------- ---------------------------------------

   TDatime da5(2019,06,29,9,06,00);// RUN032
   TDatime da6(2019,06,29,11,06,00);// RUN034
   TDatime da7(2019,06,29,12,26,00);// RUN037

   int n_1 = 3;

   float y_1[3] = {70.7, 59.1, 47.5};
   float x_1[3] = {(float)da5.Convert(), (float)da6.Convert(), (float)da7.Convert()};

   float ex_1[3] = {0, 0, 0};
   float ey_1[3] = {0,0,0};

   auto gir = new TGraphErrors(n_1,x_1,y_1,ex_1,ey_1);
   gir->SetTitle("{}^{177}Ir");

   gir->SetMarkerSize(1.6);
   gir->SetMarkerStyle(71);

   //---------------------------------- Drawing ---------------------------------------
   TCanvas *c11 = new TCanvas("c11","c11_Paper",800,500);

   TMultiGraph *mg = new TMultiGraph();

   mg->Add(gau_tri, "C");
   mg->Add(gir,"P");

   mg->SetMinimum(30);
   mg->SetMaximum(80);
   mg->Draw("A");
   gPad->Update();

   mg->GetXaxis()->SetTimeDisplay(1);
   mg->GetXaxis()->SetNdivisions(-503);
   mg->GetXaxis()->SetTimeFormat("%Y-%m-%d %H:%M");
   mg->GetXaxis()->SetTimeOffset(0,"gmt");

}

```
