9.2. Axis Configuration
Table of Contents
Axis titles
Every plot in ROOT should have clear, informative axis titles. Axis titles tell the reader what quantity is plotted and in which units. ROOT gives you full control over these strings and their appearance.
For most 1D objects you configure axis titles through the object’s axis. A very common pattern is to use the histogram’s helper:
h->SetTitle("Energy spectrum;Energy [MeV];Counts");This single string contains three parts separated by semicolons: the overall plot title, the X axis title, and the Y axis title. This is quick for simple plots, but for fine control you will usually work directly with the axes:
h->GetXaxis()->SetTitle("Energy [MeV]");
h->GetYaxis()->SetTitle("Events / bin");For graphs and functions the syntax is analogous:
g->GetXaxis()->SetTitle("Time [s]");
g->GetYaxis()->SetTitle("Position [cm]");
ROOT does not enforce any convention on units or formatting, so it is your responsibility to choose meaningful and consistent titles. It is good practice to always include units in square brackets and to use a readable style, for example p_{T} [GeV/c] for transverse momentum.
You can also change the font, size, and color of axis titles. Common methods on TAxis include
axis->SetTitleFont(42); // a standard sans serif font
axis->SetTitleSize(0.05); // relative to pad size
axis->SetTitleColor(kBlack);These settings let you match axis titles to the rest of the figure style, especially when preparing publication quality plots.
Always give axes clear titles with units, using consistent notation across all plots in an analysis.
Labels
Axis labels are the numbers or text marks that appear along the axes to indicate positions or categories. ROOT distinguishes between numeric tick labels and user defined textual labels.
For numeric axes, the default labels are automatic. You can control their appearance using:
axis->SetLabelFont(42);
axis->SetLabelSize(0.04);
axis->SetLabelColor(kBlack);
axis->SetLabelOffset(0.005); // distance from axis line
SetLabelSize uses a value relative to the pad size, not in pixels. If labels look too small when exporting to PDF, increase this value slightly.
For categorical or sparse data you may want to use text labels instead of numbers, for example for histogram bins that represent detector modules or particle types. In 1D histograms you can assign labels to bins:
h->GetXaxis()->SetBinLabel(1, "Module A");
h->GetXaxis()->SetBinLabel(2, "Module B");
h->GetXaxis()->SetBinLabel(3, "Module C");
ROOT will then draw these strings under the bins instead of numeric coordinates. In this case you often need to increase SetLabelSize and maybe rotate the labels using:
h->GetXaxis()->LabelsOption("v"); // vertical labelsor
h->GetXaxis()->LabelsOption("h"); // horizontal labels, the defaultYou can also control the number of numeric labels and their format. For example, to reduce clutter on a logarithmic axis or when values are very large or very small, you can use:
axis->SetNdivisions(510); // main and secondary tick divisions
axis->SetNoExponent(kTRUE); // avoid scientific notation on axis
axis->SetMoreLogLabels(kTRUE); // better label placement on log axesThese options give you direct control over readability. If labels overlap or look crowded, adjust the number of divisions and label size until the axis is clear.
Do not let labels overlap or become unreadable. Adjust label size, spacing, or number of divisions to keep axes clear.
Title offsets
Title offset controls how far the axis title is placed from the axis and its labels. If the offset is too small, the title overlaps with tick labels. If it is too large, the title appears visually detached from the axis.
You change title offsets through the axis:
axis->SetTitleOffset(1.2);The numeric value is relative and dimensionless. Typical values depend on the axis direction and canvas aspect ratio.
For most rectangular plots a useful starting point is:
h->GetXaxis()->SetTitleOffset(1.1);
h->GetYaxis()->SetTitleOffset(1.4);The Y axis usually needs a larger offset than the X axis because the title is drawn vertically and must clear the numeric labels to remain legible.
If you change the pad margins, especially the left margin, you will often need to retune the Y axis title offset to keep titles inside the visible area and avoid collisions with the frame. For example, after increasing the left margin:
gPad->SetLeftMargin(0.15);
h->GetYaxis()->SetTitleOffset(1.8);For 2D histograms and plots with a Z axis the same principle applies:
h2->GetZaxis()->SetTitleOffset(1.2);Adjusting the Z axis offset is important if you have a color bar with a title, so the title does not overlap with numeric scale labels.
If you change pad margins or text sizes, revisit SetTitleOffset on each axis so titles remain readable and properly aligned.
Axis ranges
Axis range configuration controls which part of your data is visible and how it is displayed. ROOT chooses an automatic range by default, but for analysis and presentation you often want explicit control.
The simplest way to set a numeric range is with SetRangeUser on the axis:
h->GetXaxis()->SetRangeUser(0.0, 10.0);
h->GetYaxis()->SetRangeUser(0.0, 100.0);This keeps the full histogram data in memory but shows only the selected interval. For graphs the pattern is the same:
g->GetXaxis()->SetLimits(0.0, 10.0); // for TGraph
Ranging via object methods like hist->GetXaxis()->SetRangeUser is part of styling and does not modify the underlying bin contents.
You can also zoom interactively in the ROOT canvas by dragging with the mouse. When you have found a good range interactively, you can read it back and hard code it in a macro for reproducibility.
On logarithmic axes, the range has an additional constraint. The visible part of the axis must stay strictly positive, since log(0) and log of negative values are undefined. Make sure your lower bound is greater than zero, for example:
gPad->SetLogy();
h->GetYaxis()->SetRangeUser(1e-1, 1e4);If you set a lower bound of zero on a log axis, ROOT will ignore that value and may emit warnings. Always check that minimal positive values are used when combining log scaling with custom ranges.
To focus on a specific subset of data, you can use ranges directly on the X axis while still keeping all Y values. For histograms:
int binLow = h->GetXaxis()->FindBin(2.0);
int binHigh = h->GetXaxis()->FindBin(5.0);
h->GetXaxis()->SetRange(binLow, binHigh);This selects a range by bin index rather than numeric value. It is useful when you know bin indices from previous calculations.
For clarity in multi plot figures, keep axis ranges consistent across related plots. If you compare two histograms, give them the same X and Y ranges so visual differences represent physics, not changing scales.
Never rely only on automatic ranges when comparing plots. Explicitly set consistent axis ranges so visual comparisons are meaningful.
Views: 13
KAHIBARO