4.11.6. plotting Package

4.11.6.1. plotting Package

4.11.6.2. canvas Module

This module implements python classes which inherit from and extend the functionality of the ROOT canvas classes.

class rootpy.plotting.canvas.Canvas(width=700, height=500, xpos=0, ypos=0, name=None, title=None)[source]

Bases: rootpy.core.Object, rootpy.plotting.canvas.PadMixin, TCanvas

class rootpy.plotting.canvas.Pad(*args, **kwargs)[source]

Bases: rootpy.core.Object, rootpy.plotting.canvas.PadMixin, TPad

class rootpy.plotting.canvas.PadMixin[source]

Bases: object

Clear(*args, **kwargs)[source]
OwnMembers()[source]
cd(*args)[source]

4.11.6.3. core Module

This module contains base classes defining core funcionality

class rootpy.plotting.core.Plottable[source]

Bases: object

This is a mixin to provide additional attributes for plottable classes and to override ROOT TAttXXX and Draw methods.

Draw(*args)[source]
EXTRA_ATTRS = {'integermode': False, 'inlegend': True, 'drawstyle': '', 'legendstyle': 'P', 'visible': True, 'norm': None}
EXTRA_ATTRS_DEPRECATED = {'intmode': 'integermode', 'format': 'drawstyle'}
GetColor()[source]
GetFillColor(mode=None)[source]

mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.

GetFillStyle(mode=None)[source]

mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.

GetLineColor(mode=None)[source]

mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.

GetLineStyle(mode=None)[source]

mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.

GetLineWidth()[source]
GetMarkerColor(mode=None)[source]

mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.

GetMarkerSize()[source]
GetMarkerStyle(mode=None)[source]

mode may be ‘root’, ‘mpl’, or None to return the ROOT, matplotlib, or input value.

SetColor(color)[source]

color may be any color understood by ROOT or matplotlib.

Set all color attributes with one method call.

For full documentation of accepted color arguments, see rootpy.plotting.style.Color.

SetFillColor(color)[source]

color may be any color understood by ROOT or matplotlib.

For full documentation of accepted color arguments, see rootpy.plotting.style.Color.

SetFillStyle(style)[source]

style may be any fill style understood by ROOT or matplotlib.

For full documentation of accepted style arguments, see rootpy.plotting.style.FillStyle.

SetLineColor(color)[source]

color may be any color understood by ROOT or matplotlib.

For full documentation of accepted color arguments, see rootpy.plotting.style.Color.

SetLineStyle(style)[source]

style may be any line style understood by ROOT or matplotlib.

For full documentation of accepted style arguments, see rootpy.plotting.style.LineStyle.

SetLineWidth(width)[source]
SetMarkerColor(color)[source]

color may be any color understood by ROOT or matplotlib.

For full documentation of accepted color arguments, see rootpy.plotting.style.Color.

SetMarkerSize(size)[source]
SetMarkerStyle(style)[source]

style may be any marker style understood by ROOT or matplotlib.

For full documentation of accepted style arguments, see rootpy.plotting.style.MarkerStyle.

color[source]
decorate(template_object=None, **kwargs)[source]
decorators[source]
fillcolor[source]
fillstyle[source]
linecolor[source]
linestyle[source]
linewidth[source]
markercolor[source]
markersize[source]
markerstyle[source]
rootpy.plotting.core.dim(hist)[source]

4.11.6.4. graph Module

class rootpy.plotting.graph.Graph(npoints=0, hist=None, efficiency=None, file=None, name=None, title=None, **kwargs)[source]

Bases: rootpy.plotting.core.Plottable, rootpy.core.NamelessConstructorObject, TGraphAsymmErrors

Crop(x1, x2, copy=False)[source]

Remove points which lie outside of [x1, x2]. If x1 and/or x2 is below/above the current lowest/highest x-coordinates, additional points are added to the graph using a linear interpolation

DIM = 1
GetMaximum(include_error=False)[source]
GetMinimum(include_error=False)[source]
Integrate()[source]

Integrate using the trapazoidal method

Invert(copy=False)[source]

