plot utils
A collection of useful basic functions for plotting.
make_legend_opaque
full signature:
def make_legend_opaque( leg )
comments:
set the transparency of all entries in a legend to zero
add_text
full signature:
def add_text( ax, text, pos, fontsize=10, horizontalalignment='left', verticalalignment='bottom', background_facecolor=None, background_alpha=None, background_edgecolor=None, **kwargs )
comments:
add text to an axis at a specified position (in relative figure coordinates)
input arguments:
- ax: matplotlib axis object
- text: string, can contain latex syntax such as /textbf{} and /textit{}
- pos: tuple with relative x- and y-axis coordinates of bottom left corner
add_cms_label
full signature:
def add_cms_label( ax, pos=(0.1,0.9), extratext=None, **kwargs )
comments:
add the CMS label and extra text (e.g. 'Preliminary') to a plot
special case of add_text, for convenience
make_text_latex_safe
full signature:
def make_text_latex_safe( text )
comments:
make a string safe to process with matplotlib's latex parser in case no tex parsing is wanted
(e.g. escape underscores)
to be extended when the need arises!
plot_hists
full signature:
def plot_hists(histlist, fig=None, ax=None, colorlist=[], labellist=[], transparency=1, xlims=(-0.5,-1), title=None, titlesize=None, xaxtitle=None, xaxtitlesize=None, yaxtitle=None, yaxtitlesize=None, ymaxfactor=None, legendsize=None, opaque_legend=False, ticksize=None, bkgcolor=None, bkgcmap='spring', bkgrange=None, bkgtitle=None)
comments:
plot some histograms (in histlist) in one figure using specified colors and/or labels
- histlist is a list of 1D arrays containing the histograms (or a 2D array of shape (nhistograms,nbins))
- colorlist is a list or array containing colors (in string format), of length nhistograms
note: it can also be a single string representing a color (in pyplot), then all histograms will take this color
- labellist is a list or array containing labels for in legend, of length nhistograms
- xlims is a tuple of min and max for the x-axis labels, defaults to (-0.5,nbins-0.5)
- title, xaxtitle, yaxtitle: strings for histogram title, x-axis title and y-axis title respectively
- bkgcolor: 1D array representing background color for the plot
(color axis will be scaled between min and max in bkgcolor)
note: if bkgcolor does not have the same length as the x-axis, it will be compressed or stretched to fit the axis,
but this might be meaningless, depending on what you are trying to visualize!
- bkgmap: name of valid pyplot color map for plotting the background color
output: tuple of figure and axis objects, that can be used to further tune the look of the figure or save it
plot_hists_from_df
full signature:
def plot_hists_from_df(df, histtype, nhists)
comments:
plot a number of histograms in a dataframe
- df is the dataframe from which to plot
- histtype is the name of the histogram type (e.g. 'chargeInner_PXLayer_1')
- nhists is the number of histograms to plot
plot_hists_multi
full signature:
def plot_hists_multi(histlist, fig=None, ax=None, colorlist=[], labellist=[], transparency=1, xlims=(-0.5,-1), title=None, titlesize=None, xaxtitle=None, xaxtitlesize=None, yaxtitle=None, yaxtitlesize=None, caxtitle=None, caxtitlesize=None, caxtitleoffset=None, remove_underflow=False, remove_overflow=False, ylims=None, ymaxfactor=None, legendsize=None, opaque_legend=False, ticksize=None )
comments:
plot many histograms (in histlist) in one figure using specified colors and/or labels
- histlist is a list of 1D arrays containing the histograms (or a 2D array of shape (nhistograms,nbins))
- colorlist is a list or array containing numbers to be mapped to colors
- labellist is a list or array containing labels for in legend
output: tuple of figure and axis objects, that can be used to further tune the look of the figure or save it
plot_sets
full signature:
def plot_sets(setlist, fig=None, ax=None, colorlist=[], labellist=[], transparencylist=[], title=None, titlesize=None, xaxtitle=None, xaxtitlesize=None, xlims=(-0.5,-1), remove_underflow=False, remove_overflow=False, yaxtitle=None, yaxtitlesize=None, ylims=None, ymaxfactor=None, legendsize=None, opaque_legend=False, ticksize=None)
comments:
plot multiple sets of 1D histograms to compare the shapes
- setlist is a list of 2D numpy arrays containing histograms
- fig and ax: a pyplot figure and axis object (if one of both is none a new figure is created)
- title is a string that will be used as the title for the ax object
other parameters are lists of which each element applies to one list of histograms
plot_anomalous
full signature:
def plot_anomalous(histlist, ls, highlight=-1, hrange=-1)
comments:
plot a range of 1D histograms and highlight one of them
input arguments:
- histlist and ls: a list of histograms and corresponding lumisection numbers
- highlight: the lumisection number of the histogram to highlight
- hrange: the number of histograms before and after lsnumber to plot (default: whole run)
plot_hist_2d
full signature:
def plot_hist_2d(hist, fig=None, ax=None, title=None, titlesize=None, xaxtitle=None, xaxtitlesize=None, yaxtitle=None, yaxtitlesize=None, ticklabelsize=None, colorticklabelsize=None, extent=None, caxrange=None)
comments:
plot a 2D histogram
- hist is a 2D numpy array of shape (nxbins, nybins)
notes:
- if the histogram contains only nonnegative values, values below 1e-12 will not be plotted
(i.e. they will be shown as white spots in the plot) to discriminate zero from small but nonzero
- if the histogram contains negative values, the color axis will be symmetrized around zero
plot_hists_2d
full signature:
def plot_hists_2d(hists, ncols=4, axsize=5, title=None, titlesize=None, subtitles=None, subtitlesize=None, xaxtitles=None, yaxtitles=None, **kwargs)
comments:
plot multiple 2D histograms next to each other
input arguments
- hists: list of 2D numpy arrays of shape (nxbins,nybins), or an equivalent 3D numpy array
- ncols: number of columns to use
- figsize: approximate size of a single axis in the figure
(will be modified by aspect ratio)
- title, titlesize: properties of the super title for the entire figure
- subtitles, subtitlesize: properties of the individual histogram titles
- xaxtitles, yaxtitles: properties of axis titles of individual histograms
- kwargs: passed down to plot_hist_2d
plot_hists_2d_gif
full signature:
def plot_hists_2d_gif(hists, titles=None, xaxtitle=None, yaxtitle=None, duration=0.3, figname='temp_gif.gif')
comments:
(no valid documentation found)
plot_moments
full signature:
def plot_moments(moments, ls, dims=(0,1), fig=None, ax=None, markersize=10)
comments:
plot the moments of a set of histograms
input arguments:
- moments: a numpy array of shape (nhists,nmoments)
- dims: a tuple of two or three values between 0 and nmoments-1
plot_distance
full signature:
def plot_distance(dists, ls=None, rmlargest=0., doplot=True, title=None, xaxtitle='lumisection number', yaxtitle='distance metric')
comments:
(no valid documentation found)
plot_loss
full signature:
def plot_loss(data, loss_key='loss', val_loss_key='val_loss', title=None, titlesize=None, xaxtitle='Epoch', xaxtitlesize=None, yaxtitle='Loss', yaxtitlesize=None, xlims=None, yaxlog=True, legendsize=None, legendloc='best', doshow=True)
comments:
plot the training and validation loss of a keras/tensorflow model
input arguments:
- data: the object returned by the .fit method when called upon a keras model
- other: plot layout options
plot_mse
full signature:
def plot_mse(mse, rmlargest=0., doplot=True, title=None, xaxtitle='lumisection number', yaxtitle='mse')
comments:
plot the mse's and return the mean and std
input args:
- mse is a 1D numpy array of mse scores
- doplot: boolean whether to make a plot or simply return mean and std
- rmlargest: fraction of largest mse's to remove (to avoid being too sensitive to outliers)
plot_score_dist
full signature:
def plot_score_dist( scores, labels, fig=None, ax=None, nbins=20, normalize=False, siglabel='Signal', sigcolor='g', bcklabel='Background', bckcolor='r', title=None, titlesize=12, xaxtitle=None, xaxtitlesize=12, yaxtitle=None, yaxtitlesize=12, legendsize=None, legendloc='best', ticksize=None, doshow=True)
comments:
make a plot showing the distributions of the output scores for signal and background
plot_score_dist_multi
full signature:
def plot_score_dist_multi( scores, labels=None, colors=None, fig=None, ax=None, nbins=20, normalize=False, linestyle=None, linewidth=1, title=None, titlesize=12, xaxtitle=None, xaxtitlesize=12, yaxtitle=None, yaxtitlesize=12, legendsize=None, legendloc='best', ticksize=None, dolegend=True )
comments:
plot the distribution of output scores for arbitrarily many sets (not limited to 'signal' and 'background')
input arguments:
- scores: list of numpy arrays of scores
- labels: list of legend entries for the scores, must have same length as scores or be None (no legend)
- colors: list of colors for the different score arrays, must have same length as scores or be None (default colors)
plot_score_ls
full signature:
def plot_score_ls( thisscore, refscores, fig=None, ax=None, thislabel='This LS', thiscolor='black', reflabel='Reference LS', refcolor='dodgerblue', **kwargs )
comments:
make a plot of the score for a single lumisection comparing to some reference distribution
plot_metric
full signature:
def plot_metric( wprange, metric, label=None, color=None, sig_eff=None, sig_label=None, sig_color=None, bck_eff=None, bck_label=None, bck_color=None, title=None, xaxtitle='working point', yaxlog=False, ymaxfactor=1.3, yaxtitle='metric' )
comments:
plot a metric based on signal and background efficiencies.
along with the metric, the actual signal and background efficiencies can be plotted as well.
input arguments:
- wprange, metric: equally long 1D-numpy arrays, x- and y-data respectively
- label: label for the metric to put in the legend
- color: color for the metric (default: blue)
- sig_eff: 1D-numpy array of signal efficiencies corresponding to wprange
- sig_label: label for sig_eff in the legend
- color: color for sig_eff (default: green)
- bck_eff, bck_label, bck_color: same as for signal
- title, xaxtitle and yaxtitle: titles for the plot and axes
- yaxlog: boolean whether to put y axis in log scale
- ymaxfactor: factor to add extra space on the top of the plot (for the legend)
plot_roc
full signature:
def plot_roc( sig_eff, bkg_eff, auc=None, sig_eff_unc=None, color='b', title=None, titlesize=None, xaxtitle='Background efficiency', xaxtitlesize=None, yaxtitle='Signal efficiency', yaxtitlesize=None, xaxlog=True, yaxlog=False, xlims='auto', ylims='auto', dogrid=True, ticksize=None, doshow=True )
comments:
note: automatic determination of xlims and ylims assumes log scale for x-axis and lin scale for y-axis;
might not work properly in other cases and ranges should be provided manually.
plot_confusion_matrix
full signature:
def plot_confusion_matrix( tp, tn, fp, fn, true_positive_label='Good', true_negative_label='Anomalous', pred_positive_label='Predicted good', pred_negative_label='Predicted anomalous', xaxlabelsize=None, yaxlabelsize=None, textsize=None, colormap='Blues', colortitle=None )
comments:
(no valid documentation found)
color
full signature:
def color(value)
comments:
(no valid documentation found)
valstr
full signature:
def valstr(value)
comments:
(no valid documentation found)
clip_scores
full signature:
def clip_scores( scores )
comments:
clip +-inf values in scores
local copy of the same functions in autoencoder_utils.py
(need to copy here locally to use in plot_fit_2d and plot_fit_1d without circular import...)
plot_fit_2d
full signature:
def plot_fit_2d( points, fitfunc=None, figsize=(10,8), logprob=False, clipprob=False, onlycontour=False, xlims=5, ylims=5, onlypositive=False, xaxtitle=None, xaxtitlesize=None, yaxtitle=None, yaxtitlesize=None, title=None, titlesize=None, caxtitle=None, caxtitlesize=None, caxrange=None, transparency=1, ticksize=None )
comments:
make a 2D scatter plot of a point cloud with fitted contour
input arguments:
- points: a numpy array of shape (npoints,ndims), where ndims is supposed to be 2
- fitfunc: an object of type CloudFitter (see src/cloudfitters)
or any other object that implements a pdf(points) method
- logprob: boolean whether to plot log probability or normal probability
- clipprob: boolean whether to replace +- inf values by (non-inf) max and min
- onlycontour: a boolean whether to draw only the fit or include the data points
- xlims and ylims: tuples of (low,high)
note: can be an integer, in which case the range will be determined automatically
from the formula low = mean-xlims*std, high = mean+xlims*std,
where mean and std are determined from the points array.
- onlypositive: overrides previous argument to set lower bound of plotting range at 0 in both dimensions.
- xaxtitle and yaxtitle: titles for axes.
plot_fit_2d_clusters
full signature:
def plot_fit_2d_clusters( points, clusters, figsize=(12,8), labels=None, colors=None, legendmarkerscale=1., legendsize=10, legendloc='best', legendbbox=None, **kwargs )
comments:
make a 2D scatter plot of a fitted contour with point clouds superimposed
input arguments:
- points: numpy arrays of shape (npoints,ndims), where ndims is supposed to be 2,
usually the points to which the fit was done
note: only used to determine plotting range, these points are not plotted!
- clusters: list of numpy arrays of shape (npoints,ndims), where ndims is supposed to be 2,
clouds of points to plot
- labels: list with legend entries (must be same length as clusters)
- colors: list with colors (must be same length as clusters)
- kwargs: passed down to plot_fit_2d
note: onlycontour is set automatically and should not be in kwargs
plot_fit_1d
full signature:
def plot_fit_1d( points, fitfunc=None, logprob=False, clipprob=False, onlycontour=False, xlims=5, onlypositive=False, xaxtitle=None, xaxtitlesize=None, yaxtitle=None, yaxtitlesize=None, title=None, titlesize=None )
comments:
make a 1D scatter plot of a point cloud with fitted contour
input arguments:
- points: a numpy array of shape (npoints,ndims), where ndims is supposed to be 1
- fitfunc: an object of type CloudFitter (see src/cloudfitters)
or any other object that implements a pdf(points) method
- logprob: boolean whether to plot log probability or normal probability
- clipprob: boolean whether to replace +- inf values by (non-inf) max and min
- onlycontour: a boolean whether to draw only the fit or include the data points
- xlims: tuple of the form (low,high)
note: can be an integer, in which case the range will be determined automatically
from the formula low = mean-xlims*std, high = mean+xlims*std,
where mean and std are determined from the points array.
- onlypositive: set lower bound of plotting range at 0 (overrides xlims)
- xaxtitle and yaxtitle: titles for axes.
plot_fit_1d_clusters
full signature:
def plot_fit_1d_clusters( points, clusters, labels=None, colors=None, **kwargs )
comments:
make a 1D scatter plot of a fitted contour with point clouds superimposed
input arguments:
- points: numpy arrays of shape (npoints,ndims), where ndims is supposed to be 1,
usually the points to which the fit was done
note: only used to determine plotting range, these points are not plotted!
- clusters: list of numpy arrays of shape (npoints,ndims), where ndims is supposed to be 1,
clouds of points to plot
- labels: list with legend entries (must be same length as clusters)
- colors: list with colors (must be same length as clusters)
- kwargs: passed down to plot_fit_1d
note: onlycontour is set automatically and should not be in kwargs