1 # Copyright 2016-2017 ARM Limited 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 """ 16 This class sublclasses :mod:`trappy.plotter.StaticPlot.StaticPlot` to 17 implement a bar plot. 18 19 """ 20 import numpy as np 21 from trappy.plotter.StaticPlot import StaticPlot 22 23 class BarPlot(StaticPlot): 24 """BarPlot can plot data as vertical bars 25 26 Values are plotted against their position in the list of data. 27 28 :param traces: The input data 29 :type traces: A single instance or a list of :mod:`trappy.trace.FTrace`, 30 :mod:`trappy.trace.SysTrace`, :mod:`trappy.trace.BareTrace` or 31 :mod:`pandas.DataFrame`. 32 33 :param column: specifies the name of the column to be plotted. 34 :type column: str or list(str) 35 36 :param templates: TRAPpy events 37 38 .. note:: 39 40 This is not required if a :mod:`pandas.DataFrame` is 41 used 42 43 :type templates: :mod:`trappy.base.Base` 44 45 :param signals: A string of the type event_name:column 46 to indicate the value that needs to be plotted 47 48 .. note:: 49 50 - Only one of `signals` or both `templates` and 51 `columns` should be specified 52 - Signals format won't work for :mod:`pandas.DataFrame` 53 input 54 55 :type signals: str or list(string) 56 57 :param title: A title describing the generated plots 58 :type title: str 59 60 :param stacked: The series are grouped by default. If you want a 61 stacked plot, set stacked to True. 62 :type stacked: bool 63 64 :param spacing: A proportion of the size of each group which 65 should be used as the spacing between the groups. e.g. 0.2 66 (default) means that 1/5 of the groups total width is used as 67 a spacing between groups. 68 :type spacing: float 69 """ 70 71 def __init__(self, traces, templates=None, **kwargs): 72 # Default keys, each can be overridden in kwargs 73 74 super(BarPlot, self).__init__( 75 traces=traces, 76 templates=templates, 77 **kwargs) 78 79 def set_defaults(self): 80 """Sets the default attrs""" 81 super(BarPlot, self).set_defaults() 82 self._attr["spacing"] = 0.2 83 self._attr["stacked"] = False 84 85 def plot_axis(self, axis, series_list, permute, concat, args_to_forward): 86 """Internal Method called to plot data (series_list) on a given axis""" 87 stacked = self._attr["stacked"] 88 #Figure out how many bars per group 89 bars_in_group = 1 if stacked else len(series_list) 90 91 #Get the width of a group 92 group_width = 1.0 - self._attr["spacing"] 93 bar_width = group_width / bars_in_group 94 95 #Keep a list of the tops of bars to plot stacks 96 #Start with a list of 0s to put the first bars at the bottom 97 value_list = [c.result[p].values for (c, p) in series_list] 98 end_of_previous = [0] * max(len(x) for x in value_list) 99 100 for i, (constraint, pivot) in enumerate(series_list): 101 result = constraint.result 102 bar_anchor = np.arange(len(result[pivot].values)) 103 if not stacked: 104 bar_anchor = bar_anchor + i * bar_width 105 106 line_2d_list = axis.bar( 107 bar_anchor, 108 result[pivot].values, 109 bottom=end_of_previous, 110 width=bar_width, 111 color=self._cmap.cmap(i), 112 **args_to_forward 113 ) 114 115 if stacked: 116 end_of_previous = [x + y for (x, y) in zip(end_of_previous, result[pivot].values)] 117 118 axis.set_title(self.make_title(constraint, pivot, permute, concat)) 119 120 self.add_to_legend(i, line_2d_list[0], constraint, pivot, concat, permute) 121