Interchange the x and y coordinates of all points

Reverse(copy=False)[source]

Reverse the order of the points

Scale(value, copy=False)[source]

Scale the graph vertically by value

Shift(value, copy=False)[source]

Shift the graph left or right by value

Stretch(value, copy=False)[source]

Stretch the graph horizontally by a factor of value

static divide(left, right, consistency=True)[source]
maximum(include_error=False)[source]
minimum(include_error=False)[source]
setErrorsFromHist(hist)[source]
x(index=None)[source]
xMax()[source]
xMin()[source]
xedges()[source]
xedgesh(index=None)[source]
xedgesl(index=None)[source]
xerr(index=None)[source]
xerravg(index=None)[source]
xerrh(index=None)[source]
xerrl(index=None)[source]
y(index=None)[source]
yMax()[source]
yMin()[source]
yerr(index=None)[source]
yerravg(index=None)[source]
yerrh(index=None)[source]
yerrl(index=None)[source]
class rootpy.plotting.graph.Graph2D(npoints=0, hist=None, efficiency=None, file=None, name=None, title=None, **kwargs)[source]

Bases: rootpy.plotting.core.Plottable, rootpy.core.NamelessConstructorObject, TGraph2D

DIM = 2
x()[source]
y()[source]
z()[source]

4.11.6.5. hist Module

exception rootpy.plotting.hist.DomainError[source]

Bases: exceptions.Exception

class rootpy.plotting.hist.Efficiency(passed, total, name=None, title=None, **kwargs)[source]

Bases: rootpy.plotting.core.Plottable, rootpy.core.Object, TEfficiency

GetGraph()[source]
errors()[source]
painted_graph[source]

Returns the painted graph for a TEfficiency, or if it isn’t available, generates one on an invisible_canvas.

xaxis[source]
yaxis[source]
class rootpy.plotting.hist.Hist(*args, **kwargs)[source]

Bases: rootpy.plotting.hist._Hist

Returns a 1-dimensional Hist object which inherits from the associated ROOT.TH1* class (where * is C, S, I, F, or D depending on the type keyword argument)

class rootpy.plotting.hist.Hist2D(*args, **kwargs)[source]

Bases: rootpy.plotting.hist._Hist2D

Returns a 2-dimensional Hist object which inherits from the associated ROOT.TH1* class (where * is C, S, I, F, or D depending on the type keyword argument)

class rootpy.plotting.hist.Hist3D(*args, **kwargs)[source]

Bases: rootpy.plotting.hist._Hist3D

Returns a 3-dimensional Hist object which inherits from the associated ROOT.TH1* class (where * is C, S, I, F, or D depending on the type keyword argument)

class rootpy.plotting.hist.HistStack(name=None, title=None, **kwargs)[source]

Bases: rootpy.plotting.core.Plottable, rootpy.core.Object, THStack

Add(hist)[source]
Clone(newName=None)[source]
GetHists()[source]
GetMaximum(**kwargs)[source]
GetMinimum(**kwargs)[source]
Integral(start=None, end=None)[source]
Scale(value)[source]
lowerbound(axis=1)[source]
maximum(**kwargs)[source]
minimum(**kwargs)[source]
upperbound(axis=1)[source]
xaxis[source]
yaxis[source]
rootpy.plotting.hist.cls

alias of Hist3D

4.11.6.6. legend Module

class rootpy.plotting.legend.Legend(nentries, pad=None, leftmargin=0.5, topmargin=0.05, rightmargin=0.05, entryheight=0.06)[source]

Bases: rootpy.core.Object, TLegend

AddEntry(thing, legendstyle=None)[source]
Draw(*args, **kwargs)[source]
Height()[source]
Width()[source]

4.11.6.7. pyplot Module

will implement matplotlib.pyplot-like module here

rootpy.plotting.pyplot.figure(*args, **kwargs)[source]

4.11.6.8. root2matplotlib Module

4.11.6.9. shapes Module

class rootpy.plotting.shapes.Ellipse(*args, **kwargs)[source]

