9.1. ROOT Plot Styles
Table of Contents
TStyle
ROOT controls most visual aspects of plots through the TStyle class. A TStyle object holds default settings for colors, fonts, margins, line widths, marker styles, statistics boxes, and many other drawing options. When you draw a histogram, graph, or function, ROOT consults the current style to decide how it should look, unless you override properties on the individual object.
The central idea is that you define a style once, then ROOT applies it automatically to all objects you draw under that style. This is much more efficient than changing the same properties on every histogram and canvas.
Each ROOT session always has an active style. By default this is gStyle, a global pointer to a TStyle object. You rarely create TStyle instances yourself at the beginning, instead you usually modify gStyle, or you load one of ROOT’s predefined styles and then adjust it.
A typical workflow is to configure gStyle at the start of your macro or session, for example:
{
gStyle->SetOptStat(0); // Disable statistics box
gStyle->SetTitleFont(42, "XYZ");
gStyle->SetLabelFont(42, "XYZ");
gStyle->SetPadLeftMargin(0.14);
gStyle->SetPadBottomMargin(0.12);
}Once this code has run, any new canvas and plot will follow these rules unless you change them again.
Many style settings are controlled through Set... methods of TStyle. Some of the most important groups of methods include:
| Aspect | Example methods | Typical use |
|---|---|---|
| Statistics box | SetOptStat, SetStatX, SetStatY | Show or hide statistics, place the box |
| Titles and fonts | SetTitleFont, SetLabelFont, SetTitleSize | Choose fonts and sizes for axes and titles |
| Margins and pads | SetPadLeftMargin, SetPadBottomMargin | Reserve space for axes and labels |
| Colors and fills | SetPalette, SetCanvasColor, SetFrameFillColor | Choose color palette and background |
| Lines and markers | SetLineWidth, SetMarkerStyle, SetMarkerSize | Global defaults for lines and markers |
| Log scales and grids | SetPadGridX, SetPadGridY | Enable grid lines |
Whenever you are unhappy with how a default ROOT plot looks, the first place to adjust it is almost always in a style configuration.
Important rule: Always configure your plotting style (using TStyle and gStyle) near the beginning of your macro or analysis script. This ensures that all plots in a session or analysis are consistent and reduces the need for repeated manual styling of individual objects.
If you want to define a completely custom named style, you can create your own TStyle object, configure it, and then call cd() on it to make it active:
{
TStyle *myStyle = new TStyle("myStyle", "My custom ROOT style");
myStyle->SetOptStat(0);
myStyle->SetFrameLineWidth(2);
myStyle->SetTitleFont(42, "XYZ");
myStyle->SetLabelSize(0.04, "XY");
myStyle->cd(); // Activate this style
}
After calling cd(), this style becomes the current one, and ROOT treats it similarly to gStyle for all subsequent drawing operations.
Global styles
Global styles define defaults that apply to all objects you draw after the style is set. In practice, this means you alter gStyle or activate a custom TStyle.
A global configuration is well suited when you want a consistent look for every figure in an analysis or publication. You set it once at the beginning of the macro, and the rest of the code focuses on physics rather than cosmetics.
There are two common ways to work with global styles.
First, directly modify gStyle:
void SetMyGlobalStyle() {
gStyle->SetOptStat(0); // No stats box by default
gStyle->SetOptTitle(0); // Hide histogram titles if desired
gStyle->SetCanvasColor(0); // White canvas
gStyle->SetPadColor(0);
gStyle->SetFrameLineWidth(2);
gStyle->SetLineWidth(2);
gStyle->SetHistLineWidth(2);
gStyle->SetTitleFont(42, "XYZ");
gStyle->SetLabelFont(42, "XYZ");
gStyle->SetTitleSize(0.05, "XYZ");
gStyle->SetLabelSize(0.04, "XYZ");
gStyle->SetPadLeftMargin(0.14);
gStyle->SetPadRightMargin(0.05);
gStyle->SetPadBottomMargin(0.13);
gStyle->SetPadTopMargin(0.06);
}
You would then call SetMyGlobalStyle(); once before creating canvases and histograms.
Second, create a named TStyle and activate it globally using its cd() method:
void UseMyStyle() {
static TStyle *myStyle = 0;
if (!myStyle) {
myStyle = new TStyle("myStyle", "My global style");
myStyle->SetOptStat(0);
myStyle->SetPadGridX(true);
myStyle->SetPadGridY(true);
myStyle->SetLegendBorderSize(0);
}
myStyle->cd(); // Make this the active global style
}This pattern is useful if you want to switch between different predefined styles in the same ROOT session.
ROOT also provides some utility functions and scripts containing styles that are common in experiments or publications. For example, you might encounter macros that define “ATLAS”, “CMS”, or “BELLE” style. These macros typically set gStyle or define a TStyle that is then selected with cd(). The principle is always the same: one global style drives the default appearance for all plots.
Global styles often control behavior you might not immediately think of as “style”. For example, gStyle->SetOptFit(1); makes ROOT automatically print fit results on the plot after using TH1::Fit or TGraph::Fit. gStyle->SetPalette(kViridis); changes the default color map for 2D histograms and surfaces. These changes apply to anything you draw after the style is set, which is convenient if you want all 2D plots to use the same palette.
When working with global styles, it is important to remember that style settings are stateful. If you run several macros in the same ROOT session, style changes from one macro will affect the next. For reproducible analysis, it is good practice to explicitly set the global style inside each macro or script so that it does not inherit an unknown style from a previous session.
Important rule: For reproducible and predictable plots, always assume that style state may be arbitrary when your macro starts. Reinitialize the global style (for example by calling gROOT->Reset() followed by your own style configuration, or by explicitly configuring gStyle) at the beginning of each plotting macro.
Global styles work best for:
- Setting font families and sizes consistently.
- Defining default margins and canvas background colors.
- Choosing common line widths and marker sizes.
- Controlling default statistics and fit info.
- Selecting a standard color palette for all 2D plots.
Once you have a reliable global style, your plots will look consistent, and you will only need to adjust details in special cases.
Object-specific styles
Sometimes you want a particular histogram or graph to look different from the global defaults. For example, you might want thicker lines for a reference distribution, different colors for several histograms drawn on the same axes, or special markers for data points compared to model predictions.
In these cases you override the global style by setting style properties directly on individual objects. Every drawable object in ROOT inherits from TAttLine, TAttFill, TAttMarker, or related attribute classes, which provide methods such as:
SetLineColor,SetLineStyle,SetLineWidthSetFillColor,SetFillStyleSetMarkerColor,SetMarkerStyle,SetMarkerSize
Histograms additionally have convenience methods like SetStats, SetTitle, and SetAxis* settings via their axes.
A simple example of object-specific styling for two histograms might be:
TH1F *h1 = new TH1F("h1", "h1", 100, 0, 10);
TH1F *h2 = new TH1F("h2", "h2", 100, 0, 10);
// Fill histograms here
// Apply object-specific styles
h1->SetLineColor(kRed);
h1->SetLineWidth(2);
h2->SetLineColor(kBlue);
h2->SetLineStyle(2); // Dashed
h2->SetLineWidth(2);
// Draw
h1->Draw(); // Draw first
h2->Draw("SAME"); // Overlay second
Although the global style might define default line colors, widths, and marker styles, these calls on h1 and h2 override those defaults only for these particular histograms.
Object-specific styling is also important when you create multiplot figures. Each dataset must stand out clearly from the others, for example by using different colors and markers. You might define a simple rule such as:
| Object type | Color | Marker style | Line style |
|---|---|---|---|
| Data | Black | 20 | Solid |
| Simulation | Red | 24 | Solid |
| Background | Blue | 25 | Dashed |
You then implement this rule with object-specific settings on each histogram or TGraph, while leaving the global style to handle fonts, margins, and other shared aspects.
Text objects and legends also support object-specific styles. For example:
TLatex *label = new TLatex(0.2, 0.85, "Preliminary");
label->SetNDC(); // Use normalized device coordinates
label->SetTextFont(42);
label->SetTextSize(0.05);
label->SetTextColor(kRed+1);
TLegend *leg = new TLegend(0.6, 0.7, 0.88, 0.88);
leg->SetBorderSize(0); // No border around legend
leg->SetFillStyle(0); // Transparent background
These properties only affect the specific TLatex and TLegend instances, regardless of the global style.
When mixing global and object-specific styles, ROOT applies them in a simple way: object-specific settings always win over the global defaults. The global style sets initial values for attributes, and then any object-specific calls modify them for that particular object.
This behavior suggests a clear strategy. Use the global style for the “baseline” look of your analysis, such as fonts, canvas colors, and margins. Then use object-specific styles sparingly for distinctions between individual datasets or annotations within a plot.
Important rule: Use global styles for consistency and object-specific styles only to highlight differences between datasets or to add special annotations. Object-specific settings override global defaults, so overusing them can make plots inconsistent and difficult to maintain.
If you find yourself repeatedly applying the same object-specific settings (for example, every “data” histogram always gets the same color and marker), it is often better to wrap that in a small function:
void StyleDataHistogram(TH1 *h) {
h->SetLineColor(kBlack);
h->SetMarkerColor(kBlack);
h->SetMarkerStyle(20);
h->SetLineWidth(2);
}
You can then call StyleDataHistogram(hData); on each relevant object. This keeps object-specific styling organized, avoids duplication, and keeps the rest of your analysis code focused on the physics.
Views: 0
Where to Move
Move chapter:
- ☰ 1. Introduction to ROOT
- ☰ 1.1. What Is ROOT?
- ☰ 1.2. Installing ROOT
- ☰ 1.3. Starting ROOT
- ☰ 1.4. Your First ROOT Session
-
- ☰ 2. C++ Basics for ROOT
- ☰ 2.1. Why ROOT Uses C++
- ☰ 2.2. Variables and Data Types
- ☰ 2.3. Operators
- ☰ 2.4. Conditional Statements
- ☰ 2.5. Loops
- ☰ 2.6. Functions
- ☰ 2.7. Arrays and Vectors
-
- ☰ 3. ROOT Macros
- ☰ 3.1. What Is a ROOT Macro?
- ☰ 3.2. Running ROOT Macros
- ☰ 3.3. Functions Inside ROOT Macros
- ☰ 3.4. Compiling ROOT Macros
- ☰ 3.5. Organizing a ROOT Project
-
- ☰ 4. ROOT Objects and the ROOT Object Model
- ☰ 4.1. Introduction to ROOT Classes
- ☰ 4.2. Object Names and Titles
- ☰ 4.3. ROOT Collections
- ☰ 4.4. Inspecting ROOT Objects
-
- ☰ 5. Histograms
- ☰ 5.1. Introduction to Histograms
- ☰ 5.2. Creating a 1D Histogram
- ☰ 5.3. Filling Histograms
- ☰ 5.4. Drawing Histograms
- ☰ 5.5. Histogram Properties
- ☰ 5.6. Accessing Histogram Bins
- ☰ 5.7. Histogram Errors
- ☰ 5.8. Normalizing Histograms
- ☰ 5.9. Combining Histograms
-
- ☰ 6. Two-Dimensional Histograms
- ☰ 6.1. Creating 2D Histograms
- ☰ 6.2. Filling 2D Histograms
- ☰ 6.3. Drawing 2D Histograms
- ☰ 6.4. Projections
- ☰ 6.5. Profiles
-
- ☰ 7. Graphs
- ☰ 7.1. Introduction to TGraph
- ☰ 7.2. Creating a TGraph
- ☰ 7.3. Graph Styling
- ☰ 7.4. TGraphErrors
- ☰ 7.5. TGraphAsymmErrors
- ☰ 7.6. MultiGraph
-
- ☰ 8. Canvases and Plotting
- ☰ 8.1. TCanvas
- ☰ 8.2. Dividing a Canvas
- ☰ 8.3. Drawing Multiple Objects
- ☰ 8.4. Legends
- ☰ 8.5. Text and Annotations
- ☰ 8.6. Logarithmic Axes
- ☰ 8.7. Saving Plots
-
- ☰ 9. Styling ROOT Plots
- ☰ 9.1. ROOT Plot Styles
- ☰ 9.2. Axis Configuration
- ☰ 9.3. Line Styles
- ☰ 9.4. Marker Styles
- ☰ 9.5. Publication-Quality Figures
-
- ☰ 10. Mathematical Functions
- ☰ 10.1. Introduction to TF1
- ☰ 10.2. Built-in Functions
- ☰ 10.3. Custom Functions
- ☰ 10.4. Evaluating Functions
- ☰ 10.5. Function Integrals
-
- ☰ 11. Curve Fitting
- ☰ 11.1. Introduction to Fitting
- ☰ 11.2. Gaussian Fits
- ☰ 11.3. Polynomial Fits
- ☰ 11.4. Custom Fit Functions
- ☰ 11.5. Fit Parameters
- ☰ 11.6. Goodness of Fit
- ☰ 11.7. Fit Residuals
- ☰ 11.8. Fitting Graphs
-
- ☰ 12. ROOT Files
- ☰ 12.1. Introduction to ROOT Files
- ☰ 12.2. Creating ROOT Files
- ☰ 12.3. Writing Objects
- ☰ 12.4. Reading ROOT Files
- ☰ 12.5. Exploring ROOT Files
- ☰ 12.6. Directories Inside ROOT Files
-
- ☰ 13. TTrees
- ☰ 13.1. Introduction to TTrees
- ☰ 13.2. Creating a TTree
- ☰ 13.3. Filling a TTree
- ☰ 13.4. Writing a TTree to a File
- ☰ 13.5. Reading a TTree
- ☰ 13.6. Inspecting TTrees
- ☰ 13.7. Drawing Data from TTrees
- ☰ 13.8. Applying Selection Cuts
- ☰ 13.9. Branch Addresses
-
- ☰ 14. Event-Based Data Analysis
- ☰ 14.1. Understanding Event Data
- ☰ 14.2. Event Loops
- ☰ 14.3. Event Selection
- ☰ 14.4. Derived Quantities
- ☰ 14.5. Analysis Workflow
-
- ☰ 15. Working with Real Experimental Data
- ☰ 15.1. Importing Text Data
- ☰ 15.2. Converting Data to ROOT
- ☰ 15.3. Data Cleaning
- ☰ 15.4. Data Calibration
- ☰ 15.5. Signal and Background
-
- ☰ 16. Random Numbers and Simulations
- ☰ 16.1. Random Number Generation
- ☰ 16.2. Common Distributions
- ☰ 16.3. Simple Monte Carlo Simulation
- ☰ 16.4. Detector Resolution Simulation
- ☰ 16.5. Efficiency Simulation
-
- ☰ 17. Statistical Analysis with ROOT
- ☰ 17.1. Basic Statistics
- ☰ 17.2. Probability Distributions
- ☰ 17.3. Statistical Uncertainties
- ☰ 17.4. Chi-Square Tests
- ☰ 17.5. Confidence Intervals
-
- ☰ 18. Modern ROOT with RDataFrame
- ☰ 18.1. Introduction to RDataFrame
- ☰ 18.2. Loading Data
- ☰ 18.3. Filtering Data
- ☰ 18.4. Defining New Columns
- ☰ 18.5. Creating Histograms
- ☰ 18.6. Statistical Operations
- ☰ 18.7. Saving Results
- ☰ 18.8. Multithreading
-
- ☰ 19. ROOT and Python
- ☰ 19.1. Introduction to PyROOT
- ☰ 19.2. ROOT Objects in Python
- ☰ 19.3. Reading ROOT Files with Python
- ☰ 19.4. PyROOT and NumPy
- ☰ 19.5. PyROOT and Matplotlib
- ☰ 19.6. Choosing Between C++ and Python
-
- ☰ 20. Advanced TTree Techniques
- ☰ 20.1. Trees with Multiple Branches
- ☰ 20.2. Storing std::vector Objects
- ☰ 20.3. TChain
- ☰ 20.4. Friends
- ☰ 20.5. Efficient TTree Analysis
-
- ☰ 21. ROOT for Particle and Nuclear Physics
- ☰ 21.1. Four-Vectors
- ☰ 21.2. Particle Kinematics
- ☰ 21.3. Invariant Mass
- ☰ 21.4. Angular Distributions
- ☰ 21.5. Detector Spectra
-
- ☰ 22. Debugging and Common ROOT Problems
- ☰ 22.1. ROOT Object Ownership
- ☰ 22.2. Null Pointers
- ☰ 22.3. Segmentation Faults
- ☰ 22.4. Missing Histograms
- ☰ 22.5. ROOT File Problems
- ☰ 22.6. TTree Branch Problems
- ☰ 22.7. Histogram Range Problems
- ☰ 22.8. Fit Failures
- ☰ 22.9. Debugging ROOT Macros
-
- ☰ 23. Writing Better ROOT Analysis Code
- ☰ 23.1. Separating Analysis and Plotting
- ☰ 23.2. Using Functions
- ☰ 23.3. Using Header Files
- ☰ 23.4. Naming Conventions
- ☰ 23.5. Avoiding Hard-Coded Values
- ☰ 23.6. Configuration Parameters
- ☰ 23.7. Reproducible Analysis
- ☰ 23.8. Version Control with Git
-
- ☰ 24. Final Project
- ☰ 24.1. Project Overview
- ☰ 24.2. Analyze a complete scientific dataset using ROOT.
- ☰ 24.3. Import the Data
- ☰ 24.4. Explore the Dataset
- ☰ 24.5. Apply Data Selection
- ☰ 24.6. Create Histograms
- ☰ 24.7. Fit the Data
- ☰ 24.8. Calculate Physical Quantities
- ☰ 24.9. Estimate Statistical Uncertainties
- ☰ 24.10. Create Publication-Quality Figures
- ☰ 24.11. Save the Analysis Results
- ☰ 24.12. Write a Short Analysis Report
-
- ☰ 25. Appendices
- ☰ 25.1. A. ROOT Command Cheat Sheet
- ☰ 25.2. B. ROOT Histogram Classes
- ☰ 25.3. C. ROOT Graph Classes
- ☰ 25.4. D. Common TTree Commands
- ☰ 25.5. E. Common Drawing Options
- ☰ 25.6. F. Common Fit Functions
- ☰ 25.7. G. ROOT C++ Cheat Sheet
- ☰ 25.8. H. PyROOT Cheat Sheet
- ☰ 25.9. I. Common ROOT Errors and Solutions
- ☰ 25.10. J. Recommended ROOT Analysis Workflow
-
Comments
Please login to add a comment.
Don't have an account? Register now!