barchart.py (1881:fc205a7edd58) barchart.py (2006:3ca085495c69)
1# Copyright (c) 2005 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

--- 14 unchanged lines hidden (view full) ---

23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28# Lisa Hsu
29
30import matplotlib, pylab
1# Copyright (c) 2005 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

--- 14 unchanged lines hidden (view full) ---

23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28# Lisa Hsu
29
30import matplotlib, pylab
31from matplotlib.font_manager import FontProperties
31from matplotlib.numerix import array, arange, reshape, shape, transpose, zeros
32from matplotlib.numerix import Float
33
34matplotlib.interactive(False)
35
32from matplotlib.numerix import array, arange, reshape, shape, transpose, zeros
33from matplotlib.numerix import Float
34
35matplotlib.interactive(False)
36
36class BarChart(object):
37 def __init__(self, **kwargs):
38 self.init(**kwargs)
37from chart import ChartOptions
39
38
40 def init(self, **kwargs):
41 self.colormap = 'jet'
39class BarChart(ChartOptions):
40 def __init__(self, default=None, **kwargs):
41 super(BarChart, self).__init__(default, **kwargs)
42 self.inputdata = None
43 self.chartdata = None
42 self.inputdata = None
43 self.chartdata = None
44 self.xlabel = None
45 self.ylabel = None
46 self.legend = None
47 self.xticks = None
48 self.yticks = None
49 self.title = None
50
44
51 for key,value in kwargs.iteritems():
52 self.__setattr__(key, value)
53
54 def gen_colors(self, count):
55 cmap = matplotlib.cm.get_cmap(self.colormap)
56 if count == 1:
57 return cmap([ 0.5 ])
58 else:
59 return cmap(arange(count) / float(count - 1))
60
61 # The input data format does not match the data format that the

--- 62 unchanged lines hidden (view full) ---

124 #
125 # This code deals with one of the dimensions in the matrix being
126 # one wide.
127 #
128 def graph(self):
129 if self.chartdata is None:
130 raise AttributeError, "Data not set for bar chart!"
131
45 def gen_colors(self, count):
46 cmap = matplotlib.cm.get_cmap(self.colormap)
47 if count == 1:
48 return cmap([ 0.5 ])
49 else:
50 return cmap(arange(count) / float(count - 1))
51
52 # The input data format does not match the data format that the

--- 62 unchanged lines hidden (view full) ---

115 #
116 # This code deals with one of the dimensions in the matrix being
117 # one wide.
118 #
119 def graph(self):
120 if self.chartdata is None:
121 raise AttributeError, "Data not set for bar chart!"
122
132 self.figure = pylab.figure()
133 self.axes = self.figure.add_subplot(111)
123 self.figure = pylab.figure(figsize=self.chart_size)
124 self.axes = self.figure.add_axes(self.figure_size)
134
135 dim = len(shape(self.inputdata))
136 cshape = shape(self.chartdata)
137 if dim == 1:
138 colors = self.gen_colors(cshape[2])
139 colors = [ [ colors ] * cshape[1] ] * cshape[0]
140
141 if dim == 2:

--- 11 unchanged lines hidden (view full) ---

153 width = 1.0 / ( bars_in_group + 1)
154 center = width / 2
155 else:
156 width = .8 / bars_in_group
157 center = .1
158
159 bars = []
160 for i,stackdata in enumerate(self.chartdata):
125
126 dim = len(shape(self.inputdata))
127 cshape = shape(self.chartdata)
128 if dim == 1:
129 colors = self.gen_colors(cshape[2])
130 colors = [ [ colors ] * cshape[1] ] * cshape[0]
131
132 if dim == 2:

--- 11 unchanged lines hidden (view full) ---

144 width = 1.0 / ( bars_in_group + 1)
145 center = width / 2
146 else:
147 width = .8 / bars_in_group
148 center = .1
149
150 bars = []
151 for i,stackdata in enumerate(self.chartdata):
161 bottom = array([0] * len(stackdata[0]))
152 bottom = array([0.0] * len(stackdata[0]), Float)
162 stack = []
163 for j,bardata in enumerate(stackdata):
164 bardata = array(bardata)
165 ind = arange(len(bardata)) + i * width + center
166 bar = self.axes.bar(ind, bardata, width, bottom=bottom,
167 color=colors[i][j])
168 stack.append(bar)
169 bottom += bardata

--- 6 unchanged lines hidden (view full) ---

176 self.axes.set_ylabel(self.ylabel)
177
178 if self.yticks is not None:
179 ymin, ymax = self.axes.get_ylim()
180 nticks = float(len(self.yticks))
181 ticks = arange(nticks) / (nticks - 1) * (ymax - ymin) + ymin
182 self.axes.set_yticks(ticks)
183 self.axes.set_yticklabels(self.yticks)
153 stack = []
154 for j,bardata in enumerate(stackdata):
155 bardata = array(bardata)
156 ind = arange(len(bardata)) + i * width + center
157 bar = self.axes.bar(ind, bardata, width, bottom=bottom,
158 color=colors[i][j])
159 stack.append(bar)
160 bottom += bardata