Bases: rootpy.plotting.core.Plottable, TEllipse

4.11.6.10. style Module

class rootpy.plotting.style.Color(color)[source]

Bases: rootpy.plotting.style._StyleContainer

Container for grouping together ROOT and matplotlib colors.

The color argument to the constructor can be a ROOT TColor or color index. If matplotlib is available, it can also accept an RGB or RGBA sequence, or a string in any of several forms:

  1. a letter from the set ‘rgbcmykw’
  2. a hex color string, like ‘#00FFFF’
  3. a standard name, like ‘aqua’
  4. a float, like ‘0.4’, indicating gray on a 0-1 scale

if color is RGBA, the A will simply be discarded.

Examples:

>>> color = Color(2)
>>> color()
2
>>> color('mpl')
(1.0, 0.0, 0.0)
>>> color = Color('blue')
>>> color('root')
4
>>> color('mpl')
(0.0, 0.0, 1.0)
>>> color = Color('0.25')
>>> color('mpl')
(0.25, 0.25, 0.25)
>>> color('root')
924
class rootpy.plotting.style.FillStyle(style)[source]

Bases: rootpy.plotting.style._StyleContainer

Container for grouping together ROOT and matplotlib fill styles.

The style argument to the constructor may be a ROOT fill style, a matplotlib fill style, or one of the following descriptions: ‘solid’ ‘hollow’

For an input value of ‘solid’, the matplotlib hatch value will be set to None, which is the same value as for ‘hollow’. The root2matplotlib functions will all check the ROOT value to see whether to make the fill solid or hollow.

Examples:

>>> style = FillStyle('hollow')
>>> style('root')
0
>>> print style('mpl')
None
class rootpy.plotting.style.LineStyle(style)[source]

Bases: rootpy.plotting.style._StyleContainer

Container for grouping together ROOT and matplotlib line styles.

The style argument to the constructor may be a ROOT line style, a matplotlib line style, or one of the following descriptions: ‘longdashdot’ ‘dashdot’ ‘dotted’ ‘longdashdotdotdot’ ‘solid’ ‘dashed’ ‘longdash’ ‘verylongdashdot’ ‘verylongdash’ ‘longdashdotdot’

Examples:

>>> style = LineStyle('verylongdashdot')
>>> style('root')
10
>>> style('mpl')
'dashdot'
class rootpy.plotting.style.MarkerStyle(style)[source]

Bases: rootpy.plotting.style._StyleContainer

Container for grouping together ROOT and matplotlib marker styles.

The style argument to the constructor may be a ROOT marker style, a matplotlib marker style, or one of the following descriptions: ‘square’ ‘triangle’ ‘largedot’ ‘opencircle’ ‘triangleup’ ‘opencross’ ‘opensquare’ ‘triangledown’ ‘cross’ ‘openstar’ ‘diamond’ ‘opendiamond’ ‘dot’ ‘mediumdot’ ‘opentriangle’ ‘fullstar’ ‘circle’ ‘star’ ‘smalldot’

Examples:

>>> style = MarkerStyle('opentriangle')
>>> style('root')
26
>>> style('mpl')
'^'
rootpy.plotting.style.convert_color(color, mode)[source]

Convert color to a TColor if mode=’root’ or to (r,g,b) if ‘mpl’.

The color argument can be a ROOT TColor or color index, an RGB or RGBA sequence or a string in any of several forms:

  1. a letter from the set ‘rgbcmykw’
  2. a hex color string, like ‘#00FFFF’
  3. a standard name, like ‘aqua’
  4. a float, like ‘0.4’, indicating gray on a 0-1 scale

if arg is RGBA, the transparency value will be ignored.

rootpy.plotting.style.convert_fillstyle(inputstyle, mode, inputmode=None)[source]

Convert inputstyle to ROOT or matplotlib format.

Output format is determined by mode (‘root’ or ‘mpl’). The inputstyle may be a ROOT fill style, a matplotlib hatch style, ‘hollow’, or ‘solid’.

rootpy.plotting.style.convert_linestyle(inputstyle, mode, inputmode=None)[source]

