def set_data(self, data): if data is None: self.inputdata = None self.chartdata = None return data = array(data) dim = len(shape(data)) if dim not in (1, 2, 3): raise AttributeError, "Input data must be a 1, 2, or 3d matrix" self.inputdata = data # If the input data is a 1d matrix, then it describes a # standard bar chart. if dim == 1: self.chartdata = array([[data]]) # If the input data is a 2d matrix, then it describes a bar # chart with groups. The matrix being an array of groups of # bars. if dim == 2: self.chartdata = transpose([data], axes=(2, 0, 1)) # If the input data is a 3d matrix, then it describes an array # of groups of bars with each bar being an array of stacked # values. if dim == 3: self.chartdata = transpose(data, axes=(1, 2, 0))
def _edges(self, X, Y): """ Return the separator line segments; helper for _add_solids. """ N = nx.shape(X)[0] # Using the non-array form of these line segments is much # simpler than making them into arrays. if self.orientation == "vertical": return [zip(X[i], Y[i]) for i in range(1, N - 1)] else: return [zip(Y[i], X[i]) for i in range(1, N - 1)]
def _edges(self, X, Y): ''' Return the separator line segments; helper for _add_solids. ''' N = nx.shape(X)[0] # Using the non-array form of these line segments is much # simpler than making them into arrays. if self.orientation == 'vertical': return [zip(X[i], Y[i]) for i in range(1, N - 1)] else: return [zip(Y[i], X[i]) for i in range(1, N - 1)]
def _outline(self, X, Y): """ Return x, y arrays of colorbar bounding polygon, taking orientation into account. """ N = nx.shape(X)[0] ii = [0, 1, N - 2, N - 1, 2 * N - 1, 2 * N - 2, N + 1, N, 0] x = nx.take(nx.ravel(nx.transpose(X)), ii) y = nx.take(nx.ravel(nx.transpose(Y)), ii) if self.orientation == "horizontal": return y, x return x, y
def _outline(self, X, Y): ''' Return x, y arrays of colorbar bounding polygon, taking orientation into account. ''' N = nx.shape(X)[0] ii = [0, 1, N - 2, N - 1, 2 * N - 1, 2 * N - 2, N + 1, N, 0] x = nx.take(nx.ravel(nx.transpose(X)), ii) y = nx.take(nx.ravel(nx.transpose(Y)), ii) if self.orientation == 'horizontal': return y, x return x, y
def _parse_args(self, *args): X, Y, U, V, C = [None]*5 args = list(args) if len(args) == 3 or len(args) == 5: C = nx.ravel(args.pop(-1)) #print 'in parse_args, C:', C V = nx.ma.asarray(args.pop(-1)) U = nx.ma.asarray(args.pop(-1)) nn = nx.shape(U) nc = nn[0] nr = 1 if len(nn) > 1: nr = nn[1] if len(args) == 2: X, Y = [nx.ravel(a) for a in args] if len(X) == nc and len(Y) == nr: X, Y = [nx.ravel(a) for a in meshgrid(X, Y)] else: X, Y = [nx.ravel(a) for a in meshgrid(nx.arange(nc), nx.arange(nr))] return X, Y, U, V, C
def set_err(self, err): if err is None: self.inputerr = None self.charterr = None return err = array(err) dim = len(shape(err)) if dim not in (1, 2, 3): raise AttributeError, "Input err must be a 1, 2, or 3d matrix" self.inputerr = err if dim == 1: self.charterr = array([[err]]) if dim == 2: self.charterr = transpose([err], axes=(2, 0, 1)) if dim == 3: self.charterr = transpose(err, axes=(1, 2, 0))
def graph(self): if self.chartdata is None: raise AttributeError, "Data not set for bar chart!" dim = len(shape(self.inputdata)) cshape = shape(self.chartdata) if self.charterr is not None and shape(self.charterr) != cshape: raise AttributeError, 'Dimensions of error and data do not match' if dim == 1: colors = self.gen_colors(cshape[2]) colors = [[colors] * cshape[1]] * cshape[0] if dim == 2: colors = self.gen_colors(cshape[0]) colors = [[[c] * cshape[2]] * cshape[1] for c in colors] if dim == 3: colors = self.gen_colors(cshape[1]) colors = [[[c] * cshape[2] for c in colors]] * cshape[0] colors = array(colors) self.figure = pylab.figure(figsize=self.chart_size) outer_axes = None inner_axes = None if self.xsubticks is not None: color = self.figure.get_facecolor() self.metaaxes = self.figure.add_axes(self.figure_size, axisbg=color, frameon=False) for tick in self.metaaxes.xaxis.majorTicks: tick.tick1On = False tick.tick2On = False self.metaaxes.set_yticklabels([]) self.metaaxes.set_yticks([]) size = [0] * 4 size[0] = self.figure_size[0] size[1] = self.figure_size[1] + .12 size[2] = self.figure_size[2] size[3] = self.figure_size[3] - .12 self.axes = self.figure.add_axes(size) outer_axes = self.metaaxes inner_axes = self.axes else: self.axes = self.figure.add_axes(self.figure_size) outer_axes = self.axes inner_axes = self.axes bars_in_group = len(self.chartdata) width = 1.0 / (bars_in_group + 1) center = width / 2 bars = [] for i, stackdata in enumerate(self.chartdata): bottom = array([0.0] * len(stackdata[0]), Float) stack = [] for j, bardata in enumerate(stackdata): bardata = array(bardata) ind = arange(len(bardata)) + i * width + center yerr = None if self.charterr is not None: yerr = self.charterr[i][j] bar = self.axes.bar(ind, bardata, width, bottom=bottom, color=colors[i][j], yerr=yerr) if self.xsubticks is not None: self.metaaxes.bar(ind, [0] * len(bardata), width) stack.append(bar) bottom += bardata bars.append(stack) if self.xlabel is not None: outer_axes.set_xlabel(self.xlabel) if self.ylabel is not None: inner_axes.set_ylabel(self.ylabel) if self.yticks is not None: ymin, ymax = self.axes.get_ylim() nticks = float(len(self.yticks)) ticks = arange(nticks) / (nticks - 1) * (ymax - ymin) + ymin inner_axes.set_yticks(ticks) inner_axes.set_yticklabels(self.yticks) elif self.ylim is not None: inner_axes.set_ylim(self.ylim) if self.xticks is not None: outer_axes.set_xticks(arange(cshape[2]) + .5) outer_axes.set_xticklabels(self.xticks) if self.xsubticks is not None: numticks = (cshape[0] + 1) * cshape[2] inner_axes.set_xticks(arange(numticks) * width + 2 * center) xsubticks = list(self.xsubticks) + [''] inner_axes.set_xticklabels(xsubticks * cshape[2], fontsize=7, rotation=30) if self.legend is not None: if dim == 1: lbars = bars[0][0] if dim == 2: lbars = [bars[i][0][0] for i in xrange(len(bars))] if dim == 3: number = len(bars[0]) lbars = [bars[0][number - j - 1][0] for j in xrange(number)] if self.fig_legend: self.figure.legend(lbars, self.legend, self.legend_loc, prop=FontProperties(size=self.legend_size)) else: self.axes.legend(lbars, self.legend, self.legend_loc, prop=FontProperties(size=self.legend_size)) if self.title is not None: self.axes.set_title(self.title)
def graph(self): if self.chartdata is None: raise AttributeError, "Data not set for bar chart!" dim = len(shape(self.inputdata)) cshape = shape(self.chartdata) if self.charterr is not None and shape(self.charterr) != cshape: raise AttributeError, "Dimensions of error and data do not match" if dim == 1: colors = self.gen_colors(cshape[2]) colors = [[colors] * cshape[1]] * cshape[0] if dim == 2: colors = self.gen_colors(cshape[0]) colors = [[[c] * cshape[2]] * cshape[1] for c in colors] if dim == 3: colors = self.gen_colors(cshape[1]) colors = [[[c] * cshape[2] for c in colors]] * cshape[0] colors = array(colors) self.figure = pylab.figure(figsize=self.chart_size) outer_axes = None inner_axes = None if self.xsubticks is not None: color = self.figure.get_facecolor() self.metaaxes = self.figure.add_axes(self.figure_size, axisbg=color, frameon=False) for tick in self.metaaxes.xaxis.majorTicks: tick.tick1On = False tick.tick2On = False self.metaaxes.set_yticklabels([]) self.metaaxes.set_yticks([]) size = [0] * 4 size[0] = self.figure_size[0] size[1] = self.figure_size[1] + 0.12 size[2] = self.figure_size[2] size[3] = self.figure_size[3] - 0.12 self.axes = self.figure.add_axes(size) outer_axes = self.metaaxes inner_axes = self.axes else: self.axes = self.figure.add_axes(self.figure_size) outer_axes = self.axes inner_axes = self.axes bars_in_group = len(self.chartdata) width = 1.0 / (bars_in_group + 1) center = width / 2 bars = [] for i, stackdata in enumerate(self.chartdata): bottom = array([0.0] * len(stackdata[0]), Float) stack = [] for j, bardata in enumerate(stackdata): bardata = array(bardata) ind = arange(len(bardata)) + i * width + center yerr = None if self.charterr is not None: yerr = self.charterr[i][j] bar = self.axes.bar(ind, bardata, width, bottom=bottom, color=colors[i][j], yerr=yerr) if self.xsubticks is not None: self.metaaxes.bar(ind, [0] * len(bardata), width) stack.append(bar) bottom += bardata bars.append(stack) if self.xlabel is not None: outer_axes.set_xlabel(self.xlabel) if self.ylabel is not None: inner_axes.set_ylabel(self.ylabel) if self.yticks is not None: ymin, ymax = self.axes.get_ylim() nticks = float(len(self.yticks)) ticks = arange(nticks) / (nticks - 1) * (ymax - ymin) + ymin inner_axes.set_yticks(ticks) inner_axes.set_yticklabels(self.yticks) elif self.ylim is not None: inner_axes.set_ylim(self.ylim) if self.xticks is not None: outer_axes.set_xticks(arange(cshape[2]) + 0.5) outer_axes.set_xticklabels(self.xticks) if self.xsubticks is not None: numticks = (cshape[0] + 1) * cshape[2] inner_axes.set_xticks(arange(numticks) * width + 2 * center) xsubticks = list(self.xsubticks) + [""] inner_axes.set_xticklabels(xsubticks * cshape[2], fontsize=7, rotation=30) if self.legend is not None: if dim == 1: lbars = bars[0][0] if dim == 2: lbars = [bars[i][0][0] for i in xrange(len(bars))] if dim == 3: number = len(bars[0]) lbars = [bars[0][number - j - 1][0] for j in xrange(number)] if self.fig_legend: self.figure.legend(lbars, self.legend, self.legend_loc, prop=FontProperties(size=self.legend_size)) else: self.axes.legend(lbars, self.legend, self.legend_loc, prop=FontProperties(size=self.legend_size)) if self.title is not None: self.axes.set_title(self.title)