Plotting ROOT objects with matplotlib

The following code block defines some useful methods to plot ROOT objects.

from mpl_tools import bind

For the detailed instructions refer to the matplotlib documentation and reference to relevant functions (plot, bar, etc).

TH1

hist1.plot(...)

plot TH1* using pyplot.plot(x, y, ...) function (x,y are overridden)

hist1.plot(autolabel=True, ...)

same as above, but guess label from histogram’s title

hist1.errorbar(autolabel=True, ...)

plot TH1* using pyplot.errorbar(x, y, yerr, xerr, ...)

hist1.bar(autolabel=True, ...)

plot TH1* using pyplot.bar(x, y, yerr, xerr, ...)

hist1a.bar(divide=3, shift=0, ...)
hist1b.bar(divide=3, shift=1, ...)
hist1c.bar(divide=3, shift=2, ...)

plot three TH1* using pyplot.bar(). The width of the bins will be times shorter and the bins will be displaced and not overlapped

autolabel may be applied to all above commands.

TH2

hist2.pcolorfast(...)

plot TH2* ax.pcolorfast() (fastest, each axis should have equal width bins)

hist2.pcolorfast(colorbar=True,...)

same as above, adds a colorbar

hist2.pcolorfast(mask=0.0,...)

same as the first command. Do not colorize bins with value=0.0 (ROOT behaviour)

hist2.pcolormesh(...)

plot TH2* ax.pcolormesh() (slower than pcolorfast)

hist2.imshow(...)

plot TH2* ax.imshow()

colorbar and mask options may be applied to all above commands.

TGraph

graph.plot(...)

plot TGraph/TGraphErrors/TGraphAsymmErrors via plt.plot(), no errors

graph.errorbar(...)

plot TGraphErrors/TGraphAsymmErrors via plt.errorbar()