x-ticks location added for bar-graph

This commit is contained in:
karthikaeyetea
2021-09-01 13:37:58 +05:30
parent d50e0861f3
commit 81470a17a1

View File

@@ -50,6 +50,7 @@ class lf_bar_graph():
_show_bar_value=False, _show_bar_value=False,
_xaxis_step=1, _xaxis_step=1,
_xticks_font = None, _xticks_font = None,
_xaxis_value_location = 0,
_text_font=None, _text_font=None,
_text_rotation=None, _text_rotation=None,
_grp_title = "", _grp_title = "",
@@ -79,6 +80,7 @@ class lf_bar_graph():
self.show_bar_value = _show_bar_value self.show_bar_value = _show_bar_value
self.xaxis_step = _xaxis_step self.xaxis_step = _xaxis_step
self.xticks_font = _xticks_font self.xticks_font = _xticks_font
self._xaxis_value_location = _xaxis_value_location
self.text_font = _text_font self.text_font = _text_font
self.text_rotation = _text_rotation self.text_rotation = _text_rotation
self.grp_title = _grp_title self.grp_title = _grp_title
@@ -129,8 +131,8 @@ class lf_bar_graph():
if self.xaxis_categories[0] == 0: if self.xaxis_categories[0] == 0:
plt.xticks(np.arange(0, len(self.xaxis_categories), step=self.xaxis_step),fontsize = self.xticks_font) plt.xticks(np.arange(0, len(self.xaxis_categories), step=self.xaxis_step),fontsize = self.xticks_font)
else: else:
plt.xticks(np.arange(0, len(self.data_set[0]), step=self.xaxis_step), self.xaxis_categories, plt.xticks([i + self._xaxis_value_location for i in np.arange(0, len(self.data_set[0]), step=self.xaxis_step)],
fontsize = self.xticks_font) self.xaxis_categories, fontsize=self.xticks_font)
plt.legend(handles=self.legend_handles, loc=self.legend_loc, bbox_to_anchor=self.legend_box, ncol=self.legend_ncol, fontsize=self.legend_fontsize) plt.legend(handles=self.legend_handles, loc=self.legend_loc, bbox_to_anchor=self.legend_box, ncol=self.legend_ncol, fontsize=self.legend_fontsize)
plt.suptitle(self.title, fontsize=self.title_size) plt.suptitle(self.title, fontsize=self.title_size)
plt.title(self.grp_title) plt.title(self.grp_title)
@@ -333,6 +335,80 @@ class lf_horizontal_stacked_graph():
return "%s.png" % self.graph_image_name return "%s.png" % self.graph_image_name
class lf_line_graph():
def __init__(self,_data_set=[[30.4, 55.3, 69.2, 37.1], [45.1, 67.2, 34.3, 22.4], [22.5, 45.6, 12.7, 34.8]],
_xaxis_name="x-axis",
_yaxis_name="y-axis",
_xaxis_categories=[1, 2, 3, 4, 5],
_xaxis_label=["a", "b", "c", "d", "e"],
_graph_title="",
_title_size=16,
_graph_image_name="image_name",
_label=["bi-downlink", "bi-uplink", 'uplink'],
_font_weight='bold',
_color=['forestgreen', 'y', 'r', 'g', 'b', 'p'],
_figsize=(10, 5),
_xaxis_step = 5,
_xticks_font = None,
_text_font=None,
_legend_handles=None,
_legend_loc="best",
_legend_box=None,
_legend_ncol=1,
_legend_fontsize=None,
_marker=None,
_dpi=96,
_enable_csv=False):
self.data_set = _data_set
self.xaxis_name = _xaxis_name
self.yaxis_name = _yaxis_name
self.xaxis_categories = _xaxis_categories
self.xaxis_label = _xaxis_label
self.grp_title = _graph_title
self.title_size = _title_size
self.graph_image_name = _graph_image_name
self.label = _label
self.color = _color
self.font_weight = _font_weight
self.figsize = _figsize
self.xaxis_step = _xaxis_step
self.xticks_font = _xticks_font
self.text_font = _text_font
self.marker = _marker
self.enable_csv = _enable_csv
self.lf_csv = lf_csv()
self.legend_handles = _legend_handles
self.legend_loc = _legend_loc
self.legend_box = _legend_box
self.legend_ncol = _legend_ncol
self.legend_fontsize = _legend_fontsize
def build_line_graph(self):
fig = plt.subplots(figsize=self.figsize)
i = 0
for data in self.data_set:
plt.plot(self.xaxis_categories, data, color=self.color[i], label=self.label[i], marker = self.marker)
i += 1
plt.xlabel(self.xaxis_name, fontweight='bold', fontsize=15)
plt.ylabel(self.yaxis_name, fontweight='bold', fontsize=15)
plt.legend(handles=self.legend_handles, loc=self.legend_loc, bbox_to_anchor=self.legend_box, ncol=self.legend_ncol, fontsize=self.legend_fontsize)
plt.suptitle(self.grp_title, fontsize=self.title_size)
fig = plt.gcf()
plt.savefig("%s.png" % self.graph_image_name, dpi=96)
plt.close()
print("{}.png".format(self.graph_image_name))
if self.enable_csv:
if self.data_set is not None:
self.lf_csv.columns = self.label
self.lf_csv.rows = self.data_set
self.lf_csv.filename = f"{self.graph_image_name}.csv"
self.lf_csv.generate_csv()
else:
print("No Dataset Found")
print("{}.csv".format(self.graph_image_name))
return "%s.png" % self.graph_image_name
# Unit Test # Unit Test
if __name__ == "__main__": if __name__ == "__main__":
output_html_1 = "graph_1.html" output_html_1 = "graph_1.html"