How to create a custom plot with ROOT using specific axis limits and customized labels?

Hey guys… :innocent:

I am currently working on visualizing some data for a project. I have been able to create basic histograms and graphs but I am running into a bit of trouble when it comes to customizing my plots. Specifically… I had like to:

  • Set specific axis limits (for example, x-axis ranging from 0 to 100 and y-axis from 0 to 50).
  • Modify the labels for both axes to include custom text (e.g., instead of just “X” and “Y”, I want something like “Energy (MeV)” for the x-axis and “Events” for the y-axis).
  • Adjust the font size and style of these labels to make the plot look more polished for presentation purposes.

I have not found any I have looked through some of the documentation, but I am finding it a bit overwhelming, especially since I am still getting familiar with the basic syntax and structure of ROOT. Could anyone point me in the right direction for achieving these customizations? A short code snippet or an example would be incredibly helpful.

I am using ROOT 6.28 on Ubuntu 22.04, if that makes any difference.

Thanks in advance!

Respected community member! :blush:

Hello @ficov12960,

You can easily find more info having a look at ROOT tutorials, see e.g. this one, and the related documentation.
For what you want to do, an example is shown in the code snippet below.

auto c = new TCanvas("c","A Zoomed Graph",200,10,700,500);
int n = 10;
double x[10] = {-.22,.05,.25,.35,.5,.61,.7,.85,.89,.95};
double y[10] = {1,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,1};
auto gr = new TGraph(n,x,y);
gr->SetMarkerColor(4);
gr->SetMarkerStyle(20);
gr->GetXaxis()->SetRangeUser(0, 0.5);
gr->GetYaxis()->SetRangeUser(1, 8);
gr->SetTitle("Main Title");
gr->GetXaxis()->SetTitle("X axis");
gr->GetYaxis()->SetTitle("Y axis");
gr->GetYaxis()->SetTitleSize(80);
gr->GetXaxis()->SetTitleSize(80);
gr->Draw();
1 Like

Thanks @mdessole for the solution.

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