barchart.py (2006:3ca085495c69) barchart.py (2115:beeeb8bb7550)
1# Copyright (c) 2005 The Regents of The University of Michigan
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
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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
32from matplotlib.numerix import array, arange, reshape, shape, transpose, zeros
33from matplotlib.numerix import Float
34
35matplotlib.interactive(False)
36
37from chart import ChartOptions
38
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
44
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
53 # graph function takes because it is intuitive. The conversion
54 # from input data format to chart data format depends on the
55 # dimensionality of the input data. Check here for the
56 # dimensionality and correctness of the input data
57 def set_data(self, data):
58 if data is None:
59 self.inputdata = None
60 self.chartdata = None
61 return
62
63 data = array(data)
64 dim = len(shape(data))
65 if dim not in (1, 2, 3):
66 raise AttributeError, "Input data must be a 1, 2, or 3d matrix"
67 self.inputdata = data
68
69 # If the input data is a 1d matrix, then it describes a
70 # standard bar chart.
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))
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
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
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
32from matplotlib.numerix import array, arange, reshape, shape, transpose, zeros
33from matplotlib.numerix import Float
34
35matplotlib.interactive(False)
36
37from chart import ChartOptions
38
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
44
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
53 # graph function takes because it is intuitive. The conversion
54 # from input data format to chart data format depends on the
55 # dimensionality of the input data. Check here for the
56 # dimensionality and correctness of the input data
57 def set_data(self, data):
58 if data is None:
59 self.inputdata = None
60 self.chartdata = None
61 return
62
63 data = array(data)
64 dim = len(shape(data))
65 if dim not in (1, 2, 3):
66 raise AttributeError, "Input data must be a 1, 2, or 3d matrix"
67 self.inputdata = data
68
69 # If the input data is a 1d matrix, then it describes a
70 # standard bar chart.
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)
79
80 # If the input data is a 3d matrix, then it describes an array
81 # of groups of bars with each bar being an array of stacked
82 # values.
83 if dim == 3:
84 self.chartdata = transpose(data, axes=(1,2,0))
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)
85
86 def get_data(self):
87 return self.inputdata
88
89 data = property(get_data, set_data)
90
91 # Graph the chart data.
92 # Input is a 3d matrix that describes a plot that has multiple
93 # groups, multiple bars in each group, and multiple values stacked
94 # in each bar. The underlying bar() function expects a sequence of
95 # bars in the same stack location and same group location, so the
96 # organization of the matrix is that the inner most sequence
97 # represents one of these bar groups, then those are grouped
98 # together to make one full stack of bars in each group, and then
99 # the outer most layer describes the groups. Here is an example
100 # data set and how it gets plotted as a result.
101 #
102 # e.g. data = [[[10,11,12], [13,14,15], [16,17,18], [19,20,21]],
103 # [[22,23,24], [25,26,27], [28,29,30], [31,32,33]]]
104 #
105 # will plot like this:
106 #
107 # 19 31 20 32 21 33
108 # 16 28 17 29 18 30
109 # 13 25 14 26 15 27
110 # 10 22 11 23 12 24
111 #
112 # Because this arrangement is rather conterintuitive, the rearrange
113 # function takes various matricies and arranges them to fit this
114 # profile.
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
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
95 # groups, multiple bars in each group, and multiple values stacked
96 # in each bar. The underlying bar() function expects a sequence of
97 # bars in the same stack location and same group location, so the
98 # organization of the matrix is that the inner most sequence
99 # represents one of these bar groups, then those are grouped
100 # together to make one full stack of bars in each group, and then
101 # the outer most layer describes the groups. Here is an example
102 # data set and how it gets plotted as a result.
103 #
104 # e.g. data = [[[10,11,12], [13,14,15], [16,17,18], [19,20,21]],
105 # [[22,23,24], [25,26,27], [28,29,30], [31,32,33]]]
106 #
107 # will plot like this:
108 #
109 # 19 31 20 32 21 33
110 # 16 28 17 29 18 30
111 # 13 25 14 26 15 27
112 # 10 22 11 23 12 24
113 #
114 # Because this arrangement is rather conterintuitive, the rearrange
115 # function takes various matricies and arranges them to fit this
116 # profile.
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
123 self.figure = pylab.figure(figsize=self.chart_size)
124 self.axes = self.figure.add_axes(self.figure_size)
125 need_subticks = True
125
126 dim = len(shape(self.inputdata))
127 cshape = shape(self.chartdata)
126
127 dim = len(shape(self.inputdata))
128 cshape = shape(self.chartdata)
129 print cshape
128 if dim == 1:
129 colors = self.gen_colors(cshape[2])
130 colors = [ [ colors ] * cshape[1] ] * cshape[0]
130 if dim == 1:
131 colors = self.gen_colors(cshape[2])
132 colors = [ [ colors ] * cshape[1] ] * cshape[0]
133 need_subticks = False
131
132 if dim == 2:
133 colors = self.gen_colors(cshape[0])
134 colors = [ [ [ c ] * cshape[2] ] * cshape[1] for c in colors ]
135
136 if dim == 3:
137 colors = self.gen_colors(cshape[1])
138 colors = [ [ [ c ] * cshape[2] for c in colors ] ] * cshape[0]
139
140 colors = array(colors)
141
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
142 bars_in_group = len(self.chartdata)
143 if bars_in_group < 5:
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):
152 bottom = array([0.0] * len(stackdata[0]), Float)
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])
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)
159 stack.append(bar)
160 bottom += bardata
161 bars.append(stack)
162
163 if self.xlabel is not None:
185 stack.append(bar)
186 bottom += bardata
187 bars.append(stack)
188
189 if self.xlabel is not None:
164 self.axes.set_xlabel(self.xlabel)
190 outer_axes.set_xlabel(self.xlabel)
165
166 if self.ylabel is not None:
191
192 if self.ylabel is not None:
167 self.axes.set_ylabel(self.ylabel)
193 inner_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
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
173 self.axes.set_yticks(ticks)
174 self.axes.set_yticklabels(self.yticks)
199 inner_axes.set_yticks(ticks)
200 inner_axes.set_yticklabels(self.yticks)
175 elif self.ylim is not None:
201 elif self.ylim is not None:
176 self.axes.set_ylim(self.ylim)
202 self.inner_axes.set_ylim(self.ylim)
177
178 if self.xticks is not None:
203
204 if self.xticks is not None:
179 self.axes.set_xticks(arange(cshape[2]) + .5)
180 self.axes.set_xticklabels(self.xticks)
181
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)
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
191 self.figure.legend(lbars, self.legend, self.legend_loc,
192 prop=FontProperties(size=self.legend_size))
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
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
223
224if __name__ == '__main__':
225 from random import randrange
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")
234 elif len(args) > 0:
235 myshape = [ int(x) for x in args ]
236 else:
237 myshape = [ 3, 4, 8 ]
238
239 # generate a data matrix of the given shape
240 size = reduce(lambda x,y: x*y, myshape)
241 #data = [ random.randrange(size - i) + 10 for i in xrange(size) ]
242 data = [ float(i)/100.0 for i in xrange(size) ]
243 data = reshape(data, myshape)
244
245 # setup some test bar charts
246 if True:
247 chart1 = BarChart()
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'
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 chart1.xsubticks = [ '%d' % x for x in xrange(myshape[1]) ]
255 chart1.graph()
256 chart1.savefig('/tmp/test1.png')
257 chart1.savefig('/tmp/test1.ps')
258 chart1.savefig('/tmp/test1.eps')
259 chart1.savecsv('/tmp/test1.csv')
260
261 if False:
262 chart2 = BarChart()
263 chart2.data = data
264 chart2.colormap = 'gray'
265 chart2.graph()
266 chart2.savefig('/tmp/test2.png')
267 chart2.savefig('/tmp/test2.ps')
268
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
269 #pylab.show()
299 pylab.myshow()