Kahibaro
Login Register

Plotting Data

Plotting ASCII Data

Sometimes we already have data stored in a text (ASCII) file that we want to visualize in ROOT. In this section, we will learn how to read and plot such data using the TGraph class.

Let us assume we have a text file called data.txt with two columns representing the $x$ and $y$ values:

-4 16
-3 9
-2 4
-1 1
0 0
1 1
2 4
3 9
4 16

Each line contains one pair of numbers separated by a space or tab.

Creating a Graph

To visualize the data, we create a file called data.C and write the following code into it:

void data()
{
  TGraph *graph = new TGraph("data.txt");
  graph->Draw("ALP");
}

Here, we use the class TGraph, which is specifically designed to plot data points.

The string "ALP" inside the Draw() method defines the drawing options:

Running the code with root data.C produces a simple graph with points connected by lines, as shown below:

Graph loaded from ASCII data.

The resulting plot shows a parabolic shape symmetric around the origin.

Adding Titles and Labels

We can make the plot more readable by adding a title and axis labels. This can be done by setting the title directly in the TGraph constructor and customizing the axes:

void data()
{
  TGraph *graph = new TGraph("data.txt");
  graph->SetTitle("Parabola;X values;Y values");
  graph->Draw("ALP");
}

The first part of the title ("Parabola") appears on top of the graph, while the text after the first and second semicolons sets the $x$ and $y$ axis titles.

Styling the Graph

As explained in the previous chapter, we can also style the graph to make it look more appealing:

graph->SetMarkerStyle(21);
graph->SetMarkerSize(1.2);
graph->SetMarkerColor(kRed);
graph->SetLineColor(kBlue);

The result can be found in the figure below

Styed TGraph for plotting data file.

Full Code

Here you can find the full code to plot the data in the text file, including the creation of a dedicated canvas:

void data()
{
  TCanvas *c1 = new TCanvas("c1", "Data Plot", 800, 600);
  TGraph *graph = new TGraph("data.txt");
  graph->SetTitle("Parabola;X values;Y values");
  graph->SetMarkerStyle(21);
  graph->SetMarkerSize(1.2);
  graph->SetMarkerColor(kRed);
  graph->SetLineColor(kBlue);
  graph->Draw("ALP");
}

This version opens a dedicated canvas window with a custom size, draws the axes, connects the data points with a blue line, and marks each point with a red dot.

Plotting Data with Errors

If your ASCII file has four columns: $x$, $y$, $x$-error, and $y$-error

-4 16 0.1 0.2
-3 9 0.1 0.2
-2 4 0.1 0.2
-1 1 0.1 0.2
0 0 0.1 0.2
1 1 0.1 0.2
2 4 0.1 0.2
3 9 0.1 0.2
4 16 0.1 0.2

you can use TGraphErrors instead of TGraph:

void data()
{
  TCanvas *c1 = new TCanvas("c1", "Data with Errors", 800, 600);
  TGraphErrors *graph = new TGraphErrors("data_errors.txt");
  graph->SetTitle("Measurement with Errors;X values;Y values");
  graph->SetMarkerStyle(20);
  graph->SetMarkerColor(kBlue);
  graph->Draw("AP");
}

This automatically adds error bars to each data point, as shown below:

Graph with error bars.

Views: 15

Comments

Please login to add a comment.

Don't have an account? Register now!