Register | Login | Visitors: 181 / 24 h

Fonts & Styles

Preparation

First, we create a simple ROOT script called styles.C with the function styles as we have done it before:
void styles()
{
    TCanvas *c1 = new TCanvas();

    TH1F *hist = new TH1F("hist", "Histogram", 100, -5, 5);
    hist->FillRandom("gaus");

    hist->Draw();
}
A simple histogram with 5000 entries following a Gaussian distribution.A simple histogram with 5000 entries following a Gaussian distribution.
This code simply creates a histogram and draws it inside a Canvas as shown in the figure. The function FillRandom("gaus") which is part of the TH1F class is an easy way to fill your histogram with some random data following a Gaussian distribution for testing your layout. The standard amount of entries is given as 5000, but you can adjust this value with an optional second argument. In the following case, only 100 entries will be created.
hist->FillRandom("gaus", 100)
Now we want to overwrite the defined title "Histogram" by writing
 hist->SetTitle("New Title")
And of course, you can also change the name from "hist" to any other name in the following way:
 hist->SetName("new_name")

Axis Titles

There are 2 ways to add titles to your histogram or graph axes. You can either get access to the x- and y-axis of the histogram and then use the function SetTitle() that takes a const char* as a parameter in the following way:
hist->GetXaxis()->SetTitle("X Values")
hist->GetYaxis()->SetTitle("Y Values")
Or you can define the axis titles directly during the construction of your histogram:
TH1F *hist = new TH1F("hist", "Histogram;X Values;Y values", 100, -5, 5);
If you want to get rid of the Stat-Box in the upper right corner, you can simply write:
hist->SetStats(0);

Font Sizes

If you think the font size of the labels or titles is too small, you can increase them by for example writing
hist->GetXaxis()->SetLabelSize(0.05);
hist->GetXaxis()->SetTitleSize(0.05);
The standard size is 0.035. You can also replace X with Y, in order to modify the label and title size for your y-axis. However, you have to keep in mind that in this case, the histogram might exceed the limits of your Canvas.

Canvas Margins

In this case, you can add a margin to different sides of the Canvas by writing:
c1->SetLeftMargin(0.15);
This value is basically a percentage. Hence, a value of 0.15 means that the left border occupies 15% of your Canvas width. The standard value for all margins is 0.1. You can replace Left with Bottom, Top, or Right, depending on which side you want to modify.

Line Styles & Colors

In the next step, we want to change the style and color of our histogram line. We can for instance use the following member functions:
hist->SetLineColor(kRed);
hist->SetLineWidth(2);
hist->SetLineStyle(kDashed);
hist->SetFillColor(kGreen);
The function SetLineColor() is used to change the color of line, whereas the line width can be varied using SetLineWidth(). The standard value here is 1. SetLineStyle is then used to change the style from the kSolid to kDashed which creates a dashed line. And at the end we use the function SetFillColor() to fill the area of our histogram below the line. Our code with all implemented changes will look now as follows:
void styles()
{
    TCanvas *c1 = new TCanvas();
    c1->SetLeftMargin(0.15);
    c1->SetBottomMargin(0.15);


    TH1F *hist = new TH1F("hist", "Histogram;X Values;Y Values", 100, -5, 5);

    hist->FillRandom("gaus");
    hist->SetStats(0);

    hist->GetXaxis()->SetLabelSize(0.05);
    hist->GetXaxis()->SetTitleSize(0.05);
    hist->GetYaxis()->SetLabelSize(0.05);
    hist->GetYaxis()->SetTitleSize(0.05);

    hist->SetLineColor(kRed);
    hist->SetLineWidth(2);
    hist->SetLineStyle(kDashed);
    hist->SetFillColor(kGreen);

    hist->Draw();
}
The final result after adjusting different colors and fonts.The final result after adjusting different colors and fonts.
This page contains 766 words and 4887 characters.