Skip to content
Snippets Groups Projects
Verified Commit 4f9a0030 authored by Frank Sauerburger's avatar Frank Sauerburger
Browse files

Add hist styling draft

parent 20f1d7c6
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
...@@ -46,9 +46,9 @@ def _type_to_histtype(type): ...@@ -46,9 +46,9 @@ def _type_to_histtype(type):
type_map = {"fill": "stepfilled", "line": "step"} type_map = {"fill": "stepfilled", "line": "step"}
return type_map[type] return type_map[type]
def hist(dataframe, variable, bins, *stacks, data=None, selection=None, def hist(dataframe, variable, bins, *stacks, selection=None,
range=None, color=None, blind=None, axes=None, figure=None, range=None, blind=None, axes=None, figure=None,
weight=None): weight=None, **kwds):
""" """
Creates a histogram of stacked processes. The first argument is the Creates a histogram of stacked processes. The first argument is the
dataframe to operate on. The 'variable' argument defines the x-axis. The dataframe to operate on. The 'variable' argument defines the x-axis. The
...@@ -65,15 +65,9 @@ def hist(dataframe, variable, bins, *stacks, data=None, selection=None, ...@@ -65,15 +65,9 @@ def hist(dataframe, variable, bins, *stacks, data=None, selection=None,
in a list are stacked. The type attributes of processes are considered in a list are stacked. The type attributes of processes are considered
during plotting. during plotting.
The optional color must have the with same structure as the stacked list
and defines the color of the process. For a list of stacked processes, the
corresponding list of colors can be replaced by a callable which is used
repeatedly for each process in the stack. The colors must be valid
matplotlib colors.
The optional blind argument controls which process should be blinded. The The optional blind argument controls which process should be blinded. The
argument can be a list of processes to blind. By default blinding is argument can be a single process, a list of processes or None. By default,
applied to data. Use an empty list to disable blinding. no process is blinded.
If the figure argument is omitted, this method creates a new axes and If the figure argument is omitted, this method creates a new axes and
figure. If axes only is omitted, the method creates a new axes from the figure. If axes only is omitted, the method creates a new axes from the
...@@ -85,6 +79,15 @@ def hist(dataframe, variable, bins, *stacks, data=None, selection=None, ...@@ -85,6 +79,15 @@ def hist(dataframe, variable, bins, *stacks, data=None, selection=None,
The weight is used to weight the entries. Entries have unit The weight is used to weight the entries. Entries have unit
weight if omitted. The argument can be a string name of a column or a weight if omitted. The argument can be a string name of a column or a
variable object. variable object.
Any other keyword argument is used to define the style. If the keyword
argument is a list, it's length must equal the number of stacks. If the
list item is a list, it is cycled for every stack member. If the list
member is not a list, this value is used for all members of the stack.
If the keyword is not a list, the property is passed directly to
matplotlib. The only exception is 'histtype'. The histtype argument can be
'step', 'stepfilled' or 'points'. In the former two cases matplotlibs hist
method ist used, in the latter case 'errorbar' is used.
""" """
# Wrap column string by variable # Wrap column string by variable
if isinstance(variable, str): if isinstance(variable, str):
...@@ -119,22 +122,45 @@ def hist(dataframe, variable, bins, *stacks, data=None, selection=None, ...@@ -119,22 +122,45 @@ def hist(dataframe, variable, bins, *stacks, data=None, selection=None,
bins = np.linspace(range[0], range[1], bins + 1) bins = np.linspace(range[0], range[1], bins + 1)
equidistant_bins = True equidistant_bins = True
# Check structure of kwds
new_kwds = {}
for kwd, value in kwds.items():
if isinstance(value, list):
if len(value) != len(stacks):
raise ValueError("Length of %s must equal number of stacks."
% repr(kwd))
# Wrap properties for the whole stack in a list
new_kwds[kwd] = [(_ if isinstance(_, list) else [_]) for _ in value]
else:
# Single value for all stacks and all processes
new_kwds[kwd] = [[value]]*len(stacks)
kwds = new_kwds
# Handle stack # Handle stack
for stack in stacks: for i_stack, stack in enumerate(stacks):
if isinstance(stack, Process): if isinstance(stack, Process):
# Wrap single process # Wrap single process
stack = [stack] stack = [stack]
bottom = np.zeros(len(bins) - 1) bottom = np.zeros(len(bins) - 1)
for process in stack: for i_process, process in enumerate(stack):
sel = selection & process.selection sel = selection & process.selection
# Prepare style
process_kwds = {}
for kwd, props in kwds.items():
# Cycle through property stacks
stack_props = props[i_stack]
process_kwds[kwd] = stack_props[i_process % len(stack_props)]
n, _, _ = axes.hist(variable(dataframe[sel(dataframe)]), n, _, _ = axes.hist(variable(dataframe[sel(dataframe)]),
bins=bins, range=range, bins=bins, range=range,
bottom=bottom, bottom=bottom,
label=process.label, label=process.label,
histtype=_type_to_histtype(process.type), weights=weight(dataframe[sel(dataframe)]),
weights=weight(dataframe[sel(dataframe)])) **process_kwds)
bottom += n bottom += n
axes.set_xlim((bins.min(), bins.max())) axes.set_xlim((bins.min(), bins.max()))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment