Graph Tools

pydiana.tools.graph_tools.add_summary_legend(figin, df, x=0.8, y=0.05, xref='x domain', yref='y domain', align='left', bordercolor='white', borderwidth=0.7, borderpad=4, bgcolor='#ffe994', opacity=1, showlegend=True, showplot=False, printchi2=True, **kwargs)[source]

Function for adding a colored square in the plot with the result of the fit parameters. Everything in df[:-6] will be added to the plot summary legend.

Parameters:
  • figin (plotly figure to which to add the annotation) --

  • df (dataframe with fit results in the style of the one made by extract fit parameters) --

  • showplot (bool for enabling plot showing) --

  • printchi2 (bool for enabling reduced chi2 writing on plot) --

  • function. (rest of the arguments are the ones for the plotly add_annotation()) --

Returns:

Plotly figure with the summary legend

pydiana.tools.graph_tools.compute_bins(xrange, binwidth=None, bins=None, logbins=False, events_number=None)[source]

Given a xrange and binwidth/nbins, this function gives bins array to be used in make_hist NB: if both binwidth and nbins are None (or nbins <=1) and logbins is False, then is taken into account events_number setting nbins = np.ceil(np.sqrt(events_number)). If event events_number is None when it's needed, the function exit with an error.

Parameters:
  • (array-like) (xrange) --

  • float) (binwidth (int or) --

  • array-like) (bins (int or) --

  • (bool) (logbins) --

  • (int) (events_number) --

Returns:

bins (array-like) : bins array of the future histogram from make_hist. It returns n+1 numbers where "n" is the number of bins, since all the edges of the bins are returned (numpy compatibility)

pydiana.tools.graph_tools.hex2rgba(color, opacity=1)[source]

Function for converting a HEX color in RGB and setting it's opacity

pydiana.tools.graph_tools.hist_RDataFrame(df, xvar, bins)[source]

Make the histogram using ROOT DataFrame and return the counts of the histogram to be plotted later on.

Parameters:
  • df (ROOT dataframe containing data) --

  • (str) (xvar) --

  • (array-like) (bins) --

Returns:

counts (array-like)

Return type:

array (N elements) containing the counts of the constructed TH1 histogram

pydiana.tools.graph_tools.make_fit_heatmap(df=None, y=None, x=None, func=None, bins=[100, 100], binwidths=[None, None], ylab=None, xlab=None, title='', logx=False, logy=False, logz=False, xrange=None, yrange=None, showplot=True, sidepannels=False, summary=False, location_x=0.7, location_y=0.7, fit_options='QSLB', colorscale=None, cl=None, logbinsx=False, logbinsy=False, get_detail_matrices=False)[source]

Function for making a scatterplot.

Parameters:
  • y (array with the y variable) --

  • x (array with the x variable) --

  • func (function to fit) --

  • ylab (string with the label of the y-axis) --

  • xlab (string with the label of the x-axis) --

  • title (string with the title of the plot) --

  • logx (bool for toggling logscale on x) --

  • logy (bool for toggling logscale on y) --

  • plot (string indicating the plotting library) --

  • xrange (range of x axis) --

  • yrange (range of y axis) --

  • showplot (bool for toggling showing of the plot) --

Return type:

pandas dataframe with fit results and figure instance

pydiana.tools.graph_tools.make_fit_scatter(x, y, func, df=None, err_x=None, err_y=None, xlab='', ylab='', title='', logx=False, logy=False, showplot=True, points2skip=None, residuals=True, summary=True, sort=False, fit_options='QSB', get_detail_matrices=False, legendpos_x=0.8, legendpos_y=0.2, fitcolor='#FF7F0E', printchi2=True, unit_x=None, unit_y=None, xrange=None, cl=None, skip_fitlabel=False)[source]

Function for easily making fits of scatter plots

Parameters:
  • x (array containing the x coordinates of the data. If df is provided then x can be the string of the df column (if the unit is written in square brackets then it is automatically read)) --

  • y (array containing the y coordinates of the data. If df is provided then y can be the string of the df column (if the unit is written in square brackets then it is automatically read)) --

  • func (root function to be fitted) --

  • df (dataframe with the data to fit. If not provided then one can provide arrays to x and y) --

  • err_x (array containing the errors on the x coordinates. If df is provided then err_x can be the string of the df column) --

  • err_y (array containing the errors on the y coordinates. If df is provided then err_y can be the string of the df column) --

  • xlab (string with the x axis label) --

  • ylab (string with the y axis label) --

  • showplot (boolean to enable plot showing) --

  • points2skip (list with the indexes of which points need to be skipped for the fit (is sort=True in order of x)) --

  • residuals (bool for enabling the construnction of the residuals plot) --

  • summary (bool for enabling the plotting of the summary legend) --

  • fit_options (string with ROOT's fitting options) --

  • matrices (get_detailed) --

  • legendpos_x (int specifying the x-position of the summary legend (from 0 to 1)) --

  • legendpos_y (int specifying the y-position of the summary legend (from 0 to 1)) --

  • cl (float between 0<cl<1 with confidence interval of fit to plot. If None it will not be plotted.) --

Returns:

  • return [fit_results,figure]

  • if get_detailed_matrices -- return [fit_results,matrices,figure]

pydiana.tools.graph_tools.make_heatmap(df=None, y=None, x=None, bins=[None, None], binwidths=[None, None], ylab=None, xlab=None, title='', logx=False, logy=False, logz=False, xrange=None, yrange=None, showplot=True, sidepannels=False, colorscale=None, logbinsx=False, logbinsy=False, zrange=None)[source]

Function for making an heatmap. (NOT COMPATIBLE WITH ROOT DATAFRAME, in progress)

Parameters:
  • y (array with the y variable) --

  • x (array with the x variable) --

  • ylab (string with the label of the y-axis) --

  • xlab (string with the label of the x-axis) --

  • title (string with the title of the plot) --

  • logx (bool for toggling logscale on x) --

  • logy (bool for toggling logscale on y) --

  • plot (string indicating the plotting library) --

  • xrange (range of x axis) --

  • yrange (range of y axis) --

  • showplot (bool for toggling showing of the plot) --

Return type:

figure instance of the decided library

pydiana.tools.graph_tools.make_hist(df=None, xvar=None, xlabel='', ylabel='', title='', bins=None, binwidth=None, sep_field='', logx=False, logy=False, xrange=None, normalization=1, style='bar', show_errx=False, show_erry=False, showplot=True)[source]

Interface for easily making pretty histograms ready to be fitted (compatible with ROOT dataframe)

Parameters:
  • df (pandas or ROOT dataframe containing the data) --

  • xvar (string containing the column of the dataframe to plot or array with the numbers that are the input to the histogram) --

  • (str) (style) --

  • (str) --

  • (str) --

  • (int/array-like) (bins) --

  • (str) --

  • (bool) (showplot) --

  • (bool) --

  • (array-like) (xrange) --

  • (float/array-like) (normalization) --

  • (str) --

  • (bool) --

Returns:

hists (dict) : dictionary containing the bin edges and counts of each histogram separated by the keys contained in the column indicated by "sep_field" (if not specified the key will be "totalhist") fig : figure instance (depends on the plotting package used)

pydiana.tools.graph_tools.make_hist_fit(df=None, xvar=None, func=None, hist2fit=None, xlabel='', ylabel='', title='', bins=None, binwidth=None, sep_field='', logx=False, logy=False, xrange=None, get_detail_matrices=True, options='QLSB', summary=True, residuals=True, location_x=0.8, location_y=0.7, style='bar', show_errx=False, show_erry=False, showplot=True, cl=None, skip_fitlabel=None)[source]

Function for generating and fitting a histogram with a user defined function :type df: :param df: :type xvar: :param xvar: :type hist2fit: Optional[str] :param hist2fit: :type xlabel: str :param xlabel: :type ylabel: str :param ylabel: :type title: str :param title: :type bins: :param bins: :param binwidth7: :type sep_field: str :param sep_field: :type logx: :param logx: :type logy: :param logy: :type xrange: Optional[list] :param xrange: :type get_detail_matrices: bool :param get_detail_matrices: :type options: str :param options: :type summary: bool :param summary: :type residuals: bool :param residuals: :type location_x: float :param location_x: :type location_y: float :param location_y: :type style: str :param style: :type showplot: bool :param showplot: :type cl: Optional[float] :param cl: :type skip_fitlabel: Optional[bool] :param skip_fitlabel:

Returns:

results from prf.fit_function figure with histogram and overlayed fitted function (also with residuals and a box with the summary of the fit if enabled)

pydiana.tools.graph_tools.make_hist_residuals(hist, figin, func, fit_result=None, showplot=True, marker=None, errorbars=None, unit=None)[source]

Function for making residual plot for histograms.

Parameters:
  • hist (instance of TH1D containing the histogram used for the fitting. Coordinates and errors are taken from this object.) --

  • figin (input plot to which append the residuals) --

  • func (input function) --

  • fit_result (DataFrame like the output of extract_fit_parameters. If None the default func parameters are used) --

  • showplot (bool for enabling plot showing) --

pydiana.tools.graph_tools.make_residuals(figin, x, y, func, fit_result=None, err_x=None, err_y=None, points2fit=None, showplot=True, marker=None, errorbars=None, unit=None)[source]

Function for making residual plot for fit.

Parameters:
  • figin (input plot to which append the residuals) --

  • x (x coordinates used for residual calculation) --

  • y (y coordinates used for residual calculation) --

  • func (input function) --

  • fit_result (DataFrame like the output of extract_fit_parameters. If None the default func parameters are used) --

  • showplot (bool for enabling plot showing) --

  • err_x (x errors used for residual calculation) --

  • err_y (y errors used for residual calculation) --

pydiana.tools.graph_tools.make_scatter(y, x=None, err_x=None, err_y=None, df=None, sep_field=None, ylab='y', xlab='x', title='', logx=False, logy=False, xrange=None, yrange=None, showplot=True, return_scatter=False, mode='p')[source]

Function for making a scatterplot. (NOT COMPATIBLE WITH ROOT DATAFRAMES, maybe it's not needed)

Parameters:
  • y (array with the y variable) --

  • x (array with the x variable) --

  • err_y (array with the error on the y variable) --

  • err_x (array with the error on the x variable) --

  • ylab (string with the label of the y-axis) --

  • xlab (string with the label of the x-axis) --

  • title (string with the title of the plot) --

  • lohx (bool for toggling logscale on x) --

  • logy (bool for toggling logscale on y) --

  • xrange (range of x axis) --

  • yrange (range of y axis) --

  • showplot (bool for toggling showing of the plot) --

  • mode (string indicating of line to use: 'l' for lines, 'p' for points, 'lp' for lines and points) --

Return type:

figure instance of the decided library

pydiana.tools.graph_tools.make_scatter_polyfit(x, y, degree, df=None, name=None, unit_x=None, unit_y=None, err_x=None, err_y=None, xrange=None, xlab='', ylab='', title='', logx=False, logy=False, showplot=True, points2skip=None, residuals=True, summary=True, sort=False, fit_options='QSB', get_detail_matrices=False, legendpos_x=0.8, legendpos_y=0.2, printchi2=True, cl=None, fitcolor='#FF7F0E', skip_fitlabel=False)[source]

Function for easily making polinomial fits of scatter plots

Parameters:
  • x (array containing the x coordinates of the data. If df is provided then x can be the string of the df column (if the unit is written in square brackets then it is automatically read)) --

  • y (array containing the y coordinates of the data. If df is provided then y can be the string of the df column (if the unit is written in square brackets then it is automatically read)) --

  • degree (Degree of the polynomial to be fitted) --

  • df (dataframe with the data to fit. If not provided then one can provide arrays to x and y) --

  • err_x (array containing the errors on the x coordinates. If df is provided then err_x can be the string of the df column) --

  • err_y (array containing the errors on the y coordinates. If df is provided then err_y can be the string of the df column) --

  • xrange (fitting range, if not provided then all the range is used.) --

  • xlab (string with the x axis label) --

  • ylab (string with the y axis label) --

  • showplot (boolean to enable plot showing) --

  • points2skip (list with the indexes of which points need to be skipped for the fit (is sort=True in order of x)) --

  • residuals (bool for enabling the construnction of the residuals plot) --

  • summary (bool for enabling the plotting of the summary legend) --

  • fit_options (string with ROOT's fitting options) --

  • matrices (get_detailed) --

  • legendpos_x (int specifying the x-position of the summary legend (from 0 to 1)) --

  • legendpos_y (int specifying the y-position of the summary legend (from 0 to 1)) --

  • cl (float between 0<cl<1 with confidence interval of fit to plot. If None it will not be plotted.) --

Returns:

  • return [fit_results,figure]

  • if get_detailed_matrices -- return [fit_results,matrices,figure]

pydiana.tools.graph_tools.make_subplots(figures, rows=None, cols=None, shared_xaxes=False, shared_yaxes=False, x_title=None, y_title=None, showplot=False, shared_labels=False, **kwargs)[source]

Utility function for making plotly subplots

Parameters:
  • figures (list of figures) --

  • rows (int indicating how many subplot rows) --

  • cols (int indicating how many subplot columns) --

  • shared_xaxes (bool for enabling xaxes sharing) --

  • shared_yaxes (bool for enabling yaxes sharing) --

  • x_title (string that sets the global x-axis title) --

  • y_title (string that sets the global y-axis title) --

  • showplot (bool indicating whether or not to show the plot) --

  • **kwargs --

pydiana.tools.graph_tools.makelogaxis(*axis_range, maxticks=10)[source]

Function for making the labels for a log-axis

Parameters:
  • *axis_range (two numbers with the range of the axis) --

  • maxticks (maximum number of ticks to be returned) --

Returns:

1D array with the tick values 1D array with the tick labels

pydiana.tools.graph_tools.overlay_fit_func(figin, fit, func, funlab=None, showplot=True, color=None, summary=False, location_x=0.7, location_y=0.7, cl=None, clopacity=0.3, skip_fitlabel=False)[source]

Function used to overlay a fitted function to a plot

Parameters:
  • figin (input figure) --

  • fit (pandas dataframe) --

  • func (instance of root function) --

  • funlab (label of the fit function for the plot) --

  • showplot (boolean to toggle the showplot) --

  • summary (boolean for enabling summary fit legend) --

  • location_x (relative x position of fit legend) --

  • location_y (relative y position of fit legend) --

  • cl (if not None then plots a colorband around the function that specifies the confidence interval specified) --

Returns:

fig

Return type:

output figure

pydiana.tools.graph_tools.overlay_func(figin, func, npoints=1000, params=None, xrange=None, funlab=None, showplot=True, color=None, skip_fitlabel=False)[source]

Function used to overlay a function to a plot

Parameters:
  • figin (input figure) --

  • func (instance of root function) --

  • npoints (number of points used to plot the function) --

  • params (dictionary of the values of the parameters) --

  • xrange (range in which to plot the function) --

  • funlab (label of the fit function for the plot) --

  • showplot (boolean to toggle the showplot) --

Returns:

fig

Return type:

output figure

pydiana.tools.graph_tools.overlay_plots(figs, showplot=False, labels=None, colors=None, skip_traces=None, autoyrescale=False, autoxrescale=False, useTotalIndex=True, skip_label=None)[source]

Function for overlaying plots (with matplotlib only works with scatter plots and if histograms are the first plot).

Parameters:
  • (array-like) (figs) --

  • (bool) (showplot) --

  • (dict) (labcolorsels) --

  • (dict) --

Return type:

fig = one plot containing all the overlay

pydiana.tools.graph_tools.plot_fit_matrices(matrices, variables, showplot=False, **kwargs)[source]

Utility function for plotting covariance and correlation matrices

Parameters:
  • matrices (dictionary containing the matrices title as keys and the 2D array of the matrix as elemets) --

  • variables (list containing the parameters' names) --

  • showplot (bool indicating whether or not to show the plot) --

pydiana.tools.graph_tools.plot_heatmap(counts=None, xbin_edges=None, ybin_edges=None, xlabel=None, ylabel=None, zlabel=None, title=None, trace_label=None, logx=False, logy=False, logz=False, xrange=None, yrange=None, zrange=None, hist2d=None, sidepannels=True, colorscale=None)[source]

Function for plotting a precomputed 2dhistogram :param counts=None: :param : :param xbin_edges=None: :param : :param ybin_edges=None: :param : :type xlabel: Optional[str] :param xlabel: :type xlabel: str=None, :type ylabel: Optional[str] :param ylabel: :type ylabel: str=None, :type zlabel: Optional[str] :param zlabel: :type zlabel: str=None, :type title: Optional[str] :param title: :type title: str=None, :type trace_label: Optional[str] :param trace_label: :type trace_label: str = None, :param logx=False: :param : :param logy=False: :param : :param logz=False: :param : :type xrange: Optional[list] :param xrange: :type xrange: list=None, :type yrange: Optional[list] :param yrange: :type yrange: list=None, :param hist2d=None: :param :

Returns:

figure instance

pydiana.tools.graph_tools.plot_histogram(counts=None, bin_edges=None, figin=None, xlabel=None, ylabel=None, title=None, trace_label=None, logx=False, logy=False, xrange=None, style='bar', show_errx=False, show_erry=True, error_y=None, hist=None)[source]

Function for plotting a precomputed histogram :type counts: :param counts: :type bin_edges: :param bin_edges: :type figin: :param figin: :type xlabel: Optional[str] :param xlabel: :type ylabel: Optional[str] :param ylabel: :type title: Optional[str] :param title: :type trace_label: Optional[str] :param trace_label: :type logx: :param logx: :type logy: :param logy: :type xrange: Optional[list] :param xrange: :type style: str :param style: :type show_errx: bool :param show_errx: :type show_erry: bool :param show_erry: :type error_y: :param error_y: :param hist ROOT histogram instance:

Returns:

figure instance

pydiana.tools.graph_tools.plot_matrix(matrix, ylabel='', xlabel='', title='', colorscale='Viridis', exponentformat='power', showplot=False, **kwargs)[source]

Function for plotting a matrix as a heatmap.

Parameters:
  • matrix (2D array with the matrix values) --

  • ylabel (string with the labels for the yaxis) --

  • xlabel (string with the labels for the xaxis) --

  • title (string with the title for the plot) --

  • colorscale (string indicating the colorscale) --

  • exponentformat (string indicating the type of colorscale to use) --

  • showplot (Boolean indicating wether to show or not the plot) --

  • **kwargs --

Return type:

go.Figure Instance

pydiana.tools.graph_tools.plotly2dict(fig, outfile, savefullfig=False)[source]

Function for exporting a plotly figure to a dictionary and saving it to file

Parameters:
  • fig (go.Figure() instance to save to file) --

  • outfile (path to the file to be created (if .pkl then pickle is used otherwise json to write text files)) --

  • savefullfig (calls the full_figure_for_development() function to save all parameters (usually not necessary because all user set parameters are used and the rest is just the estetical configuration)) --

pydiana.tools.graph_tools.plotly_from_dict(infile)[source]

Function for importing a figure saved to a file as dict (see plotly2dict function)

Parameters:

infile (path to the file to read) --

Returns:

plotly figure instance

pydiana.tools.graph_tools.rebin_histogram(histogram, rebin_factor=3)[source]

Function for reducing histogram binning

Parameters:
  • histogram (dictionary with the histogram bin_edges and counts) --

  • rebin_factor (integer indicating how many bins will be summed together) --

Return type:

dictionary with histogram

pydiana.tools.graph_tools.summary_plot(df, var, err_var=None, labels=None, color=None, title=None, unit=None, round_digit=1, showMean=True, strict_color=False, showSigma=True, add2title=True, showplot=True, showlegend=False, separate_stats=False, annotation_size=25, annotation_positions=['top right', 'top left', 'bottom right', 'bottom left'], make_annotation=False, left_margin=None, showchi2=True)[source]

Function that automatically construct summary plot of a given var2plot. If getError is False, var2plot is plotted with no error. If getColor is False, plot all points of the same color (red). (NOT COMPATIBLE WITH ROOT DATAFRAME, in progress)

Parameters:
  • df (Pandas dataframe with final results) --

  • (str) (unit) --

  • (str) --

  • (str) --

  • (str) --

  • (str) --

  • (str) --

  • (int) (round_digit) --

  • (bool) (showplot) --

  • (bool) --

  • (bool) --

  • (bool) --

  • (bool) --

Returns:

fig : plotly scatter of Channel vs df[var2plot]