barchart.py revision 2117
16899SN/A# Copyright (c) 2005-2006 The Regents of The University of Michigan 26899SN/A# All rights reserved. 37553SN/A# 46899SN/A# Redistribution and use in source and binary forms, with or without 56899SN/A# modification, are permitted provided that the following conditions are 66899SN/A# met: redistributions of source code must retain the above copyright 76899SN/A# notice, this list of conditions and the following disclaimer; 86899SN/A# redistributions in binary form must reproduce the above copyright 96899SN/A# notice, this list of conditions and the following disclaimer in the 106899SN/A# documentation and/or other materials provided with the distribution; 116899SN/A# neither the name of the copyright holders nor the names of its 126899SN/A# contributors may be used to endorse or promote products derived from 136899SN/A# this software without specific prior written permission. 146899SN/A# 156899SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 166899SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 176899SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 186899SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 196899SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 206899SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 216899SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 226899SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 236899SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 246899SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 256899SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 266899SN/A# 276899SN/A# Authors: Nathan Binkert 286899SN/A# Lisa Hsu 296899SN/A 307553SN/Aimport matplotlib, pylab 317553SN/Afrom matplotlib.font_manager import FontProperties 326899SN/Afrom matplotlib.numerix import array, arange, reshape, shape, transpose, zeros 337055SN/Afrom matplotlib.numerix import Float 348229Snate@binkert.org 357454SN/Amatplotlib.interactive(False) 367055SN/A 377053SN/Afrom chart import ChartOptions 387053SN/A 3911017Snilay@cs.wisc.educlass BarChart(ChartOptions): 408229Snate@binkert.org def __init__(self, default=None, **kwargs): 418229Snate@binkert.org super(BarChart, self).__init__(default, **kwargs) 427553SN/A self.inputdata = None 436899SN/A self.chartdata = None 447553SN/A 457553SN/A def gen_colors(self, count): 467553SN/A cmap = matplotlib.cm.get_cmap(self.colormap) 476899SN/A if count == 1: 487053SN/A return cmap([ 0.5 ]) 498922Swilliam.wang@arm.com else: 507053SN/A return cmap(arange(count) / float(count - 1)) 517053SN/A 527553SN/A # The input data format does not match the data format that the 536899SN/A # graph function takes because it is intuitive. The conversion 547053SN/A # from input data format to chart data format depends on the 558854Sandreas.hansson@arm.com # dimensionality of the input data. Check here for the 569031Sandreas.hansson@arm.com # dimensionality and correctness of the input data 578965Sandreas.hansson@arm.com def set_data(self, data): 587053SN/A if data is None: 596899SN/A self.inputdata = None 607053SN/A self.chartdata = None 618975Sandreas.hansson@arm.com return 6210713Sandreas.hansson@arm.com 638922Swilliam.wang@arm.com data = array(data) 647053SN/A dim = len(shape(data)) 656899SN/A if dim not in (1, 2, 3): 667553SN/A raise AttributeError, "Input data must be a 1, 2, or 3d matrix" 677553SN/A self.inputdata = data 687553SN/A 696899SN/A # If the input data is a 1d matrix, then it describes a 709294Sandreas.hansson@arm.com # standard bar chart. 719294Sandreas.hansson@arm.com if dim == 1: 726899SN/A self.chartdata = array([[data]]) 738922Swilliam.wang@arm.com 746899SN/A # If the input data is a 2d matrix, then it describes a bar 757053SN/A # chart with groups. The matrix being an array of groups of 766899SN/A # bars. 777053SN/A if dim == 2: 786899SN/A self.chartdata = transpose([data], axes=(2,0,1)) 797553SN/A print shape(self.chartdata) 806899SN/A 817055SN/A # If the input data is a 3d matrix, then it describes an array 827053SN/A # of groups of bars with each bar being an array of stacked 837055SN/A # values. 846899SN/A if dim == 3: 857055SN/A self.chartdata = transpose(data, axes=(1,2,0)) 866899SN/A print shape(self.chartdata) 877053SN/A 887553SN/A def get_data(self): 897053SN/A return self.inputdata 907053SN/A 917553SN/A data = property(get_data, set_data) 926899SN/A 937053SN/A # Graph the chart data. 947553SN/A # Input is a 3d matrix that describes a plot that has multiple 957053SN/A # groups, multiple bars in each group, and multiple values stacked 967053SN/A # in each bar. The underlying bar() function expects a sequence of 977053SN/A # bars in the same stack location and same group location, so the 987553SN/A # organization of the matrix is that the inner most sequence 997053SN/A # represents one of these bar groups, then those are grouped 1007053SN/A # together to make one full stack of bars in each group, and then 1017553SN/A # the outer most layer describes the groups. Here is an example 1027053SN/A # data set and how it gets plotted as a result. 1036899SN/A # 1047553SN/A # e.g. data = [[[10,11,12], [13,14,15], [16,17,18], [19,20,21]], 1056899SN/A # [[22,23,24], [25,26,27], [28,29,30], [31,32,33]]] 1067053SN/A # 1076899SN/A # will plot like this: 1087053SN/A # 1097553SN/A # 19 31 20 32 21 33 1107553SN/A # 16 28 17 29 18 30 1116899SN/A # 13 25 14 26 15 27 11211061Snilay@cs.wisc.edu # 10 22 11 23 12 24 1138950Sandreas.hansson@arm.com # 11411061Snilay@cs.wisc.edu # Because this arrangement is rather conterintuitive, the rearrange 1157553SN/A # function takes various matricies and arranges them to fit this 1166899SN/A # profile. 1176899SN/A # 1187553SN/A # This code deals with one of the dimensions in the matrix being 119 # one wide. 120 # 121 def graph(self): 122 if self.chartdata is None: 123 raise AttributeError, "Data not set for bar chart!" 124 125 need_subticks = True 126 127 dim = len(shape(self.inputdata)) 128 cshape = shape(self.chartdata) 129 print cshape 130 if dim == 1: 131 colors = self.gen_colors(cshape[2]) 132 colors = [ [ colors ] * cshape[1] ] * cshape[0] 133 need_subticks = False 134 135 if dim == 2: 136 colors = self.gen_colors(cshape[0]) 137 colors = [ [ [ c ] * cshape[2] ] * cshape[1] for c in colors ] 138 139 if dim == 3: 140 colors = self.gen_colors(cshape[1]) 141 colors = [ [ [ c ] * cshape[2] for c in colors ] ] * cshape[0] 142 143 colors = array(colors) 144 145 self.figure = pylab.figure(figsize=self.chart_size) 146 147 outer_axes = None 148 inner_axes = None 149 if need_subticks: 150 self.metaaxes = self.figure.add_axes(self.figure_size) 151 self.metaaxes.set_yticklabels([]) 152 self.metaaxes.set_yticks([]) 153 size = [0] * 4 154 size[0] = self.figure_size[0] 155 size[1] = self.figure_size[1] + .075 156 size[2] = self.figure_size[2] 157 size[3] = self.figure_size[3] - .075 158 self.axes = self.figure.add_axes(size) 159 outer_axes = self.metaaxes 160 inner_axes = self.axes 161 else: 162 self.axes = self.figure.add_axes(self.figure_size) 163 outer_axes = self.axes 164 inner_axes = self.axes 165 166 bars_in_group = len(self.chartdata) 167 if bars_in_group < 5: 168 width = 1.0 / ( bars_in_group + 1) 169 center = width / 2 170 else: 171 width = .8 / bars_in_group 172 center = .1 173 174 bars = [] 175 for i,stackdata in enumerate(self.chartdata): 176 bottom = array([0.0] * len(stackdata[0]), Float) 177 stack = [] 178 for j,bardata in enumerate(stackdata): 179 bardata = array(bardata) 180 ind = arange(len(bardata)) + i * width + center 181 bar = self.axes.bar(ind, bardata, width, bottom=bottom, 182 color=colors[i][j]) 183 if dim != 1: 184 self.metaaxes.bar(ind, [0] * len(bardata), width) 185 stack.append(bar) 186 bottom += bardata 187 bars.append(stack) 188 189 if self.xlabel is not None: 190 outer_axes.set_xlabel(self.xlabel) 191 192 if self.ylabel is not None: 193 inner_axes.set_ylabel(self.ylabel) 194 195 if self.yticks is not None: 196 ymin, ymax = self.axes.get_ylim() 197 nticks = float(len(self.yticks)) 198 ticks = arange(nticks) / (nticks - 1) * (ymax - ymin) + ymin 199 inner_axes.set_yticks(ticks) 200 inner_axes.set_yticklabels(self.yticks) 201 elif self.ylim is not None: 202 self.inner_axes.set_ylim(self.ylim) 203 204 if self.xticks is not None: 205 outer_axes.set_xticks(arange(cshape[2]) + .5) 206 outer_axes.set_xticklabels(self.xticks) 207 if self.xsubticks is not None: 208 inner_axes.set_xticks(arange((cshape[0] + 1)*cshape[2])*width + 2*center) 209 self.xsubticks.append('') 210 inner_axes.set_xticklabels(self.xsubticks * cshape[0], fontsize=8) 211 if self.legend is not None: 212 if dim == 1: 213 lbars = bars[0][0] 214 if dim == 2: 215 lbars = [ bars[i][0][0] for i in xrange(len(bars))] 216 if dim == 3: 217 number = len(bars[0]) 218 lbars = [ bars[0][number - j - 1][0] for j in xrange(number)] 219 220 self.figure.legend(lbars, self.legend, self.legend_loc, 221 prop=FontProperties(size=self.legend_size)) 222 223 if self.title is not None: 224 self.axes.set_title(self.title) 225 226 def savefig(self, name): 227 self.figure.savefig(name) 228 229 def savecsv(self, name): 230 f = file(name, 'w') 231 data = array(self.inputdata) 232 dim = len(data.shape) 233 234 if dim == 1: 235 #if self.xlabel: 236 # f.write(', '.join(list(self.xlabel)) + '\n') 237 f.write(', '.join([ '%f' % val for val in data]) + '\n') 238 if dim == 2: 239 #if self.xlabel: 240 # f.write(', '.join([''] + list(self.xlabel)) + '\n') 241 for i,row in enumerate(data): 242 ylabel = [] 243 #if self.ylabel: 244 # ylabel = [ self.ylabel[i] ] 245 f.write(', '.join(ylabel + [ '%f' % val for val in row]) + '\n') 246 if dim == 3: 247 f.write("don't do 3D csv files\n") 248 pass 249 250 f.close() 251 252if __name__ == '__main__': 253 from random import randrange 254 import random, sys 255 256 dim = 3 257 number = 5 258 259 args = sys.argv[1:] 260 if len(args) > 3: 261 sys.exit("invalid number of arguments") 262 elif len(args) > 0: 263 myshape = [ int(x) for x in args ] 264 else: 265 myshape = [ 3, 4, 8 ] 266 267 # generate a data matrix of the given shape 268 size = reduce(lambda x,y: x*y, myshape) 269 #data = [ random.randrange(size - i) + 10 for i in xrange(size) ] 270 data = [ float(i)/100.0 for i in xrange(size) ] 271 data = reshape(data, myshape) 272 273 # setup some test bar charts 274 if True: 275 chart1 = BarChart() 276 chart1.data = data 277 278 chart1.xlabel = 'Benchmark' 279 chart1.ylabel = 'Bandwidth (GBps)' 280 chart1.legend = [ 'x%d' % x for x in xrange(myshape[-1]) ] 281 chart1.xticks = [ 'xtick%d' % x for x in xrange(myshape[0]) ] 282 chart1.title = 'this is the title' 283 chart1.figure_size = [0.1, 0.2, 0.7, 0.85 ] 284 if len(myshape) > 1: 285 chart1.xsubticks = [ '%d' % x for x in xrange(myshape[1]) ] 286 chart1.graph() 287 chart1.savefig('/tmp/test1.png') 288 chart1.savefig('/tmp/test1.ps') 289 chart1.savefig('/tmp/test1.eps') 290 chart1.savecsv('/tmp/test1.csv') 291 292 if False: 293 chart2 = BarChart() 294 chart2.data = data 295 chart2.colormap = 'gray' 296 chart2.graph() 297 chart2.savefig('/tmp/test2.png') 298 chart2.savefig('/tmp/test2.ps') 299 300 pylab.myshow() 301