6.3. Drawing 2D Histograms
Table of Contents
COL
The simplest way to view a two dimensional histogram in ROOT is with a color map. For a TH2F or TH2D that you have already created and filled, you call the Draw method and pass a drawing option string. The option "COL" tells ROOT to represent each bin with a colored box whose color encodes the bin content. For example:
h2->Draw("COL");Here h2 is a pointer to a TH2 object, such as TH2F. The x axis and y axis represent the two variables you filled, and each bin is drawn as a rectangle whose color increases or decreases with the bin value. This gives you a quick visual impression of where the density of entries is high or low.
The "COL" option is useful when you want to see fine structures because it uses the full binning of the histogram. There is no interpolation between bins, so you see exactly the bin grid that you defined when you created the histogram. If the binning is coarse, the plot will look blocky. If the binning is fine, you get a smooth looking color pattern.
You can combine "COL" with other options, for instance:
h2->Draw("COL TEXT");This draws colored boxes and overlays the numerical bin contents as text in each bin. For two dimensional histograms with many bins, this is usually only readable in restricted zoom regions. To zoom, you can use the mouse in the ROOT canvas or set axis ranges in code before drawing.
If you call Draw without any option, ROOT chooses a default that often looks similar to "COL". It is usually better to specify the option explicitly so that your plots are reproducible and your code is clear.
When using color plots, remember that axis labels and titles work exactly as for one dimensional histograms. You set them with methods such as:
h2->GetXaxis()->SetTitle("x variable");
h2->GetYaxis()->SetTitle("y variable");Then you redraw with "COL". The axes are drawn only on the first Draw call, so if you draw with a new option, you usually call Draw again on a fresh canvas or pad.
COLZ
The "COLZ" option extends "COL" by adding a color palette scale on the side of the plot. You use it like:
h2->Draw("COLZ");You get the same colored boxes for each bin, and in addition a separate vertical bar, usually to the right of the plot, that shows how colors map to numerical values of the bin contents. This color scale is extremely useful when you want to read or communicate approximate values from the plot.
The Z in "COLZ" refers to the third quantity of a two dimensional histogram, the bin content, sometimes called the z value. In a TH2, x and y are the two coordinates, and the bin content is the value associated with each (x, y) bin. "COLZ" makes that explicit by drawing the z scale.
You can control the number of colors and the palette by changing the global style, for example:
gStyle->SetPalette(kViridis); // or another palette index
h2->Draw("COLZ");Different palettes can make structures more or less visible and can improve readability for printed figures. Once you set a palette in gStyle, it affects subsequent COL and COLZ plots in that ROOT session.
The color scale also has its own axis. You can modify its formatting using methods from the underlying TGaxis, but for many analyses the default formatting is sufficient. If you adjust the histogram minimum and maximum with:
h2->SetMinimum(0.0);
h2->SetMaximum(100.0);the color scale is updated to reflect these limits on the next Draw with "COLZ". This is a common way to fix the color range so that different plots can be compared.
In interactive work, "COLZ" is the default choice for two dimensional histograms when you care about quantitative interpretation. It is particularly useful when comparing different regions of your phase space, such as high density clusters versus sparse areas.
Important: Use "COLZ" when you need both a color map and a visible numeric scale for bin contents. Control the color range with SetMinimum and SetMaximum to make plots from different histograms comparable.
LEGO
The "LEGO" option draws a two dimensional histogram as a three dimensional bar plot. Each bin becomes a block that rises above the x-y plane with a height proportional to the bin content. You call:
h2->Draw("LEGO");By default ROOT creates a three dimensional view where you can change the perspective by dragging with the mouse in the canvas. This makes it easy to see relative heights of bins and to understand peak structures at a glance.
The LEGO option has several variants that control the style of the blocks. Some common ones are:
h2->Draw("LEGO"); // default 3D bars
h2->Draw("LEGO1"); // flat shaded surfaces
h2->Draw("LEGO2"); // bars with different shadingThese are mostly visual differences. The underlying data is identical in all cases, only the rendering changes. A LEGO plot can be more visually striking than a simple color map, but it can also obscure details if the grid is dense, because bars can hide other bars behind them.
A LEGO view is also sensitive to the choice of viewing angle. A perspective that highlights one structure might hide another. You can rotate the view interactively or control it through TPad and TView methods in code if you need a consistent angle for publication.
Because LEGO plots use height as well as color, they can be helpful for teaching or for quick qualitative presentations. However, for precise analysis or for compact summary figures, a COLZ plot is often more practical and easier to read.
Guideline: Use "LEGO" for qualitative, three dimensional visual intuition about your two dimensional distribution, not for precise quantitative comparison of many histograms.
SURF
The "SURF" option draws the histogram as a continuous three dimensional surface over the x-y plane. Instead of separate bars, ROOT connects neighboring bin centers and creates a surface whose height follows the bin contents. You can use it with:
h2->Draw("SURF");The result is a smooth looking landscape that can reveal ridges, valleys, and peaks in your two dimensional data more clearly than a bar plot. As with LEGO, you can rotate the view interactively to inspect the shape from different angles.
There are several variants of SURF that control shading and color:
h2->Draw("SURF1");
h2->Draw("SURF2");
h2->Draw("SURF3");
h2->Draw("SURF4");These change how the surface is lit and colored. For example, some variants use only lines on the surface grid, others use filled colored surfaces, and some combine both. You can experiment interactively to see which representation shows your structure best.
A SURF plot is especially helpful when the binning is fine and the underlying distribution is smooth. In that case the surface gives a clear sense of the topology of your distribution. If the statistics are low and the histogram is noisy, the surface will look jagged and might be misleading, because our eyes tend to interpret surfaces as smooth functions.
As with other two dimensional representations, SURF uses the same histogram object. You can switch between different views simply by calling Draw with a new option on the same TH2, ideally on a fresh canvas or after clearing the pad. For example, you can inspect the same data first with "COLZ" and then with "SURF" to get both numerical and shape perspectives.
Rule of thumb: Use "SURF" when you want to emphasize the shape of the two dimensional distribution as a smooth landscape. Always cross check with a color map such as "COLZ" to avoid misinterpreting noise as structure.
Views: 12
KAHIBARO