9.3. Line Styles
Table of Contents
Line width
Lines in ROOT are used for many objects, for example function curves, graph lines, histogram outlines, and axes. The apparent thickness of these lines is controlled by the line width. In ROOT, line width is an integer value, with 1 as the default.
For any drawable object that inherits line attributes, such as TH1, TGraph, TF1, or TLine, you control the thickness with the method SetLineWidth(Int_t width). The value is unitless but corresponds roughly to pixels on a typical screen. A width of 1 is thin, 2 or 3 is often suitable for presentations, and values above 4 usually produce very thick lines for emphasis or overlays.
For example, after creating a histogram h, you can write:
h->SetLineWidth(2);
h->Draw();or for a function:
TF1 *f = new TF1("f","gaus",0,10);
f->SetLineWidth(3);
f->Draw("same");
Rule: Use SetLineWidth(n) with small integers. A value of 1 is default, 2–3 is typical for clear plots. Very large values can dominate the figure and hide other information.
When preparing figures for publication or projection, it is often useful to increase line widths slightly compared to the defaults so that curves remain visible when printed or viewed from a distance. Try to keep line widths consistent across similar objects in the same figure, for example all data curves with the same width, and all model curves with another, to give a coherent visual style.
Line type
Line type controls the pattern of the line, such as solid, dashed, or dotted. In ROOT, the line style is set with SetLineStyle(Int_t style). The style code is an integer that ROOT maps to predefined dash patterns. By convention:
| Style code | Appearance (typical) |
|---|---|
| 1 | Solid line |
| 2 | Long dashes |
| 3 | Medium dashes |
| 4 | Short dashes |
| 5 | Dotted |
| 6 | Dash dot |
| 7 | Dash dot dot |
The exact visual appearance depends slightly on the output device and resolution, but the relative patterns remain useful for distinguishing different curves, especially in black and white prints.
To set the line style of an object, you can write:
g->SetLineStyle(2); // dashed
g->SetLineWidth(2); // combine with higher width
g->Draw("AL");or for a function:
f->SetLineStyle(7); // dash dot dot
f->Draw("same");
Rule: Use SetLineStyle(k) to distinguish multiple curves when color is unavailable, for example in grayscale printouts. Combine line style with line width for clear separation.
If you need custom dash patterns, ROOT allows you to define them through more advanced configuration of line styles, but as a beginner you will typically rely on the built in integer styles. When overlaying several predictions on top of the same data, it is good practice to choose different line styles and document them clearly in the legend so that the plot remains interpretable even when printed without color.
Line color
Color is the most direct way to distinguish different curves or components in a plot. ROOT handles colors through integer codes, and you set them with SetLineColor(Int_t color). There are predefined basic colors accessed through constants in the global namespace:
| Constant | Typical code | Color |
|---|---|---|
| kBlack | 1 | Black |
| kRed | 2 | Red |
| kGreen | 3 | Green |
| kBlue | 4 | Blue |
| kYellow | 5 | Yellow |
| kMagenta | 6 | Magenta |
| kCyan | 7 | Cyan |
| kOrange | 800 | Orange |
| kGray | 920 | Gray |
You can either use the numeric code directly or, more readably, use the corresponding constant. For example:
h->SetLineColor(kRed);
h->Draw();
f->SetLineColor(kBlue);
f->SetLineWidth(2);
f->Draw("same");
Rule: Always choose line colors that contrast well with the background and with each other. Use symbolic names like kRed instead of raw numbers to keep your code readable and consistent.
ROOT also provides darker and lighter variants of the main colors by adding offsets to the base colors. For example, kRed+1, kRed+2, and so on create a small color palette around red. This is especially useful if you want several related curves in similar hues but still distinguishable:
g1->SetLineColor(kBlue);
g2->SetLineColor(kBlue+2);
g3->SetLineColor(kBlue+4);For publication quality figures, it is important to choose colors that remain distinguishable for color blind readers and in grayscale prints. A common strategy is to combine color with line style and line width, for example a thick solid blue line for data and thinner dashed red and green lines for different models. By combining color, style, and width, you can keep plots clear under many different viewing conditions.
Views: 10
KAHIBARO