Convert inputstyle to ROOT or matplotlib format.

Output format is determined by mode (‘root’ or ‘mpl’). The inputstyle may be a ROOT line style, a matplotlib line style, or a description such as ‘solid’ or ‘dotted’.

rootpy.plotting.style.convert_markerstyle(inputstyle, mode, inputmode=None)[source]

Convert inputstyle to ROOT or matplotlib format.

Output format is determined by mode (‘root’ or ‘mpl’). The inputstyle may be a ROOT marker style, a matplotlib marker style, or a description such as ‘star’ or ‘square’.

4.11.6.11. views Module

4.11.6.11.1. Folder “View” Classes

These classes wrap Directories and perform automatic actions to Histograms retrieved from them. The different views can be composited and layered.

Summary of views:

  • ScaleView: scale histogram normalization
  • NormalizeView: normalize histograms
  • SumView: sum histograms from different folders together
  • StyleView: apply a style to histograms
  • StackView: build THStacks using histograms from different folders
  • TitleView: change the title of histograms
  • FunctorView: apply a arbitrary transformation function to the histograms
  • MultiFunctorView: apply a arbitrary transformation function to a collection of histograms
  • SubdirectoryView: A view of a subdirectory, which maintains the same view as the base.

4.11.6.11.1.1. Example use case

One has a ROOT file with the following content:

zjets/mutau_mass
zz/mutau_mass
wz/mutau_mass
data_2010/mutau_mass
data_2011/mutau_mass

and wants to do the following:

  1. Merge the two data taking periods together
  2. Scale the Z, WZ, and ZZ simulated results to the appropriate int. lumi.
  3. Combine WZ and ZZ into a single diboson sample
  4. Apply different colors to the MC samples
  5. Make a Stack of the expected yields from different simulated processes

This example can be tested by running:

python -m rootpy.plotting.views
>>> # Mock up the example test case
>>> import rootpy.io as io
>>> # We have to keep these, to make sure PyROOT doesn't garbage collect them
>>> keep = []
>>> zjets_dir = io.Directory('zjets', 'Zjets directory')
>>> zz_dir = io.Directory('zz', 'ZZ directory')
>>> wz_dir = io.Directory('wz', 'WZ directory')
>>> data2010_dir = io.Directory('data2010', 'data2010 directory')
>>> data2011_dir = io.Directory('data2011', 'data2011 directory')
>>> # Make the Zjets case
>>> _ = zjets_dir.cd()
>>> zjets_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> zjets_hist.FillRandom('gaus', 5000)
>>> keep.append(zjets_hist)
>>> # Make the ZZ case
>>> _ = zz_dir.cd()
>>> zz_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> zz_hist.FillRandom('gaus', 5000)
>>> keep.append(zz_hist)
>>> # Make the WZ case
>>> _ = wz_dir.cd()
>>> wz_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> wz_hist.FillRandom('gaus', 5000)
>>> keep.append(wz_hist)
>>> # Make the 2010 data case
>>> _ = data2010_dir.cd()
>>> data2010_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> data2010_hist.FillRandom('gaus', 30)
>>> keep.append(data2010_hist)
>>> # Make the 2011 data case
>>> _ = data2011_dir.cd()
>>> data2011_hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> data2011_hist.FillRandom('gaus', 51)
>>> keep.append(data2011_hist)
4.11.6.11.1.1.1. SumView

We can merge the two data periods into a single case using a SumView.

>>> data = SumView(data2010_dir, data2011_dir)
>>> data_hist = data.Get("mutau_mass")
>>> data_hist.Integral()
81.0
>>> data_hist.Integral() == data2010_hist.Integral() + data2011_hist.Integral()
True
4.11.6.11.1.1.2. ScaleView

The simulated results (Z & diboson) can be scaled to the expected integrated luminosity using ScaleViews.

>>> zjets = ScaleView(zjets_dir, 0.01)
>>> zjets_hist = zjets.Get("mutau_mass")
>>> abs(zjets_hist.Integral() - 50.0) < 1e-5
True
>>> # Scale the diboson contribution
>>> zz = ScaleView(zz_dir, 0.001)
>>> wz = ScaleView(wz_dir, 0.003)
4.11.6.11.1.1.3. Combining views

