@@ -180,7 +180,71 @@ def bin_centers(self):
180180 centers = [(bin_edges [i ]+ bin_edges [i + 1 ])/ 2. for i in range (len (bin_edges )- 1 )]
181181 return np .array (centers )
182182
183-
183+
184+ class CategorizeHistogramMethods (object ):
185+ def plotmatplotlib (self , name = None , ** kwargs ):
186+ """
187+ name : title of the plot.
188+ kwargs : `matplotlib.patches.Rectangle` properties.
189+
190+ Returns a matplotlib.axes instance
191+ """
192+ import matplotlib .pyplot as plt
193+ import numpy as np
194+ ax = plt .gca ()
195+
196+ width = kwargs .pop ('width' ,0.8 )
197+
198+ labels = self .bin_labels ()
199+ values = self .bin_entries ()
200+ assert len (labels )== len (values ), \
201+ 'labels and values have different array lengths: %d vs %d.' % \
202+ (len (labels ),len (values ))
203+
204+ # plot histogram
205+ tick_pos = np .arange (len (labels )) + 0.5
206+ ax .bar (tick_pos - 0.4 , values , width = width , ** kwargs )
207+
208+ # set x-axis properties
209+ def xtick (lab ):
210+ lab = str (lab )
211+ if len (lab ) > 20 :
212+ lab = lab [:17 ] + '...'
213+ return lab
214+ ax .set_xlim ((0. , float (len (labels ))))
215+ ax .set_xticks (tick_pos )
216+ ax .set_xticklabels ([xtick (lab ) for lab in labels ], fontsize = 12 , rotation = 90 )
217+
218+ # set title
219+ if name is not None :
220+ ax .set_title (name )
221+ else :
222+ ax .set_title (self .name )
223+
224+ return ax
225+
226+ def bin_entries (self ):
227+ """
228+ Returns bin values
229+ """
230+ import numpy as np
231+ return np .array ([self .bins [i ].entries for i in self .bins ])
232+
233+ def bin_labels (self ):
234+ """
235+ Returns bin labels
236+ """
237+ import numpy as np
238+ labels = []
239+ for i ,key in enumerate (self .bins .keys ()):
240+ try :
241+ label = str (key )
242+ except :
243+ label = 'bin_%d' % i
244+ labels .append (label )
245+ return np .asarray (labels )
246+
247+
184248class ProfileMethods (object ):
185249 def plotmatplotlib (self , name = None , ** kwargs ):
186250 """ Plotting method for Bin of Average
@@ -418,10 +482,10 @@ def plotmatplotlib(self, name=None, **kwargs):
418482 ax = plt .gca ()
419483
420484 x_ranges , y_ranges , grid = self .xy_ranges_grid ()
421- ax .set_ylim (self .y_lim ())
422- ax .set_xlim (self .x_lim ())
423485
424486 ax .pcolormesh (x_ranges , y_ranges , grid , ** kwargs )
487+ ax .set_ylim (self .y_lim ())
488+ ax .set_xlim (self .x_lim ())
425489
426490 if name is not None :
427491 ax .set_title (name )
@@ -456,7 +520,39 @@ def y_lim(self):
456520 """
457521 samp = self .values [0 ]
458522 return (samp .low ,samp .high )
523+
524+ def project_on_x (self ):
525+ """ project 2d histogram onto x-axis
526+
527+ :returns: on x-axis projected histogram (1d)
528+ :rtype: histogrammar.Bin
529+ """
530+ from histogrammar import Bin , Count
531+
532+ h_x = Bin (num = self .num , low = self .low , high = self .high , \
533+ quantity = self .quantity , value = Count ())
534+ # loop over all counters and integrate over y (=j)
535+ for i ,bi in enumerate (self .values ):
536+ h_x .values [i ].entries += sum (bj .entries for bj in bi .values )
537+ return h_x
538+
539+ def project_on_y (self ):
540+ """ project 2d histogram onto y-axis
541+
542+ :returns: on y-axis projected histogram (1d)
543+ :rtype: histogrammar.Bin
544+ """
545+ from histogrammar import Bin , Count
459546
547+ ybin = self .values [0 ]
548+ h_y = Bin (num = ybin .num , low = ybin .low , high = ybin .high , \
549+ quantity = ybin .quantity , value = Count ())
550+ # loop over all counters and integrate over x (=i)
551+ for bi in self .values :
552+ for j ,bj in enumerate (bi .values ):
553+ h_y .values [j ].entries += bj .entries
554+ return h_y
555+
460556
461557class SparselyTwoDimensionallyHistogramMethods (object ):
462558 def plotmatplotlib (self , name = None , ** kwargs ):
@@ -491,7 +587,11 @@ def xy_ranges_grid(self):
491587 yminBin , ymaxBin , ynum , ylow , yhigh = prepare2Dsparse (self )
492588
493589 xbinWidth = self .binWidth
494- ybinWidth = self .bins [0 ].binWidth
590+ try :
591+ ykey = list (self .bins .keys ())[0 ]
592+ except :
593+ raise KeyError ('SparselyBin 2d hist is not filled.' )
594+ ybinWidth = self .bins [ykey ].binWidth
495595
496596 xmaxBin = max (self .bins .keys ())
497597 xminBin = min (self .bins .keys ())
@@ -521,3 +621,42 @@ def y_lim(self):
521621 yminBin , ymaxBin , ynum , ylow , yhigh = prepare2Dsparse (self )
522622 return (ylow ,yhigh )
523623
624+ def project_on_x (self ):
625+ """ project 2d sparselybin histogram onto x-axis
626+
627+ :returns: on x-axis projected histogram (1d)
628+ :rtype: histogrammar.SparselyBin
629+ """
630+ from histogrammar import SparselyBin , Count
631+
632+ h_x = SparselyBin (binWidth = self .binWidth , origin = self .origin , \
633+ quantity = self .quantity , value = Count ())
634+ # loop over all counters and integrate over y (=j)
635+ for i in self .bins :
636+ bi = self .bins [i ]
637+ h_x .bins [i ] = Count .ed ( sum (bi .bins [j ].entries for j in bi .bins ) )
638+ return h_x
639+
640+ def project_on_y (self ):
641+ """ project 2d sparselybin histogram onto y-axis
642+
643+ :returns: on y-axis projected histogram (1d)
644+ :rtype: histogrammar.SparselyBin
645+ """
646+ from histogrammar import SparselyBin , Count
647+
648+ try :
649+ ykey = list (self .bins .keys ())[0 ]
650+ except :
651+ raise KeyError ('SparselyBin 2d hist is not filled. Cannot project on y-axis.' )
652+ ybin = self .bins [ykey ]
653+ h_y = SparselyBin (binWidth = ybin .binWidth , origin = ybin .origin , \
654+ quantity = ybin .quantity , value = Count ())
655+ # loop over all counters and integrate over x (=i)
656+ for i in self .bins :
657+ bi = self .bins [i ]
658+ for j in bi .bins :
659+ if not j in h_y .bins :
660+ h_y .bins [j ] = Count ()
661+ h_y .bins [j ].entries += bi .bins [j ].entries
662+ return h_y
0 commit comments