Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist005_TH1_palettecolor.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Palette coloring for TH1

Palette coloring for histogram is activated thanks to the options PFC (Palette Fill Color), PLC (Palette Line Color) and PMC (Palette Marker Color). When one of these options is given to TH1::Draw the histogram gets its color from the current color palette defined by gStyle->SetPalette(...). The color is determined according to the number of objects having palette coloring in the current pad.

In this example five histograms are displayed with palette coloring for lines and and marker. The histograms are drawn with markers and error bars and one can see the color of each histogram is picked inside the default palette kBird.

{
auto *canvas = new TCanvas();
// Disable drawing the title of the canvas
// Disable drawing the stats box
auto *h1 = new TH1D("h1", "Histogram drawn with full circles", 100, -4, 4);
auto *h2 = new TH1D("h2", "Histogram drawn with full squares", 100, -4, 4);
auto *h3 = new TH1D("h3", "Histogram drawn with full triangles up", 100, -4, 4);
auto *h4 = new TH1D("h4", "Histogram drawn with full triangles down", 100, -4, 4);
auto *h5 = new TH1D("h5", "Histogram drawn with empty circles", 100, -4, 4);
// Use Mersenne-Twister random number generator
for (int i = 0; i < 25000; i++) {
// "Rannor" fills the two parameters we pass with RANdom numbers picked from a NORmal distribution.
// In this case we ignore the second value.
double val, ignored;
rng.Rannor(val, ignored);
// Fill() called with 2 arguments adds the given value (first arg) with the specified weight (second arg)
h1->Fill(val, 10.);
h2->Fill(val, 8.);
h3->Fill(val, 6.);
h4->Fill(val, 4.);
h5->Fill(val, 2.);
}
// Set different styles for the various histograms
h2->SetMarkerStyle(kFullSquare);
h3->SetMarkerStyle(kFullTriangleUp);
h4->SetMarkerStyle(kFullTriangleDown);
h5->SetMarkerStyle(kOpenCircle);
// Draw all histograms overlapped in the same canvas (thanks to the "SAME" option)
h1->Draw("PLC PMC");
h2->Draw("SAME PLC PMC");
h3->Draw("SAME PLC PMC");
h4->Draw("SAME PLC PMC");
h5->Draw("SAME PLC PMC");
// Build a legend from the objects drawn in the pad, using their description that we specified when constructing them
gPad->BuildLegend();
}
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
@ kFullTriangleDown
Definition TAttMarker.h:59
@ kFullSquare
Definition TAttMarker.h:58
@ kFullTriangleUp
Definition TAttMarker.h:58
@ kFullCircle
Definition TAttMarker.h:58
@ kOpenCircle
Definition TAttMarker.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
#define gPad
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:41
The Canvas class.
Definition TCanvas.h:23
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:693
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3315
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3037
Random number generator class based on M.
Definition TRandom3.h:27
void SetOptTitle(Int_t tit=1)
Definition TStyle.h:338
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1642
TH1F * h1
Definition legend1.C:5
Date
November 2024
Author
Olivier Couet

Definition in file hist005_TH1_palettecolor.C.