--- 6 unchanged lines hidden (view full) ---

167 self.axes.set_ylabel(self.ylabel)
168
169 if self.yticks is not None:
170 ymin, ymax = self.axes.get_ylim()
171 nticks = float(len(self.yticks))
172 ticks = arange(nticks) / (nticks - 1) * (ymax - ymin) + ymin
173 self.axes.set_yticks(ticks)
174 self.axes.set_yticklabels(self.yticks)
175 elif self.ylim is not None:
176 self.axes.set_ylim(self.ylim)
184
185 if self.xticks is not None:
186 self.axes.set_xticks(arange(cshape[2]) + .5)
187 self.axes.set_xticklabels(self.xticks)
188
189 if self.legend is not None:
190 if dim == 1:
191 lbars = bars[0][0]
192 if dim == 2:
193 lbars = [ bars[i][0][0] for i in xrange(len(bars))]
194 if dim == 3:
195 number = len(bars[0])
196 lbars = [ bars[0][number - j - 1][0] for j in xrange(number)]
197
177
178 if self.xticks is not None:
179 self.axes.set_xticks(arange(cshape[2]) + .5)
180 self.axes.set_xticklabels(self.xticks)
181
182 if self.legend is not None:
183 if dim == 1:
184 lbars = bars[0][0]
185 if dim == 2:
186 lbars = [ bars[i][0][0] for i in xrange(len(bars))]
187 if dim == 3:
188 number = len(bars[0])
189 lbars = [ bars[0][number - j - 1][0] for j in xrange(number)]
190
198 self.axes.legend(lbars, self.legend, loc='best')
191 self.figure.legend(lbars, self.legend, self.legend_loc,
192 prop=FontProperties(size=self.legend_size))
199
200 if self.title is not None:
201 self.axes.set_title(self.title)
202
203 def savefig(self, name):
204 self.figure.savefig(name)
205
193
194 if self.title is not None:
195 self.axes.set_title(self.title)
196
197 def savefig(self, name):
198 self.figure.savefig(name)
199
200 def savecsv(self, name):
201 f = file(name, 'w')
202 data = array(self.inputdata)
203 dim = len(data.shape)
204
205 if dim == 1:
206 #if self.xlabel:
207 # f.write(', '.join(list(self.xlabel)) + '\n')
208 f.write(', '.join([ '%f' % val for val in data]) + '\n')
209 if dim == 2:
210 #if self.xlabel:
211 # f.write(', '.join([''] + list(self.xlabel)) + '\n')
212 for i,row in enumerate(data):
213 ylabel = []
214 #if self.ylabel:
215 # ylabel = [ self.ylabel[i] ]
216 f.write(', '.join(ylabel + [ '%f' % val for val in row]) + '\n')
217 if dim == 3:
218 f.write("don't do 3D csv files\n")
219 pass
220
221 f.close()
222
223
206if __name__ == '__main__':
224if __name__ == '__main__':
225 from random import randrange
207 import random, sys
208
209 dim = 3
210 number = 5
211
212 args = sys.argv[1:]
213 if len(args) > 3:
214 sys.exit("invalid number of arguments")

--- 14 unchanged lines hidden (view full) ---

229 chart1.data = data
230
231 chart1.xlabel = 'Benchmark'
232 chart1.ylabel = 'Bandwidth (GBps)'
233 chart1.legend = [ 'x%d' % x for x in xrange(myshape[-1]) ]
234 chart1.xticks = [ 'xtick%d' % x for x in xrange(myshape[0]) ]
235 chart1.title = 'this is the title'
236 chart1.graph()
226 import random, sys
227
228 dim = 3
229 number = 5
230
231 args = sys.argv[1:]
232 if len(args) > 3:
233 sys.exit("invalid number of arguments")

--- 14 unchanged lines hidden (view full) ---

248 chart1.data = data
249
250 chart1.xlabel = 'Benchmark'
251 chart1.ylabel = 'Bandwidth (GBps)'
252 chart1.legend = [ 'x%d' % x for x in xrange(myshape[-1]) ]
253 chart1.xticks = [ 'xtick%d' % x for x in xrange(myshape[0]) ]
254 chart1.title = 'this is the title'
255 chart1.graph()
237 #chart1.savefig('/tmp/test1.png')
256 chart1.savefig('/tmp/test1.png')
257 chart1.savefig('/tmp/test1.ps')
258 chart1.savefig('/tmp/test1.eps')
259 chart1.savecsv('/tmp/test1.csv')
238
239 if False:
240 chart2 = BarChart()
241 chart2.data = data
242 chart2.colormap = 'gray'
243 chart2.graph()
260
261 if False:
262 chart2 = BarChart()
263 chart2.data = data
264 chart2.colormap = 'gray'
265 chart2.graph()
244 #chart2.savefig('/tmp/test2.png')
266 chart2.savefig('/tmp/test2.png')
267 chart2.savefig('/tmp/test2.ps')
245
268
246 pylab.show()
269 #pylab.show()