HErmes.plotting package

HErmes.visual.canvases module

Provides canvases for multi axes plots

class HErmes.visual.canvases.YStackedCanvas(subplot_yheights=(0.2, 0.2, 0.5), padding=(0.15, 0.05, 0.0, 0.1), space_between_plots=0, figsize='auto', figure_factory=None)[source]

Bases: object

A canvas for plotting multiple axes on top of each other in Y-direction. So basically creates a several panel multiplot.

eliminate_lower_yticks()[source]

Eliminate the lowest y tick on each axes. The bottom axes keeps its lowest y-tick. This might be useful, since typically for stacked plots, the lowest y-tick overwrites the uppermost y-tick of the axis below.

global_legend(*args, **kwargs)[source]

A combined legend for all axes

Parameters:args will be passed to pylab.legend (all) –
Keyword Arguments:
 kwargs will be passed to pylab.legend (all) –
limit_xrange(xmin=None, xmax=None)[source]

Walk through all axes and set xlims

Keyword Arguments:
 
  • xmin (float) – left x edge of axes
  • xmax (float) – right x edge of axes
Returns:

None

limit_yrange(ymin=None, ymax=None)[source]

Walk through all axes and adjust ymin and ymax

Keyword Arguments:
 
  • ymin (float) – min ymin value which will be applied to all axes
  • ymin – max ymin value which will be applied to all axes
save(path, name, formats=('pdf', 'png'), **kwargs)[source]

Calls pylab.savefig for all endings

Parameters:
  • path (str) – path to savefile
  • name (str) – filename to save
  • formats (tuple) – for each name in endings, a file is save
Keyword Arguments:
 

keyword args will be passed to pylab.savefig (all) –

Returns:

The full path to the the saved file

Return type:

str

select_axes(axes)[source]

Set the scope on a certain axes

Parameters:axes (int) – 0 lowest, -1 highest, increasing y-order
Returns:The axes instance
Return type:matplotlib.axes.axes
show()[source]

Use the IPython.core.Image to show the plot

Returns:the plot
Return type:IPython.core.Image

HErmes.visual.plotting module

Define some

class HErmes.visual.plotting.VariableDistributionPlot(cuts=None, color_palette='dark', bins=None, xlabel=None)[source]

Bases: object

A plot which shows the distribution of a certain variable. Cuts can be indicated with lines and arrows. This class defines (and somehow enforces) a certain style.

add_cumul(name)[source]

Add a cumulative distribution to the plot

Parameters:name (str) – the name of the category
add_cuts(cut)[source]

Add a cut to the the plot which can be indicated by an arrow

Parameters:cuts (HErmes.selection.cuts.Cut) –
Returns:None
add_data(variable_data, name, bins=None, weights=None, label='')[source]

Histogram the added data and store internally

Parameters:
  • name (string) – the name of a category
  • variable_data (array) – the actual data
Keyword Arguments:
 
  • bins (array) – histogram binning
  • weights (array) – weights for the histogram
  • label (str) – A label for the data when plotted
add_legend(**kwargs)[source]

Add a legend to the plot. If no kwargs are passed, use some reasonable default.

Keyword Arguments:
 be passed to pylab.legend (will) –
add_ratio(nominator, denominator, total_ratio=None, total_ratio_errors=None, log=False, label='data/$\\Sigma$ bg')[source]

Add a ratio plot to the canvas

Parameters:
  • nominator (list or str) – name(s) of the categorie(s) which will be the nominator in the ratio
  • denominator (list or str) – name(s) of the categorie(s) which will be the nominator in the ratio
Keyword Arguments:
 
  • total_ratio (bool) – Indicate the total ratio with a line in the plot
  • total_ratio_errors (bool) – Draw error region around total ratio
  • log (bool) – draw ratio plot in log-scale
  • label (str) – y-label for the ratio plot
add_variable(category, variable_name, external_weights=None, transform=None)[source]

Convenience interface if data is sorted in categories already

Parameters:
  • category (HErmese.variables.category.Category) – Get variable from this category
  • variable_name (string) – The name of the variable
Keyword Arguments:
 
  • external_weights (np.ndarray) – Supply an array for weighting. This will OVERIDE ANY INTERNAL WEIGHTING MECHANISM and use the supplied weights.
  • transform (callable) – Apply transformation todata
indicate_cut(ax, arrow=True)[source]

If cuts are given, indicate them by lines

Parameters:ax (pylab.axes) – axes to draw on
static optimal_plotrange_histo(histograms)[source]

Get most suitable x and y limits for a bunc of histograms

Parameters:histograms (list(d.factory.hist1d)) – The histograms in question
Returns:xmin, xmax, ymin, ymax
Return type:tuple (float, float, float, float)
plot(axes_locator=((0, 'c', 0.2), (1, 'r', 0.2), (2, 'h', 0.5)), combined_distro=True, combined_ratio=True, combined_cumul=True, normalized=True, style='classic', log=True, legendwidth=1.5, ylabel='rate/bin [1/s]', figure_factory=None, zoomin=False, adjust_ticks=<function VariableDistributionPlot.<lambda>>)[source]

