def plot(self, level, per_line=3): """Temporary function to plot data. Expected to be implemented in plotter """ num_plots = self._first_agg.topology.level_span(level) result_1 = self._first_agg.aggregate(level=level, **self._agg_kwargs) result_2 = self._second_agg.aggregate(level=level, **self._agg_kwargs) layout = PlotLayout(per_line, num_plots) plot_index = 0 for group_id, result_group in enumerate(result_1): s_x = result_group s_y = result_2[group_id] s_x = self._resample(s_x) s_y = self._resample(s_y) s_x, s_y = shift_series(s_x, s_y, self._shift) ymax = 1.25 + max(max(s_x.values), max(s_y.values)) + 1 ymin = min(min(s_x.values), min(s_y.values)) - 1 ylim = [ymin, ymax] ylim = [-1, 3] axis = layout.get_axis(plot_index) axis.plot(s_x.index, s_x.values) axis.plot(s_y.index, s_y.values) axis.set_ylim(ylim) plot_index += 1 layout.finish(plot_index)
def plot(self, level, per_line=3): """Temporary function to plot data. Expected to be implemented in plotter :param level: Topological Level (level in :mod:`trappy.stats.Topology`) :type level: str :param per_line: Number of plots per line :type per_line: int """ from trappy.plotter.PlotLayout import PlotLayout num_plots = self._first_agg.topology.level_span(level) result_1 = self._first_agg.aggregate(level=level, **self._agg_kwargs) result_2 = self._second_agg.aggregate(level=level, **self._agg_kwargs) layout = PlotLayout(per_line, num_plots) plot_index = 0 for group_id, result_group in enumerate(result_1): s_x = result_group s_y = result_2[group_id] s_x = self._resample(s_x) s_y = self._resample(s_y) s_x, s_y = shift_series(s_x, s_y, self._shift) ymax = 1.25 + max(max(s_x.values), max(s_y.values)) + 1 ymin = min(min(s_x.values), min(s_y.values)) - 1 ylim = [ymin, ymax] ylim = [-1, 3] axis = layout.get_axis(plot_index) axis.plot(s_x.index, s_x.values) axis.plot(s_y.index, s_y.values) axis.set_ylim(ylim) plot_index += 1 layout.finish(plot_index)
def _resolve(self, permute, concat): """Determine what data to plot on which axis""" pivot_vals, len_pivots = self.c_mgr.generate_pivots(permute) pivot_vals = list(pivot_vals) num_of_axes = len(self.c_mgr) if concat else len_pivots # Create a 2D Layout self._layout = PlotLayout(self._attr["per_line"], num_of_axes, width=self._attr["width"], length=self._attr["length"], title=self._attr['title']) self._fig = self._layout.get_fig() # Determine what constraint to plot and the corresponding pivot value if permute: legend_len = self.c_mgr._max_len pivots = [y for _, y in pivot_vals] c_dict = {c: str(c) for c in self.c_mgr} c_list = sorted(c_dict.items(), key=lambda x: (x[1].split(":")[-1], x[1].split(":")[0])) constraints = [c[0] for c in c_list] cp_pairs = [(c, p) for c in constraints for p in sorted(set(pivots))] else: legend_len = len_pivots if concat else len(self.c_mgr) pivots = pivot_vals cp_pairs = [(c, p) for c in self.c_mgr for p in pivots if p in c.result] # Initialise legend data and colormap self._attr["_legend_handles"] = [None] * legend_len self._attr["_legend_labels"] = [None] * legend_len if "colors" in self._attr: self._cmap = ColorMap.rgb_cmap(self._attr["colors"]) else: self._cmap = ColorMap(legend_len) # Group constraints/series with the axis they are to be plotted on figure_data = ddict(list) for i, (constraint, pivot) in enumerate(cp_pairs): axis = self._layout.get_axis( constraint.trace_index if concat else i) figure_data[axis].append((constraint, pivot)) # Plot each axis for axis, series_list in figure_data.items(): self.plot_axis(axis, series_list, permute, self._attr["concat"], self._attr["args_to_forward"]) if self._attr["xlim"]: axis.set_xlim(self._attr["xlim"]) if self._attr["ylim"]: axis.set_ylim(self._attr["ylim"]) # Show legend legend = self._fig.legend(self._attr["_legend_handles"], self._attr["_legend_labels"], loc='lower center', ncol=self._attr["legend_ncol"], borderaxespad=0.) legend.get_frame().set_facecolor('#F4F4F4') self._layout.finish(num_of_axes)
def _plot_concat(self): """Plot all lines on a single figure""" pivot_vals, len_pivots = self.c_mgr.generate_pivots() cmap = ColorMap(len_pivots) self._layout = PlotLayout(self._attr["per_line"], len(self.c_mgr), width=self._attr["width"], length=self._attr["length"]) self._fig = self._layout.get_fig() legend = [None] * len_pivots legend_str = [""] * len_pivots plot_index = 0 for constraint in self.c_mgr: result = constraint.result title = str(constraint) result = constraint.result pivot_index = 0 for pivot in pivot_vals: if pivot in result: axis = self._layout.get_axis(plot_index) line_2d_list = axis.plot(result[pivot].index, result[pivot].values, color=cmap.cmap(pivot_index), **self._attr["args_to_forward"]) if self._attr["xlim"] != None: axis.set_xlim(self._attr["xlim"]) if self._attr["ylim"] != None: axis.set_ylim(self._attr["ylim"]) legend[pivot_index] = line_2d_list[0] if self._attr["fill"]: drawstyle = line_2d_list[0].get_drawstyle() if drawstyle.startswith("steps"): # This has been fixed in upstream matplotlib raise UserWarning( "matplotlib does not support fill for step plots" ) xdat, ydat = line_2d_list[0].get_data(orig=False) axis.fill_between(xdat, axis.get_ylim()[0], ydat, facecolor=cmap.cmap(pivot_index), alpha=AttrConf.ALPHA) if pivot == AttrConf.PIVOT_VAL: legend_str[pivot_index] = self._attr["column"] else: legend_str[pivot_index] = "{0}: {1}".format( self._attr["pivot"], pivot) else: axis = self._layout.get_axis(plot_index) axis.plot([], [], color=cmap.cmap(pivot_index), **self._attr["args_to_forward"]) pivot_index += 1 plot_index += 1 self._fig.legend(legend, legend_str) plot_index = 0 for constraint in self.c_mgr: self._layout.get_axis(plot_index).set_title(str(constraint)) plot_index += 1 self._layout.finish(len(self.c_mgr))
def _plot(self, permute): """Internal Method called to draw the plot""" pivot_vals, len_pivots = self.c_mgr.generate_pivots(permute) # Create a 2D Layout self._layout = PlotLayout(self._attr["per_line"], len_pivots, width=self._attr["width"], length=self._attr["length"]) self._fig = self._layout.get_fig() legend_str = [] plot_index = 0 if permute: legend = [None] * self.c_mgr._max_len cmap = ColorMap(self.c_mgr._max_len) else: legend = [None] * len(self.c_mgr) cmap = ColorMap(len(self.c_mgr)) for p_val in pivot_vals: l_index = 0 for constraint in self.c_mgr: if permute: run_idx, pivot = p_val if constraint.run_index != run_idx: continue legend_str.append(constraint._column) l_index = self.c_mgr.get_column_index(constraint) title = constraint.get_data_name() + ":" else: pivot = p_val legend_str.append(str(constraint)) title = "" result = constraint.result if pivot in result: axis = self._layout.get_axis(plot_index) line_2d_list = axis.plot(result[pivot].index, result[pivot].values, color=cmap.cmap(l_index), **self._attr["args_to_forward"]) if self._attr["fill"]: drawstyle = line_2d_list[0].get_drawstyle() # This has been fixed in upstream matplotlib if drawstyle.startswith("steps"): raise UserWarning( "matplotlib does not support fill for step plots" ) xdat, ydat = line_2d_list[0].get_data(orig=False) axis.fill_between(xdat, axis.get_ylim()[0], ydat, facecolor=cmap.cmap(l_index), alpha=AttrConf.ALPHA) legend[l_index] = line_2d_list[0] if self._attr["xlim"] != None: axis.set_xlim(self._attr["xlim"]) if self._attr["ylim"] != None: axis.set_ylim(self._attr["ylim"]) else: axis = self._layout.get_axis(plot_index) axis.plot([], [], **self._attr["args_to_forward"]) l_index += 1 if pivot == AttrConf.PIVOT_VAL: title += ",".join(self._attr["column"]) else: title += "{0}: {1}".format(self._attr["pivot"], pivot) axis.set_title(title) plot_index += 1 for l_idx, legend_line in enumerate(legend): if not legend_line: del legend[l_idx] del legend_str[l_idx] self._fig.legend(legend, legend_str) self._layout.finish(len_pivots)