The dibosons individually are tiny, let’s put them together using a SumView. Note that this operation nests two ScaleViews into a SumView.

>>> dibosons = SumView(zz, wz)
>>> # We expect 5000*0.001 + 5000*0.003 = 20 events
>>> dibosons_hist = dibosons.Get("mutau_mass")
>>> abs(dibosons_hist.Integral() - 20) < 1e-4
True
4.11.6.11.1.1.4. StyleView

A style view automatically applies a style to retrieved Plottable objects. The style is specified using the same arguments as the Plottable.decorate. Let’s make the Z background red and the diboson background blue.

>>> zjets = StyleView(zjets, fillcolor=ROOT.EColor.kRed)
>>> dibosons = StyleView(dibosons, fillcolor=ROOT.EColor.kBlue)
>>> zjets_hist = zjets.Get("mutau_mass")
>>> zjets_hist.GetFillColor() == ROOT.EColor.kRed
True
>>> dibosons_hist = dibosons.Get("mutau_mass")
>>> dibosons_hist.GetFillColor() == ROOT.EColor.kBlue
True
4.11.6.11.1.1.5. StackView

The StackView combines multiple items into a HistStack. In our example we stack the SM backgrounds to compare to the data.

>>> sm_bkg = StackView(zjets, dibosons)
>>> sm_bkg_stack = sm_bkg.Get("mutau_mass")
>>> '%0.0f' % sm_bkg_stack.Integral()
'70'

Looks like we have an excess of 11 events - must be the Higgs.

4.11.6.11.1.2. Other Examples

4.11.6.11.1.2.1. NormalizeView

The normalization view renormalizes histograms to a given value (default 1.0). Here is an example of using the NormalizeView to compare the Z and diboson shapes.

>>> z_shape = NormalizeView(zjets)
>>> z_shape_hist = z_shape.Get("mutau_mass")
>>> abs(1 - z_shape_hist.Integral()) < 1e-5
True
>>> # Let's compare the shapes using a HistStack, using the "nostack" option.
>>> diboson_shape = NormalizeView(dibosons)
>>> shape_comparison = StackView(z_shape, diboson_shape)
>>> # To draw the comparison:
>>> # shape_comparison.Get("mutau_mass").Draw('nostack')
4.11.6.11.1.2.2. FunctorView

FunctorView allows you to apply an arbitrary transformation to the object. Here we show how you can change the axis range for all histograms in a directory.

>>> rebin = lambda x: x.Rebin(2)
>>> zjets_rebinned = FunctorView(zjets, rebin)
>>> zjets.Get("mutau_mass").GetNbinsX()
100
>>> zjets_rebinned.Get("mutau_mass").GetNbinsX()
50

The functor doesn’t have to return a histogram.

>>> mean_getter = lambda x: x.GetMean()
>>> mean = zjets.Get("mutau_mass").GetMean()
>>> zjets_mean = FunctorView(zjets, mean_getter)
>>> zjets_mean.Get("mutau_mass") == mean
True
4.11.6.11.1.2.3. MultiFunctorView

MultiFunctorView is similar except that it operates on a group of histograms. The functor should take one argument, a generator of the sub-objects.

Here’s an example to get the integral of the biggest histogram in a set:

>>> biggest_histo = lambda objects: max(y.Integral() for y in objects)
>>> biggest = MultiFunctorView(biggest_histo, zjets, dibosons)
>>> biggest.Get("mutau_mass") == zjets.Get("mutau_mass").Integral()
True
4.11.6.11.1.2.4. SubdirectoryView

If you’d like to “cd” into a lower subdirectory, while still maintaining the same view, use a SubdirectoryView.