Create the plot

Keyword Arguments:
 
  • axes_locator (tuple) –

    A specialized tuple defining where the axes should be located in the plot tuple has the following form: ( (PLOTA), (PLOTB), …) where PLOTA is a tuple itself of the form (int, str, int) describing (plotnumber, plottype, height of the axes in the figure) plottype can be either: “c” - cumulative

    ”r” - ratio “h” - histogram
  • combined_distro
  • combined_ratio
  • combined_cumul
  • log (bool) –
  • style (str) – Apply a simple style to the plot. Options are “modern” or “classic”
  • normalized (bool) –
  • figure_factor (fcn) – Must return a matplotlib figure, use for custom formatting
  • zoomin (bool) – If True, select the yrange in a way that the interesting part of the histogram is shown. Caution is needed, since this might lead to an overinterpretation of fluctuations.
  • adjust_ticks (fcn) – A function, applied on a matplotlib axes which will set the proper axis ticks

Returns:

HErmes.visual.plotting.create_arrow(ax, x_0, y_0, dx, dy, length, width=0.1, shape='right', fc='k', ec='k', alpha=1.0, log=False)[source]

Create an arrow object for plots. This is typically a large arrow, which can used to indicate a region in the plot which is excluded by a cut.

Parameters:
  • ax (matplotlib.axes._subplots.AxesSubplot) – The axes where the arrow will be attached to
  • x_0 (float) – x-origin of the arrow
  • y_0 (float) – y-origin of the arrow
  • dx (float) – x length of the arrow
  • dy (float) – y length of the arrow
  • length (float) – additional scaling parameter to scale the length of the arrow
Keyword Arguments:
 
  • width (float) – thickness of arrow
  • shape (str) – either “full”, “left” or “right”
  • fc (str) – facecolor
  • ec (str) – edgecolor
  • alpha (float) – 0 -1 alpha value of the arrow
  • log (bool) – I for logscale, the proportions of the arrow will be adjusted accorginly.
Returns:

matplotlib.axes._subplots.AxesSubplot

HErmes.visual.plotting.gaussian_fwhm_fit(data, startparams=(0, 0.2, 1), fitrange=((None, None), (None, None), (None, None)), fig=None, bins=80, xlabel='$\\theta_{{rec}} - \\theta_{{true}}$')[source]

A plot with a gaussian fitted to data. A histogram of the data will be created and a gaussian will be fitted, with 68 and 95 percentiles indicated in the plot. The gaussian will be in a form so that the fwhm can be read directly from it. The “width” parameter of the gaussian is NOT the standard deviation, but FWHM!

Parameters:

data (array-like) – input data with a (preferably) gaussian distribution

Keyword Arguments:
 
  • startparams (tuple) – a set of startparams of the gaussian fit. It is a 3 parameter fit with mu, fwhm and amplitude
  • fitrange (tuple) – if desired, the fit can be restrained. One tuple of (min, max) per parameter
  • fig (matplotlib.Figure) – pre-created figure to draw the plot in
  • bins (array-like or int) – bins for the underliying histogram
  • xlabel (str) – label for the x-axes
HErmes.visual.plotting.gaussian_model_fit(data, startparams=(0, 0.2), fitrange=((None, None), (None, None)), fig=None, norm=True, bins=80, xlabel='$\\theta_{{rec}} - \\theta_{{true}}$')[source]

A plot with a gaussian fitted to data. A histogram of the data will be created and a gaussian will be fitted, with 68 and 95 percentiles indicated in the plot.

Parameters:

data (array-like) – input data with a (preferably) gaussian distribution

Keyword Arguments:
 
  • startparams (tuple) – a set of startparams of the gaussian fit. If only mu/sigma are given, then the plot will be normalized
  • fig (matplotlib.Figure) – pre-created figure to draw the plot in
  • bins (array-like or int) – bins for the underliying histogram
  • fitrange (tuple(min, max) – min-max range for the gaussian fit
  • xlabel (str) – label for the x-axes
HErmes.visual.plotting.line_plot(quantities, bins=None, xlabel='', add_ratio=None, ratiolabel='', colors=None, figure_factory=None)[source]
Parameters:

quantities

Keyword Arguments:
 
  • bins
  • xlabel
  • add_ratio (tuple) – ([“data1”],[“data2”])
  • ratiolabel (str) –
  • colors
  • figure_factory (callable) – Factory function returning matplotolib.Figure

Returns:

HErmes.visual.plotting.meshgrid(xs, ys)[source]

Create x and y data for matplotlib pcolormesh and similar plotting functions.

Parameters:
  • xs (np.ndarray) – 1d x bins
  • ys (np.ndarray) – 2d y bins
Returns:

2d X and 2d Y matrices as well as a placeholder for the Z array

Return type:

tuple (np.ndarray, np.ndarray, np.ndarray)

Module contents

A set of