8.6. Logarithmic Axes
Table of Contents
Log X
Logarithmic axes are useful when your data spans several orders of magnitude in one direction. In ROOT you usually control log axes through the current pad, which is often a TCanvas or a subdivided pad.
To activate a logarithmic scale on the horizontal axis for the current pad, you call:
gPad->SetLogx(1);The argument 1 means log scale is enabled. To return to a linear scale, use:
gPad->SetLogx(0);
You typically set the log state before or immediately after drawing an object. If you already have a canvas pointer, for example TCanvas *c1, you can write:
c1->cd();
c1->SetLogx();
hist->Draw();
c1->Update();
The default argument is 1, so SetLogx() and SetLogx(1) are equivalent.
On a log X axis, ROOT draws tick marks at powers of 10 by default and uses labels like 0.1, 1, 10, 100, and so on. Any data values that are less than or equal to zero cannot be shown on a log axis. They are simply invisible and do not appear on the plot. This can be confusing if your histogram or graph includes non positive values.
On a logarithmic X axis, all X values must be strictly positive. Any values with $x \le 0$ are not displayed.
When you plan to use log X, it is important that the X range of your object excludes non positive values. For histograms, the X range is defined by the bin edges, and for graphs it is defined by the actual X values of the points. If needed, restrict the drawn range with GetXaxis()->SetRangeUser(xmin, xmax) so that the minimum is greater than zero. If you zoom interactively or programmatically into a region that would start at or below zero, the log axis will not be meaningful, and ROOT may warn you or simply not draw what you expect.
Log X is particularly helpful for spectra, distributions that extend from very low to very high energies or momenta, or any quantity that varies as a power law in X. In such cases, power law behavior appears as approximately straight lines in a log X representation, which can make trends much easier to see.
If your plot looks empty after turning on log X, check that your data actually has positive X values within the visible range, and that you did not accidentally move the axes into a region where the minimum is zero or negative.
Log Y
A logarithmic Y axis is widely used to visualize distributions with a wide dynamic range, such as histograms with a long tail, or background distributions that stretch over many orders of magnitude.
To enable a logarithmic Y scale on the current pad, you call:
gPad->SetLogy(1);To switch back to a linear Y axis, use:
gPad->SetLogy(0);As with log X, you usually call this on a specific canvas or pad that you have already selected:
TCanvas *c1 = new TCanvas("c1","logy example",800,600);
c1->SetLogy();
hist->Draw();
c1->Update();ROOT draws tick marks at powers of 10 vertically, and labels like $10^{-1}$, $10^{0}$, $10^{1}$ and so on, depending on the range of your data.
The same restriction applies here to the Y values of what you draw. For a histogram, the Y values are the bin contents. On a log Y axis, any bin with content less than or equal to zero is not shown. This has two important consequences. First, empty bins, with content exactly zero, are invisible. Second, if you have negative bin contents, for example after subtracting histograms, those bins also disappear when log Y is enabled.
On a logarithmic Y axis, all Y values must be strictly positive. Bins or points with $y \le 0$ cannot be displayed.
If you are plotting a histogram where many bins are empty, a log Y scale can still be very useful, but you must remember that the empty bins will not appear. The non empty bins will form a continuous curve or stepped line, and the zeros will be gaps between those bins. For graphs such as TGraph or TGraphErrors, any point with $y \le 0$ will not be drawn when log Y is active.
For histograms, ROOT automatically chooses a default minimum that is greater than zero when you turn on log Y, typically a small fraction of the maximum bin content. However, if you want explicit control, you can set the minimum and maximum by hand:
hist->SetMinimum(1e-2); // for example
hist->SetMaximum(1e4);
gPad->SetLogy();
hist->Draw();This is especially important if your histogram includes statistical errors and very small counts. Setting a meaningful positive minimum avoids situations where the choice of scale hides relevant structures at low values.
Log Y is extremely common in particle and nuclear physics when visualizing energy spectra, momentum distributions, or any quantity where the relative differences over several decades matter more than the absolute linear differences. It allows you to see small signals or backgrounds next to large peaks on the same plot without saturating the scale.
If your histogram suddenly looks like a few isolated bins when you turn on log Y, inspect the bin contents and possibly adjust the minimum, or consider whether negative or zero values are present due to previous manipulations.
Log Z
Logarithmic Z axes are used for two dimensional histograms and other color mapped plots where the color represents a third quantity. In ROOT, the Z axis corresponds to the bin content or function value, and appears as a color scale legend when you draw a 2D histogram with options like "COLZ".
To enable a logarithmic Z scale on the current pad, you call:
gPad->SetLogz(1);To return to a linear color scale:
gPad->SetLogz(0);A typical sequence for a 2D histogram might look like this:
TCanvas *c1 = new TCanvas("c1","logz example",800,600);
c1->SetLogz();
h2->Draw("COLZ");
c1->Update();With log Z enabled, the color scale is spaced according to powers of 10, and regions that would otherwise be saturated at the maximum can show fine structure in the lower populated areas. This is especially useful when some bins have extremely high occupancy while a broad region has modest counts.
As with X and Y, the Z values must be strictly positive to appear on a log scale. For histograms, the Z values are again the bin contents. If a bin has content zero or less than zero, it will not be represented on the log Z color scale. Instead, it will appear as the background color, or be indistinguishable from an empty region.
On a logarithmic Z axis, all Z values must be strictly positive. Bins with $z \le 0$ are not visible on the color map.
To get a useful color range, it is often necessary to set the minimum Z value explicitly before drawing:
h2->SetMinimum(1e-3); // any small positive value appropriate for your data
h2->SetMaximum(1e3);
gPad->SetLogz();
h2->Draw("COLZ");The minimum controls the lowest color level that appears. Any bin with content below that minimum but still greater than zero will be drawn with the lowest color. Any bin with content less than or equal to zero will still not be visible at all on a log Z axis.
Log Z plots help to reveal rare structures, such as tails or off diagonal features, that would otherwise be lost when a few very large bins dominate the linear color scale. For example, in a two dimensional mass versus momentum histogram, a dense cluster near the origin might mask a weaker correlation at higher values if the Z axis is linear. Using log Z can make both the dense core and the sparse outer region visible in the same plot.
When you use log Z, always check the numeric range of the color bar after drawing. If all your interesting bins are compressed into a tiny part of the bar, adjust the SetMinimum and SetMaximum values and redraw until the features of interest are clearly visible.
Views: 11
KAHIBARO