1# Copyright (c) 2005-2006 The Regents of The University of Michigan |
2# All rights reserved. 3# 4# Redistribution and use in source and binary forms, with or without 5# modification, are permitted provided that the following conditions are 6# met: redistributions of source code must retain the above copyright 7# notice, this list of conditions and the following disclaimer; 8# redistributions in binary form must reproduce the above copyright 9# notice, this list of conditions and the following disclaimer in the --- 61 unchanged lines hidden (view full) --- 71 if dim == 1: 72 self.chartdata = array([[data]]) 73 74 # If the input data is a 2d matrix, then it describes a bar 75 # chart with groups. The matrix being an array of groups of 76 # bars. 77 if dim == 2: 78 self.chartdata = transpose([data], axes=(2,0,1)) |
79 print shape(self.chartdata) |
80 81 # If the input data is a 3d matrix, then it describes an array 82 # of groups of bars with each bar being an array of stacked 83 # values. 84 if dim == 3: 85 self.chartdata = transpose(data, axes=(1,2,0)) |
86 print shape(self.chartdata) |
87 88 def get_data(self): 89 return self.inputdata 90 91 data = property(get_data, set_data) 92 93 # Graph the chart data. 94 # Input is a 3d matrix that describes a plot that has multiple --- 22 unchanged lines hidden (view full) --- 117 # 118 # 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)] --- 25 unchanged lines hidden (view full) --- 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:] --- 15 unchanged lines hidden (view full) --- 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 chart1.xsubticks = [ '%d' % x for x in xrange(myshape[1]) ] |
285 chart1.graph() 286 chart1.savefig('/tmp/test1.png') 287 chart1.savefig('/tmp/test1.ps') 288 chart1.savefig('/tmp/test1.eps') 289 chart1.savecsv('/tmp/test1.csv') 290 291 if False: 292 chart2 = BarChart() 293 chart2.data = data 294 chart2.colormap = 'gray' 295 chart2.graph() 296 chart2.savefig('/tmp/test2.png') 297 chart2.savefig('/tmp/test2.ps') 298 |
299 pylab.myshow() |