>>> basedir = io.Directory('base', 'base directory')
>>> _ = basedir.cd()
>>> subdir1 = io.Directory('subdir1', 'subdir directory in 1')
>>> _ = subdir1.cd()
>>> hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> hist.FillRandom('gaus', 2000)
>>> keep.append(hist)
>>> _ = basedir.cd()
>>> subdir2 = io.Directory('subdir2', 'subdir directory 2')
>>> _ = subdir2.cd()
>>> hist = ROOT.TH1F("mutau_mass", "Mu-Tau mass", 100, 0, 100)
>>> hist.FillRandom('gaus', 5000)
>>> keep.append(hist)
The directory structure is now::
base/subdir1/hist base/subdir2/hist

Subdirectory views work on top of other views.

>>> baseview = ScaleView(basedir, 0.1)
>>> subdir1view = SubdirectoryView(baseview, 'subdir1')
>>> subdir2view = SubdirectoryView(baseview, 'subdir2')
>>> histo1 = subdir1view.Get('mutau_mass')
>>> histo2 = subdir2view.Get('mutau_mass')
>>> exp_histo1 = baseview.Get("subdir1/mutau_mass")
>>> exp_histo2 = baseview.Get("subdir2/mutau_mass")
>>> def equivalent(h1, h2):
...     return (abs(h1.GetMean() - h2.GetMean()) < 1e-4 and
...             abs(h1.GetRMS() - h2.GetRMS()) < 1e-4 and
...             abs(h1.Integral() - h2.Integral()) < 1e-4)
>>> equivalent(exp_histo1, histo1)
True
>>> equivalent(exp_histo2, histo2)
True
>>> equivalent(histo1, histo2)
False
class rootpy.plotting.views.FunctorView(directory, function)[source]

Bases: rootpy.plotting.views._FolderView

Apply an arbitrary function to the output histogram.

The histogram is always cloned before it is passed to the function.

apply_view(object)[source]
class rootpy.plotting.views.MultiFunctorView(f, *directories)[source]

Bases: rootpy.plotting.views._MultiFolderView

Apply an arbitrary function to the output histograms.

The function must take one argument, a generator of objects.

merge_views(objects)[source]
class rootpy.plotting.views.NormalizeView(directory, normalization=1.0)[source]

Bases: rootpy.plotting.views.ScaleView

Normalize histograms to a constant value

apply_view(object)[source]
class rootpy.plotting.views.PathModifierView(dir, path_modifier)[source]

Bases: rootpy.plotting.views._FolderView

Does some magic to the path

User should supply a functor which transforms the path argument passed to Get(...)

Get(path)[source]
apply_view(object)[source]

Do nothing

class rootpy.plotting.views.ScaleView(directory, scale_factor)[source]

Bases: rootpy.plotting.views._FolderView

View of a folder which applies a scaling factor to histograms.

apply_view(object)[source]
class rootpy.plotting.views.StackView(*directories, **kwargs)[source]

Bases: rootpy.plotting.views._MultiFolderView

Build a HistStack from the input histograms

The default draw option that histograms will use is “hist”.

One can override this for all histograms by passing a string. Individual behavior can be controlled by passing a list of draw options, corresponding to the input directories. In this case the option for all histograms must be specified.

The name and title of the HistStack is taken from the first histogram in the list.

Normally the histograms will be added to the stack in the order of the constructor. Optionally, one can add them in order of ascending integral by passing the kwarg sorted=True.

merge_views(objects)[source]
class rootpy.plotting.views.StyleView(directory, **kwargs)[source]

Bases: rootpy.plotting.views._FolderView

View of a folder which applies a style to Plottable objects.

The kwargs are passed to Plottable.decorate

apply_view(object)[source]
class rootpy.plotting.views.SubdirectoryView(dir, subdirpath)[source]

Bases: rootpy.plotting.views.PathModifierView

Add some base directories to the path of Get()

<subdir> is the directory you want to ‘cd’ too.

class rootpy.plotting.views.SumView(*directories)[source]

Bases: rootpy.plotting.views._MultiFolderView

Add a collection of histograms together

merge_views(objects)[source]
class rootpy.plotting.views.TitleView(directory, title)[source]

Bases: rootpy.plotting.views._FolderView

Override the title of gotten histograms

apply_view(object)[source]