barchart.py (2179:7e15ffdd03d8) | barchart.py (2180:44599b79ef80) |
---|---|
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 --- 28 unchanged lines hidden (view full) --- 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 | 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 --- 28 unchanged lines hidden (view full) --- 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 self.inputerr = None 46 self.charterr = 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 51 if count < 5: 52 return cmap(arange(5) / float(4))[:count] --- 34 unchanged lines hidden (view full) --- 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 | 47 48 def gen_colors(self, count): 49 cmap = matplotlib.cm.get_cmap(self.colormap) 50 if count == 1: 51 return cmap([ 0.5 ]) 52 53 if count < 5: 54 return cmap(arange(5) / float(4))[:count] --- 34 unchanged lines hidden (view full) --- 89 if dim == 3: 90 self.chartdata = transpose(data, axes=(1,2,0)) 91 92 def get_data(self): 93 return self.inputdata 94 95 data = property(get_data, set_data) 96 |
97 def set_err(self, err): 98 if err is None: 99 self.inputerr = None 100 self.charterr = None 101 return 102 103 err = array(err) 104 dim = len(shape(err)) 105 if dim not in (1, 2, 3): 106 raise AttributeError, "Input err must be a 1, 2, or 3d matrix" 107 self.inputerr = err 108 109 if dim == 1: 110 self.charterr = array([[err]]) 111 112 if dim == 2: 113 self.charterr = transpose([err], axes=(2,0,1)) 114 115 if dim == 3: 116 self.charterr = transpose(err, axes=(1,2,0)) 117 118 def get_err(self): 119 return self.inputerr 120 121 err = property(get_err, set_err) 122 |
|
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 --- 18 unchanged lines hidden (view full) --- 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) | 123 # Graph the chart data. 124 # Input is a 3d matrix that describes a plot that has multiple 125 # groups, multiple bars in each group, and multiple values stacked 126 # in each bar. The underlying bar() function expects a sequence of 127 # bars in the same stack location and same group location, so the 128 # organization of the matrix is that the inner most sequence 129 # represents one of these bar groups, then those are grouped 130 # together to make one full stack of bars in each group, and then --- 18 unchanged lines hidden (view full) --- 149 # one wide. 150 # 151 def graph(self): 152 if self.chartdata is None: 153 raise AttributeError, "Data not set for bar chart!" 154 155 dim = len(shape(self.inputdata)) 156 cshape = shape(self.chartdata) |
157 if self.charterr is not None and shape(self.charterr) != cshape: 158 raise AttributeError, 'Dimensions of error and data do not match' 159 |
|
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 --- 35 unchanged lines hidden (view full) --- 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 | 160 if dim == 1: 161 colors = self.gen_colors(cshape[2]) 162 colors = [ [ colors ] * cshape[1] ] * cshape[0] 163 164 if dim == 2: 165 colors = self.gen_colors(cshape[0]) 166 colors = [ [ [ c ] * cshape[2] ] * cshape[1] for c in colors ] 167 --- 35 unchanged lines hidden (view full) --- 203 204 bars = [] 205 for i,stackdata in enumerate(self.chartdata): 206 bottom = array([0.0] * len(stackdata[0]), Float) 207 stack = [] 208 for j,bardata in enumerate(stackdata): 209 bardata = array(bardata) 210 ind = arange(len(bardata)) + i * width + center |
211 yerr = None 212 if self.charterr is not None: 213 yerr = self.charterr[i][j] |
|
180 bar = self.axes.bar(ind, bardata, width, bottom=bottom, | 214 bar = self.axes.bar(ind, bardata, width, bottom=bottom, |
181 color=colors[i][j]) | 215 color=colors[i][j], yerr=yerr) |
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) --- 23 unchanged lines hidden (view full) --- 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 | 216 if self.xsubticks is not None: 217 self.metaaxes.bar(ind, [0] * len(bardata), width) 218 stack.append(bar) 219 bottom += bardata 220 bars.append(stack) 221 222 if self.xlabel is not None: 223 outer_axes.set_xlabel(self.xlabel) --- 23 unchanged lines hidden (view full) --- 247 if dim == 1: 248 lbars = bars[0][0] 249 if dim == 2: 250 lbars = [ bars[i][0][0] for i in xrange(len(bars))] 251 if dim == 3: 252 number = len(bars[0]) 253 lbars = [ bars[0][number - j - 1][0] for j in xrange(number)] 254 |
221 self.figure.legend(lbars, self.legend, self.legend_loc, 222 prop=FontProperties(size=self.legend_size)) | 255 if self.fig_legend: 256 self.figure.legend(lbars, self.legend, self.legend_loc, 257 prop=FontProperties(size=self.legend_size)) 258 else: 259 self.axes.legend(lbars, self.legend, self.legend_loc, 260 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): --- 70 unchanged lines hidden --- | 261 262 if self.title is not None: 263 self.axes.set_title(self.title) 264 265 def savefig(self, name): 266 self.figure.savefig(name) 267 268 def savecsv(self, name): --- 70 unchanged lines hidden --- |