barchart.py (2160:d851e177e4c6) barchart.py (2179:7e15ffdd03d8)
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
34from matplotlib.ticker import NullLocator
35
36matplotlib.interactive(False)
37
38from chart import ChartOptions
39
40class BarChart(ChartOptions):
41 def __init__(self, default=None, **kwargs):
42 super(BarChart, self).__init__(default, **kwargs)
43 self.inputdata = None
44 self.chartdata = None
45
46 def gen_colors(self, count):
47 cmap = matplotlib.cm.get_cmap(self.colormap)
48 if count == 1:
49 return cmap([ 0.5 ])
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
34from matplotlib.ticker import NullLocator
35
36matplotlib.interactive(False)
37
38from chart import ChartOptions
39
40class BarChart(ChartOptions):
41 def __init__(self, default=None, **kwargs):
42 super(BarChart, self).__init__(default, **kwargs)
43 self.inputdata = None
44 self.chartdata = None
45
46 def gen_colors(self, count):
47 cmap = matplotlib.cm.get_cmap(self.colormap)
48 if count == 1:
49 return cmap([ 0.5 ])
50 else:
51 return cmap(arange(count) / float(count - 1))
52
50
51 if count < 5:
52 return cmap(arange(5) / float(4))[:count]
53
54 return cmap(arange(count) / float(count - 1))
55
53 # The input data format does not match the data format that the
54 # graph function takes because it is intuitive. The conversion
55 # from input data format to chart data format depends on the
56 # dimensionality of the input data. Check here for the
57 # dimensionality and correctness of the input data
58 def set_data(self, data):
59 if data is None:
60 self.inputdata = None
61 self.chartdata = None
62 return
63
64 data = array(data)
65 dim = len(shape(data))
66 if dim not in (1, 2, 3):
67 raise AttributeError, "Input data must be a 1, 2, or 3d matrix"
68 self.inputdata = data
69
70 # If the input data is a 1d matrix, then it describes a
71 # standard bar chart.
72 if dim == 1:
73 self.chartdata = array([[data]])
74
75 # If the input data is a 2d matrix, then it describes a bar
76 # chart with groups. The matrix being an array of groups of
77 # bars.
78 if dim == 2:
79 self.chartdata = transpose([data], axes=(2,0,1))
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
87 def get_data(self):
88 return self.inputdata
89
90 data = property(get_data, set_data)
91
92 # Graph the chart data.
93 # Input is a 3d matrix that describes a plot that has multiple
94 # groups, multiple bars in each group, and multiple values stacked
95 # in each bar. The underlying bar() function expects a sequence of
96 # bars in the same stack location and same group location, so the
97 # organization of the matrix is that the inner most sequence
98 # represents one of these bar groups, then those are grouped
99 # together to make one full stack of bars in each group, and then
100 # the outer most layer describes the groups. Here is an example
101 # data set and how it gets plotted as a result.
102 #
103 # e.g. data = [[[10,11,12], [13,14,15], [16,17,18], [19,20,21]],
104 # [[22,23,24], [25,26,27], [28,29,30], [31,32,33]]]
105 #
106 # will plot like this:
107 #
108 # 19 31 20 32 21 33
109 # 16 28 17 29 18 30
110 # 13 25 14 26 15 27
111 # 10 22 11 23 12 24
112 #
113 # Because this arrangement is rather conterintuitive, the rearrange
114 # function takes various matricies and arranges them to fit this
115 # profile.
116 #
117 # This code deals with one of the dimensions in the matrix being
118 # one wide.
119 #
120 def graph(self):
121 if self.chartdata is None:
122 raise AttributeError, "Data not set for bar chart!"
123
124 dim = len(shape(self.inputdata))
125 cshape = shape(self.chartdata)
126 if dim == 1:
127 colors = self.gen_colors(cshape[2])
128 colors = [ [ colors ] * cshape[1] ] * cshape[0]
129
130 if dim == 2:
131 colors = self.gen_colors(cshape[0])
132 colors = [ [ [ c ] * cshape[2] ] * cshape[1] for c in colors ]
133
134 if dim == 3:
135 colors = self.gen_colors(cshape[1])
136 colors = [ [ [ c ] * cshape[2] for c in colors ] ] * cshape[0]
137
138 colors = array(colors)
139
140 self.figure = pylab.figure(figsize=self.chart_size)
141
142 outer_axes = None
143 inner_axes = None
144 if self.xsubticks is not None:
145 color = self.figure.get_facecolor()
146 self.metaaxes = self.figure.add_axes(self.figure_size, axisbg=color, frameon=False)
147 for tick in self.metaaxes.xaxis.majorTicks:
148 tick.tick1On = False
149 tick.tick2On = False
150 self.metaaxes.set_yticklabels([])
151 self.metaaxes.set_yticks([])
152 size = [0] * 4
153 size[0] = self.figure_size[0]
154 size[1] = self.figure_size[1] + .12
155 size[2] = self.figure_size[2]
156 size[3] = self.figure_size[3] - .12
157 self.axes = self.figure.add_axes(size)
158 outer_axes = self.metaaxes
159 inner_axes = self.axes
160 else:
161 self.axes = self.figure.add_axes(self.figure_size)
162 outer_axes = self.axes
163 inner_axes = self.axes
164
165 bars_in_group = len(self.chartdata)
166
167 width = 1.0 / ( bars_in_group + 1)
168 center = width / 2
169
170 bars = []
171 for i,stackdata in enumerate(self.chartdata):
172 bottom = array([0.0] * len(stackdata[0]), Float)
173 stack = []
174 for j,bardata in enumerate(stackdata):
175 bardata = array(bardata)
176 ind = arange(len(bardata)) + i * width + center
177 bar = self.axes.bar(ind, bardata, width, bottom=bottom,
178 color=colors[i][j])
179 if self.xsubticks is not None:
180 self.metaaxes.bar(ind, [0] * len(bardata), width)
181 stack.append(bar)
182 bottom += bardata
183 bars.append(stack)
184
185 if self.xlabel is not None:
186 outer_axes.set_xlabel(self.xlabel)
187
188 if self.ylabel is not None:
189 inner_axes.set_ylabel(self.ylabel)
190
191 if self.yticks is not None:
192 ymin, ymax = self.axes.get_ylim()
193 nticks = float(len(self.yticks))
194 ticks = arange(nticks) / (nticks - 1) * (ymax - ymin) + ymin
195 inner_axes.set_yticks(ticks)
196 inner_axes.set_yticklabels(self.yticks)
197 elif self.ylim is not None:
198 self.inner_axes.set_ylim(self.ylim)
199
200 if self.xticks is not None:
201 outer_axes.set_xticks(arange(cshape[2]) + .5)
202 outer_axes.set_xticklabels(self.xticks)
203
204 if self.xsubticks is not None:
205 inner_axes.set_xticks(arange((cshape[0] + 1)*cshape[2])*width + 2*center)
206 self.xsubticks.append('')
207 inner_axes.set_xticklabels(self.xsubticks * cshape[2], fontsize=7, rotation=90)
208
209 if self.legend is not None:
210 if dim == 1:
211 lbars = bars[0][0]
212 if dim == 2:
213 lbars = [ bars[i][0][0] for i in xrange(len(bars))]
214 if dim == 3:
215 number = len(bars[0])
216 lbars = [ bars[0][number - j - 1][0] for j in xrange(number)]
217
218 self.figure.legend(lbars, self.legend, self.legend_loc,
219 prop=FontProperties(size=self.legend_size))
220
221 if self.title is not None:
222 self.axes.set_title(self.title)
223
224 def savefig(self, name):
225 self.figure.savefig(name)
226
227 def savecsv(self, name):
228 f = file(name, 'w')
229 data = array(self.inputdata)
230 dim = len(data.shape)
231
232 if dim == 1:
233 #if self.xlabel:
234 # f.write(', '.join(list(self.xlabel)) + '\n')
235 f.write(', '.join([ '%f' % val for val in data]) + '\n')
236 if dim == 2:
237 #if self.xlabel:
238 # f.write(', '.join([''] + list(self.xlabel)) + '\n')
239 for i,row in enumerate(data):
240 ylabel = []
241 #if self.ylabel:
242 # ylabel = [ self.ylabel[i] ]
243 f.write(', '.join(ylabel + [ '%f' % val for val in row]) + '\n')
244 if dim == 3:
245 f.write("don't do 3D csv files\n")
246 pass
247
248 f.close()
249
250if __name__ == '__main__':
251 from random import randrange
252 import random, sys
253
254 dim = 3
255 number = 5
256
257 args = sys.argv[1:]
258 if len(args) > 3:
259 sys.exit("invalid number of arguments")
260 elif len(args) > 0:
261 myshape = [ int(x) for x in args ]
262 else:
263 myshape = [ 3, 4, 8 ]
264
265 # generate a data matrix of the given shape
266 size = reduce(lambda x,y: x*y, myshape)
267 #data = [ random.randrange(size - i) + 10 for i in xrange(size) ]
268 data = [ float(i)/100.0 for i in xrange(size) ]
269 data = reshape(data, myshape)
270
271 # setup some test bar charts
272 if True:
273 chart1 = BarChart()
274 chart1.data = data
275
276 chart1.xlabel = 'Benchmark'
277 chart1.ylabel = 'Bandwidth (GBps)'
278 chart1.legend = [ 'x%d' % x for x in xrange(myshape[-1]) ]
279 chart1.xticks = [ 'xtick%d' % x for x in xrange(myshape[0]) ]
280 chart1.title = 'this is the title'
281 if len(myshape) > 2:
282 chart1.xsubticks = [ '%d' % x for x in xrange(myshape[1]) ]
283 chart1.graph()
284 chart1.savefig('/tmp/test1.png')
285 chart1.savefig('/tmp/test1.ps')
286 chart1.savefig('/tmp/test1.eps')
287 chart1.savecsv('/tmp/test1.csv')
288
289 if False:
290 chart2 = BarChart()
291 chart2.data = data
292 chart2.colormap = 'gray'
293 chart2.graph()
294 chart2.savefig('/tmp/test2.png')
295 chart2.savefig('/tmp/test2.ps')
296
297# pylab.show()
56 # The input data format does not match the data format that the
57 # graph function takes because it is intuitive. The conversion
58 # from input data format to chart data format depends on the
59 # dimensionality of the input data. Check here for the
60 # dimensionality and correctness of the input data
61 def set_data(self, data):
62 if data is None:
63 self.inputdata = None
64 self.chartdata = None
65 return
66
67 data = array(data)
68 dim = len(shape(data))
69 if dim not in (1, 2, 3):
70 raise AttributeError, "Input data must be a 1, 2, or 3d matrix"
71 self.inputdata = data
72
73 # If the input data is a 1d matrix, then it describes a
74 # standard bar chart.
75 if dim == 1:
76 self.chartdata = array([[data]])
77
78 # If the input data is a 2d matrix, then it describes a bar
79 # chart with groups. The matrix being an array of groups of
80 # bars.
81 if dim == 2:
82 self.chartdata = transpose([data], axes=(2,0,1))
83
84 # If the input data is a 3d matrix, then it describes an array
85 # of groups of bars with each bar being an array of stacked
86 # values.
87 if dim == 3:
88 self.chartdata = transpose(data, axes=(1,2,0))
89
90 def get_data(self):
91 return self.inputdata
92
93 data = property(get_data, set_data)
94
95 # Graph the chart data.
96 # Input is a 3d matrix that describes a plot that has multiple
97 # groups, multiple bars in each group, and multiple values stacked
98 # in each bar. The underlying bar() function expects a sequence of
99 # bars in the same stack location and same group location, so the
100 # organization of the matrix is that the inner most sequence
101 # represents one of these bar groups, then those are grouped
102 # together to make one full stack of bars in each group, and then
103 # the outer most layer describes the groups. Here is an example
104 # data set and how it gets plotted as a result.
105 #
106 # e.g. data = [[[10,11,12], [13,14,15], [16,17,18], [19,20,21]],
107 # [[22,23,24], [25,26,27], [28,29,30], [31,32,33]]]
108 #
109 # will plot like this:
110 #
111 # 19 31 20 32 21 33
112 # 16 28 17 29 18 30
113 # 13 25 14 26 15 27
114 # 10 22 11 23 12 24
115 #
116 # Because this arrangement is rather conterintuitive, the rearrange
117 # function takes various matricies and arranges them to fit this
118 # profile.
119 #
120 # This code deals with one of the dimensions in the matrix being
121 # one wide.
122 #
123 def graph(self):
124 if self.chartdata is None:
125 raise AttributeError, "Data not set for bar chart!"
126
127 dim = len(shape(self.inputdata))
128 cshape = shape(self.chartdata)
129 if dim == 1:
130 colors = self.gen_colors(cshape[2])
131 colors = [ [ colors ] * cshape[1] ] * cshape[0]
132
133 if dim == 2:
134 colors = self.gen_colors(cshape[0])
135 colors = [ [ [ c ] * cshape[2] ] * cshape[1] for c in colors ]
136
137 if dim == 3:
138 colors = self.gen_colors(cshape[1])
139 colors = [ [ [ c ] * cshape[2] for c in colors ] ] * cshape[0]
140
141 colors = array(colors)
142
143 self.figure = pylab.figure(figsize=self.chart_size)
144
145 outer_axes = None
146 inner_axes = None
147 if self.xsubticks is not None:
148 color = self.figure.get_facecolor()
149 self.metaaxes = self.figure.add_axes(self.figure_size, axisbg=color, frameon=False)
150 for tick in self.metaaxes.xaxis.majorTicks:
151 tick.tick1On = False
152 tick.tick2On = False
153 self.metaaxes.set_yticklabels([])
154 self.metaaxes.set_yticks([])
155 size = [0] * 4
156 size[0] = self.figure_size[0]
157 size[1] = self.figure_size[1] + .12
158 size[2] = self.figure_size[2]
159 size[3] = self.figure_size[3] - .12
160 self.axes = self.figure.add_axes(size)
161 outer_axes = self.metaaxes
162 inner_axes = self.axes
163 else:
164 self.axes = self.figure.add_axes(self.figure_size)
165 outer_axes = self.axes
166 inner_axes = self.axes
167
168 bars_in_group = len(self.chartdata)
169
170 width = 1.0 / ( bars_in_group + 1)
171 center = width / 2
172
173 bars = []
174 for i,stackdata in enumerate(self.chartdata):
175 bottom = array([0.0] * len(stackdata[0]), Float)
176 stack = []
177 for j,bardata in enumerate(stackdata):
178 bardata = array(bardata)
179 ind = arange(len(bardata)) + i * width + center
180 bar = self.axes.bar(ind, bardata, width, bottom=bottom,
181 color=colors[i][j])
182 if self.xsubticks is not None:
183 self.metaaxes.bar(ind, [0] * len(bardata), width)
184 stack.append(bar)
185 bottom += bardata
186 bars.append(stack)
187
188 if self.xlabel is not None:
189 outer_axes.set_xlabel(self.xlabel)
190
191 if self.ylabel is not None:
192 inner_axes.set_ylabel(self.ylabel)
193
194 if self.yticks is not None:
195 ymin, ymax = self.axes.get_ylim()
196 nticks = float(len(self.yticks))
197 ticks = arange(nticks) / (nticks - 1) * (ymax - ymin) + ymin
198 inner_axes.set_yticks(ticks)
199 inner_axes.set_yticklabels(self.yticks)
200 elif self.ylim is not None:
201 self.inner_axes.set_ylim(self.ylim)
202
203 if self.xticks is not None:
204 outer_axes.set_xticks(arange(cshape[2]) + .5)
205 outer_axes.set_xticklabels(self.xticks)
206
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[2], fontsize=7, rotation=90)
211
212 if self.legend is not None:
213 if dim == 1:
214 lbars = bars[0][0]
215 if dim == 2:
216 lbars = [ bars[i][0][0] for i in xrange(len(bars))]
217 if dim == 3:
218 number = len(bars[0])
219 lbars = [ bars[0][number - j - 1][0] for j in xrange(number)]
220
221 self.figure.legend(lbars, self.legend, self.legend_loc,
222 prop=FontProperties(size=self.legend_size))
223
224 if self.title is not None:
225 self.axes.set_title(self.title)
226
227 def savefig(self, name):
228 self.figure.savefig(name)
229
230 def savecsv(self, name):
231 f = file(name, 'w')
232 data = array(self.inputdata)
233 dim = len(data.shape)
234
235 if dim == 1:
236 #if self.xlabel:
237 # f.write(', '.join(list(self.xlabel)) + '\n')
238 f.write(', '.join([ '%f' % val for val in data]) + '\n')
239 if dim == 2:
240 #if self.xlabel:
241 # f.write(', '.join([''] + list(self.xlabel)) + '\n')
242 for i,row in enumerate(data):
243 ylabel = []
244 #if self.ylabel:
245 # ylabel = [ self.ylabel[i] ]
246 f.write(', '.join(ylabel + [ '%f' % val for val in row]) + '\n')
247 if dim == 3:
248 f.write("don't do 3D csv files\n")
249 pass
250
251 f.close()
252
253if __name__ == '__main__':
254 from random import randrange
255 import random, sys
256
257 dim = 3
258 number = 5
259
260 args = sys.argv[1:]
261 if len(args) > 3:
262 sys.exit("invalid number of arguments")
263 elif len(args) > 0:
264 myshape = [ int(x) for x in args ]
265 else:
266 myshape = [ 3, 4, 8 ]
267
268 # generate a data matrix of the given shape
269 size = reduce(lambda x,y: x*y, myshape)
270 #data = [ random.randrange(size - i) + 10 for i in xrange(size) ]
271 data = [ float(i)/100.0 for i in xrange(size) ]
272 data = reshape(data, myshape)
273
274 # setup some test bar charts
275 if True:
276 chart1 = BarChart()
277 chart1.data = data
278
279 chart1.xlabel = 'Benchmark'
280 chart1.ylabel = 'Bandwidth (GBps)'
281 chart1.legend = [ 'x%d' % x for x in xrange(myshape[-1]) ]
282 chart1.xticks = [ 'xtick%d' % x for x in xrange(myshape[0]) ]
283 chart1.title = 'this is the title'
284 if len(myshape) > 2:
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.show()