KAHIBARO
Discord Login Register

9.3. Line Styles

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:

cpp
h->SetLineWidth(2);
h->Draw();

or for a function:

cpp
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 codeAppearance (typical)
1Solid line
2Long dashes
3Medium dashes
4Short dashes
5Dotted
6Dash dot
7Dash 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:

cpp
g->SetLineStyle(2);    // dashed
g->SetLineWidth(2);    // combine with higher width
g->Draw("AL");

or for a function:

cpp
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:

ConstantTypical codeColor
kBlack1Black
kRed2Red
kGreen3Green
kBlue4Blue
kYellow5Yellow
kMagenta6Magenta
kCyan7Cyan
kOrange800Orange
kGray920Gray

You can either use the numeric code directly or, more readably, use the corresponding constant. For example:

cpp
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:

cpp
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

Comments

Please login to add a comment.

Don't have an account? Register now!