From d02d050152b883196183495ebe4eeb5b2efcfc81 Mon Sep 17 00:00:00 2001 From: anjali Date: Thu, 13 May 2021 21:54:46 +0530 Subject: [PATCH] added horizontal stacked bar chart, some changes in report and graph library --- py-scripts/lf_graph.py | 94 ++++++++++++++++++++++++++++++++---- py-scripts/lf_report.py | 2 +- py-scripts/lf_report_test.py | 39 ++++++++++++++- 3 files changed, 122 insertions(+), 13 deletions(-) diff --git a/py-scripts/lf_graph.py b/py-scripts/lf_graph.py index e7af1382..f548bfa2 100755 --- a/py-scripts/lf_graph.py +++ b/py-scripts/lf_graph.py @@ -24,6 +24,7 @@ import numpy as np import pandas as pd import pdfkit import math +from matplotlib.colors import ListedColormap # internal candela references included during intial phases, to be deleted at future date @@ -98,8 +99,9 @@ class lf_bar_graph(): class lf_scatter_graph(): def __init__(self, - _x_data_set= ["sta0 ","sta1","sta2","sta3"], - _y_data_set= [[30,55,69,37]], + _x_data_set=["sta0 ","sta1","sta2","sta3"], + _y_data_set=[[30,55,69,37]], + _values =None, _xaxis_name="x-axis", _yaxis_name="y-axis", _label = ["num1", "num2"], @@ -114,19 +116,28 @@ class lf_scatter_graph(): self.graph_image_name = _graph_image_name self.color = _color self.label = _label + self.values = _values def build_scatter_graph(self): if self.color is None: self.color = ["orchid", "lime", "aquamarine", "royalblue", "darkgray", "maroon"] fig = plt.subplots(figsize=self.figsize) - plt.scatter(self.x_data_set, self.y_data_set[0], color=self.color[0], label=self.label[0]) - if len(self.y_data_set) > 1: - for i in range(1,len(self.y_data_set)): - plt.scatter(self.x_data_set, self.y_data_set[i], color=self.color[i], label=self.label[i]) - plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) - plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) - plt.gcf().autofmt_xdate() - plt.legend() + if self.values is None: + plt.scatter(self.x_data_set, self.y_data_set[0], color=self.color[0], label=self.label[0]) + if len(self.y_data_set) > 1: + for i in range(1,len(self.y_data_set)): + plt.scatter(self.x_data_set, self.y_data_set[i], color=self.color[i], label=self.label[i]) + plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) + plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) + plt.gcf().autofmt_xdate() + plt.legend() + else: + colours = ListedColormap(self.color) + scatter = plt.scatter(self.x_data_set, self.y_data_set, c=self.values, cmap=colours) + plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15) + plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15) + plt.gcf().autofmt_xdate() + plt.legend(handles=scatter.legend_elements()[0], labels=self.label) plt.savefig("%s.png" % (self.graph_image_name), dpi=96) plt.close() print("{}.png".format(self.graph_image_name)) @@ -167,6 +178,69 @@ class lf_stacked_graph(): plt.close() print("{}.png".format(self.graph_image_name)) + return "%s.png" % (self.graph_image_name) + +class lf_horizontal_stacked_graph(): + def __init__(self, + _seg= 2, + _yaxis_set=('A','B'), + _xaxis_set1=[12, 0, 0, 16, 15], + _xaxis_set2 = [23, 34, 23, 0], + _unit ="%", + _xaxis_name="Stations", + _label=['Success', 'Fail'], + _graph_image_name="image_name", + _color=["success", "Fail"], + _figsize=(9,4)): + self.unit = _unit + self.seg = _seg + self.xaxis_set1 = _xaxis_set1 + self.xaxis_set2 = _xaxis_set2 + self.yaxis_set = _yaxis_set + self.xaxis_name = _xaxis_name + self.figsize = _figsize + self.graph_image_name = _graph_image_name + self.label = _label + self.color = _color + + def build_horizontal_stacked_graph(self): + def sumzip(items): + return [sum(values) for values in zip(items)] + + fig, ax = plt.subplots(figsize=self.figsize) + + N = self.seg + values1 = self.xaxis_set1 + values2 = self.xaxis_set2 + + ind = np.arange(N) + .15 + width = 0.3 + + rects1 = plt.barh(ind, values1, width, color=self.color[0], label=self.label[0]) + rects2 = plt.barh(ind, values2, width, left=sumzip(values1), color=self.color[1],label=self.label[1]) + + extra_space = 0.15 + ax.set_yticks(ind + width - extra_space) + ax.set_yticklabels(self.yaxis_set) + ax.yaxis.set_tick_params(length=0, labelbottom=True) + + for i, v in enumerate(values1): + if v!=0: + plt.text(v * 0.45, i + .145, "%s%s" % (v, self.unit), color='white', fontweight='bold', fontsize=10, + ha='center', va='center') + + for i, v in enumerate(values2): + if v != 0: + plt.text(v * 0.45 + values1[i], i + .145, "%s%s" % (v, self.unit), color='white', fontweight='bold', fontsize=10, + ha='center', va='center') + + ax.spines['right'].set_visible(False) + ax.spines['top'].set_visible(False) + ax.legend(loc='upper right') + plt.savefig("%s.png" % (self.graph_image_name), dpi=96) + plt.close() + print("{}.png".format(self.graph_image_name)) + return "%s.png" % (self.graph_image_name) # Unit Test if __name__ == "__main__": diff --git a/py-scripts/lf_report.py b/py-scripts/lf_report.py index 762de904..0c6b6c85 100755 --- a/py-scripts/lf_report.py +++ b/py-scripts/lf_report.py @@ -275,7 +275,7 @@ class lf_report(): os.mkdir(self.path_date_time) def build_table(self): - self.dataframe_html = self.dataframe.to_html(index=False) # have the index be able to be passed in. + self.dataframe_html = self.dataframe.to_html(index=False, justify='center') # have the index be able to be passed in. self.html += self.dataframe_html def build_custom(self): diff --git a/py-scripts/lf_report_test.py b/py-scripts/lf_report_test.py index eff1fbd1..75e1cbec 100755 --- a/py-scripts/lf_report_test.py +++ b/py-scripts/lf_report_test.py @@ -24,7 +24,7 @@ import numpy as np import pandas as pd import pdfkit from lf_report import lf_report -from lf_graph import lf_bar_graph, lf_scatter_graph , lf_stacked_graph +from lf_graph import lf_bar_graph, lf_scatter_graph , lf_stacked_graph, lf_horizontal_stacked_graph # Unit Test if __name__ == "__main__": @@ -103,7 +103,7 @@ if __name__ == "__main__": report.build_graph() set1 = [1, 2, 3, 4] set2 = [[45, 67, 45, 34], [34, 56, 45, 34], [45, 78, 23, 45]] - graph2 = lf_scatter_graph(_x_data_set=set1, _y_data_set=set2, _xaxis_name="x-axis", + graph2 = lf_scatter_graph(_x_data_set=set1, _y_data_set=set2, _xaxis_name="x-axis", _values = None, _yaxis_name="y-axis", _graph_image_name="image_name1", _color=None, @@ -115,6 +115,20 @@ if __name__ == "__main__": report.set_graph_image(graph_png) report.move_graph_image() + report.build_graph() + # this will generate graph which is independent,we can customize the value with different colors + graph2 = lf_scatter_graph(_x_data_set=set1, _y_data_set=[45, 67, 45, 34], _values=[0 , 0, 0, 1], _xaxis_name="x-axis", + _yaxis_name="y-axis", + _graph_image_name="image_name_map", + _color=None, + _label=["s1", "s2"]) + graph_png = graph2.build_scatter_graph() + + print("graph name {}".format(graph_png)) + + report.set_graph_image(graph_png) + report.move_graph_image() + report.build_graph() dataset = [["1", "2", "3", "4"], [12, 45, 67, 34], [23, 67, 23, 12], [25, 45, 34, 23]] graph = lf_stacked_graph(_data_set=dataset, @@ -131,6 +145,27 @@ if __name__ == "__main__": report.set_graph_image(graph_png) report.move_graph_image() report.build_graph() + + + + graph = lf_horizontal_stacked_graph(_seg= 2, + _yaxis_set=('A','B'), + _xaxis_set1=[12, 65], + _xaxis_set2=[23, 34], + _unit="", + _xaxis_name="Stations", + _label=['Success', 'Fail'], + _graph_image_name="image_name_pass_fail", + _color=["r", "g"], + _figsize=(9, 4)) + + graph_png = graph.build_horizontal_stacked_graph() + + print("graph name {}".format(graph_png)) + + report.set_graph_image(graph_png) + report.move_graph_image() + report.build_graph() #report.build_all() html_file = report.write_html()