From a515ebb3b2af32f99a8b1d5d374b4b52448d7c8a Mon Sep 17 00:00:00 2001 From: fbury <florian.bury@cern.ch> Date: Fri, 22 Oct 2021 11:44:36 +0200 Subject: [PATCH] Add vertical drawing --- Chapter_5/Histogram.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Chapter_5/Histogram.py b/Chapter_5/Histogram.py index b19810a..7a4720c 100644 --- a/Chapter_5/Histogram.py +++ b/Chapter_5/Histogram.py @@ -56,6 +56,31 @@ class Histogram: if scale != 1: print ("y-axis scale : 1 # = {}".format(1./scale)) + def drawT(self): + """Affiche l'histogramme en console verticalement (plus facile) """ + # Scale my data (bin content) to a fixed width # + y_max = self.get_maximum() + width = 60 + scale = width/y_max + scaled_data = [b*scale for b in self._data] + # Draw y axis # + print ('-' * width + '> y') + # Draw bin by bin # + for ibin,bin_content in enumerate(scaled_data): + line = f'{ibin:3d}|' + for y in range(width): + if bin_content>= y: + line += "#" + else: + line += " " + # Show the unscaled bin content on the right # + line += f'\t{self._data[ibin]:6.0f}' + print (line) + # Draw end of x axis # + print (' v') + print (' x') + # Print scaling of histogram # + print (f'Scaling : `#` = {scale:.5f}') @@ -67,10 +92,11 @@ if __name__=="__main__": h.fill(0.1) h.fill(2) h.fill(-1) - h.draw() + #h.draw() + h.drawT() - h2 = Histogram(100,-5,5) + h2 = Histogram(50,-5,5) from random import gauss - for i in range(int(100000)): + for i in range(int(1000000)): h2.fill(gauss(0,1)) - h2.draw() + h2.drawT() -- GitLab