Skip to content

Commit 4f670b5

Browse files
Max BaakMax Baak
authored andcommitted
Added projection functions for 2-dim histograms
Added two functions project_on_x() and project_on_y() for both regular 2-dim Bin and 2-dim SparselyBin histograms. They return a 1-dim Bin or SparselyBin histogram respectively, where the 2-dim histogram has been projected on the x- or y-axis, i.e. has been integrated over the y- or x-axis. So, one can fill a 2-dim histogram, and then extract the corresponding 1-dim histograms from this. Two more small things: Also added a small bug-fix to the xy_ranges_grid() function of the class SparselyTwoDimensionallyHistogramMethods, which now raises a KeyError if the histograms is not filled. In the plotmatplotlib() functions of TwoDimensionallyHistogramMethods and SparselyTwoDimensionallyHistogramMethods, setting the x- and y-axis limits is now done consistently after plotting the histograms.
1 parent 842324e commit 4f670b5

1 file changed

Lines changed: 80 additions & 5 deletions

File tree

histogrammar/plot/matplotlib.py

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ def bin_centers(self):
179179
bin_edges = self.bin_edges()
180180
centers = [(bin_edges[i]+bin_edges[i+1])/2. for i in range(len(bin_edges)-1)]
181181
return np.array(centers)
182-
183-
182+
183+
184184
class ProfileMethods(object):
185185
def plotmatplotlib(self, name=None, **kwargs):
186186
""" Plotting method for Bin of Average
@@ -418,10 +418,10 @@ def plotmatplotlib(self, name=None, **kwargs):
418418
ax = plt.gca()
419419

420420
x_ranges, y_ranges, grid = self.xy_ranges_grid()
421-
ax.set_ylim(self.y_lim())
422-
ax.set_xlim(self.x_lim())
423421

424422
ax.pcolormesh(x_ranges, y_ranges, grid, **kwargs)
423+
ax.set_ylim(self.y_lim())
424+
ax.set_xlim(self.x_lim())
425425

426426
if name is not None:
427427
ax.set_title(name)
@@ -456,7 +456,39 @@ def y_lim(self):
456456
"""
457457
samp = self.values[0]
458458
return (samp.low,samp.high)
459+
460+
def project_on_x(self):
461+
""" project 2d histogram onto x-axis
462+
463+
:returns: on x-axis projected histogram (1d)
464+
:rtype: histogrammar.Bin
465+
"""
466+
from histogrammar import Bin, Count
467+
468+
h_x = Bin(num = self.num, low = self.low, high = self.high, \
469+
quantity = self.quantity, value = Count())
470+
# loop over all counters and integrate over y (=j)
471+
for i,bi in enumerate(self.values):
472+
h_x.values[i].entries += sum(bj.entries for bj in bi.values)
473+
return h_x
474+
475+
def project_on_y(self):
476+
""" project 2d histogram onto y-axis
477+
478+
:returns: on y-axis projected histogram (1d)
479+
:rtype: histogrammar.Bin
480+
"""
481+
from histogrammar import Bin, Count
459482

483+
ybin = self.values[0]
484+
h_y = Bin(num = ybin.num, low = ybin.low, high = ybin.high, \
485+
quantity = ybin.quantity, value = Count())
486+
# loop over all counters and integrate over x (=i)
487+
for bi in self.values:
488+
for j,bj in enumerate(bi.values):
489+
h_y.values[j].entries += bj.entries
490+
return h_y
491+
460492

461493
class SparselyTwoDimensionallyHistogramMethods(object):
462494
def plotmatplotlib(self, name=None, **kwargs):
@@ -491,7 +523,11 @@ def xy_ranges_grid(self):
491523
yminBin, ymaxBin, ynum, ylow, yhigh = prepare2Dsparse(self)
492524

493525
xbinWidth = self.binWidth
494-
ybinWidth = self.bins[0].binWidth
526+
try:
527+
ykey = list(self.bins.keys())[0]
528+
except:
529+
raise KeyError('SparselyBin 2d hist is not filled.')
530+
ybinWidth = self.bins[ykey].binWidth
495531

496532
xmaxBin = max(self.bins.keys())
497533
xminBin = min(self.bins.keys())
@@ -521,3 +557,42 @@ def y_lim(self):
521557
yminBin, ymaxBin, ynum, ylow, yhigh = prepare2Dsparse(self)
522558
return (ylow,yhigh)
523559

560+
def project_on_x(self):
561+
""" project 2d sparselybin histogram onto x-axis
562+
563+
:returns: on x-axis projected histogram (1d)
564+
:rtype: histogrammar.SparselyBin
565+
"""
566+
from histogrammar import SparselyBin, Count
567+
568+
h_x = SparselyBin(binWidth = self.binWidth, origin = self.origin, \
569+
quantity = self.quantity, value = Count())
570+
# loop over all counters and integrate over y (=j)
571+
for i in self.bins:
572+
bi = self.bins[i]
573+
h_x.bins[i] = Count.ed( sum(bi.bins[j].entries for j in bi.bins) )
574+
return h_x
575+
576+
def project_on_y(self):
577+
""" project 2d sparselybin histogram onto y-axis
578+
579+
:returns: on y-axis projected histogram (1d)
580+
:rtype: histogrammar.SparselyBin
581+
"""
582+
from histogrammar import SparselyBin, Count
583+
584+
try:
585+
ykey = list(self.bins.keys())[0]
586+
except:
587+
raise KeyError('SparselyBin 2d hist is not filled. Cannot project on y-axis.')
588+
ybin = self.bins[ykey]
589+
h_y = SparselyBin(binWidth = ybin.binWidth, origin = ybin.origin, \
590+
quantity = ybin.quantity, value = Count())
591+
# loop over all counters and integrate over x (=i)
592+
for i in self.bins:
593+
bi = self.bins[i]
594+
for j in bi.bins:
595+
if not j in h_y.bins:
596+
h_y.bins[j] = Count()
597+
h_y.bins[j].entries += bi.bins[j].entries
598+
return h_y

0 commit comments

Comments
 (0)