From ef14fd4ad31e5da21ed309770e384d82e0a47b10 Mon Sep 17 00:00:00 2001 From: Lisa Hsu Date: Sat, 18 Feb 2006 17:24:23 -0500 Subject: Now you can have sublabels for every bar using the self.xsubticklabels parameter. --HG-- extra : convert_revision : a6bdf3a972e81c84947b7d6ae76f828494a125c8 --- util/stats/barchart.py | 56 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 13 deletions(-) (limited to 'util/stats/barchart.py') diff --git a/util/stats/barchart.py b/util/stats/barchart.py index 19cccb58a..99f9b8f35 100644 --- a/util/stats/barchart.py +++ b/util/stats/barchart.py @@ -1,4 +1,4 @@ -# Copyright (c) 2005 The Regents of The University of Michigan +# Copyright (c) 2005-2006 The Regents of The University of Michigan # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -76,12 +76,14 @@ class BarChart(ChartOptions): # bars. if dim == 2: self.chartdata = transpose([data], axes=(2,0,1)) + print shape(self.chartdata) # 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)) + print shape(self.chartdata) def get_data(self): return self.inputdata @@ -120,14 +122,15 @@ class BarChart(ChartOptions): if self.chartdata is None: raise AttributeError, "Data not set for bar chart!" - self.figure = pylab.figure(figsize=self.chart_size) - self.axes = self.figure.add_axes(self.figure_size) + need_subticks = True dim = len(shape(self.inputdata)) cshape = shape(self.chartdata) + print cshape if dim == 1: colors = self.gen_colors(cshape[2]) colors = [ [ colors ] * cshape[1] ] * cshape[0] + need_subticks = False if dim == 2: colors = self.gen_colors(cshape[0]) @@ -139,6 +142,27 @@ class BarChart(ChartOptions): colors = array(colors) + self.figure = pylab.figure(figsize=self.chart_size) + + outer_axes = None + inner_axes = None + if need_subticks: + self.metaaxes = self.figure.add_axes(self.figure_size) + self.metaaxes.set_yticklabels([]) + self.metaaxes.set_yticks([]) + size = [0] * 4 + size[0] = self.figure_size[0] + size[1] = self.figure_size[1] + .075 + size[2] = self.figure_size[2] + size[3] = self.figure_size[3] - .075 + 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) if bars_in_group < 5: width = 1.0 / ( bars_in_group + 1) @@ -156,29 +180,34 @@ class BarChart(ChartOptions): ind = arange(len(bardata)) + i * width + center bar = self.axes.bar(ind, bardata, width, bottom=bottom, color=colors[i][j]) + if dim != 1: + self.metaaxes.bar(ind, [0] * len(bardata), width) stack.append(bar) bottom += bardata bars.append(stack) if self.xlabel is not None: - self.axes.set_xlabel(self.xlabel) + outer_axes.set_xlabel(self.xlabel) if self.ylabel is not None: - self.axes.set_ylabel(self.ylabel) + 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 - self.axes.set_yticks(ticks) - self.axes.set_yticklabels(self.yticks) + inner_axes.set_yticks(ticks) + inner_axes.set_yticklabels(self.yticks) elif self.ylim is not None: - self.axes.set_ylim(self.ylim) + self.inner_axes.set_ylim(self.ylim) if self.xticks is not None: - self.axes.set_xticks(arange(cshape[2]) + .5) - self.axes.set_xticklabels(self.xticks) - + outer_axes.set_xticks(arange(cshape[2]) + .5) + outer_axes.set_xticklabels(self.xticks) + if self.xsubticks is not None: + inner_axes.set_xticks(arange((cshape[0] + 1)*cshape[2])*width + 2*center) + self.xsubticks.append('') + inner_axes.set_xticklabels(self.xsubticks * cshape[0], fontsize=8) if self.legend is not None: if dim == 1: lbars = bars[0][0] @@ -220,7 +249,6 @@ class BarChart(ChartOptions): f.close() - if __name__ == '__main__': from random import randrange import random, sys @@ -252,6 +280,8 @@ if __name__ == '__main__': chart1.legend = [ 'x%d' % x for x in xrange(myshape[-1]) ] chart1.xticks = [ 'xtick%d' % x for x in xrange(myshape[0]) ] chart1.title = 'this is the title' + chart1.figure_size = [0.1, 0.2, 0.7, 0.85 ] + chart1.xsubticks = [ '%d' % x for x in xrange(myshape[1]) ] chart1.graph() chart1.savefig('/tmp/test1.png') chart1.savefig('/tmp/test1.ps') @@ -266,4 +296,4 @@ if __name__ == '__main__': chart2.savefig('/tmp/test2.png') chart2.savefig('/tmp/test2.ps') - #pylab.show() + pylab.myshow() -- cgit v1.2.3 From 71bf22165acb4a330a98e183f96188daf49d078f Mon Sep 17 00:00:00 2001 From: Lisa Hsu Date: Sat, 18 Feb 2006 17:29:43 -0500 Subject: more changes for subtick labels. util/stats/barchart.py: oop forgot this for 1D graph cases. util/stats/chart.py: need to add default param to chart. --HG-- extra : convert_revision : f4e6c6c614d584e7928ed905e97608716455ab6c --- util/stats/barchart.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'util/stats/barchart.py') diff --git a/util/stats/barchart.py b/util/stats/barchart.py index 99f9b8f35..ddb31be46 100644 --- a/util/stats/barchart.py +++ b/util/stats/barchart.py @@ -281,7 +281,8 @@ if __name__ == '__main__': chart1.xticks = [ 'xtick%d' % x for x in xrange(myshape[0]) ] chart1.title = 'this is the title' chart1.figure_size = [0.1, 0.2, 0.7, 0.85 ] - chart1.xsubticks = [ '%d' % x for x in xrange(myshape[1]) ] + if len(myshape) > 1: + chart1.xsubticks = [ '%d' % x for x in xrange(myshape[1]) ] chart1.graph() chart1.savefig('/tmp/test1.png') chart1.savefig('/tmp/test1.ps') -- cgit v1.2.3 From 6cf0ba84953b0c18fcd702197f3aec9cf8a33f2d Mon Sep 17 00:00:00 2001 From: Lisa Hsu Date: Sat, 18 Feb 2006 18:39:19 -0500 Subject: remove print statements --HG-- extra : convert_revision : abd635034424eeb9685aea777440a02887ce81a6 --- util/stats/barchart.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'util/stats/barchart.py') diff --git a/util/stats/barchart.py b/util/stats/barchart.py index ddb31be46..29d16db17 100644 --- a/util/stats/barchart.py +++ b/util/stats/barchart.py @@ -76,14 +76,12 @@ class BarChart(ChartOptions): # bars. if dim == 2: self.chartdata = transpose([data], axes=(2,0,1)) - print shape(self.chartdata) # 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)) - print shape(self.chartdata) def get_data(self): return self.inputdata @@ -207,7 +205,7 @@ class BarChart(ChartOptions): if self.xsubticks is not None: inner_axes.set_xticks(arange((cshape[0] + 1)*cshape[2])*width + 2*center) self.xsubticks.append('') - inner_axes.set_xticklabels(self.xsubticks * cshape[0], fontsize=8) + inner_axes.set_xticklabels(self.xsubticks * cshape[2], fontsize=8) if self.legend is not None: if dim == 1: lbars = bars[0][0] -- cgit v1.2.3 From a611b8100379e28728c1084801413954c973e734 Mon Sep 17 00:00:00 2001 From: Lisa Hsu Date: Sat, 18 Feb 2006 20:10:42 -0500 Subject: few changes for nate: 1) cosmetic - removing visibility of meta axes except for the tick labels. 2) unless subticklabels defined, don't do meta axes. (instead of assuming if you have 3D graph, do meta axes) --HG-- extra : convert_revision : 396011ffaa51ea4066b34257f6fd5b3faac9d242 --- util/stats/barchart.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'util/stats/barchart.py') diff --git a/util/stats/barchart.py b/util/stats/barchart.py index 29d16db17..3f202d9bf 100644 --- a/util/stats/barchart.py +++ b/util/stats/barchart.py @@ -31,6 +31,7 @@ import matplotlib, pylab from matplotlib.font_manager import FontProperties from matplotlib.numerix import array, arange, reshape, shape, transpose, zeros from matplotlib.numerix import Float +from matplotlib.ticker import NullLocator matplotlib.interactive(False) @@ -120,15 +121,11 @@ class BarChart(ChartOptions): if self.chartdata is None: raise AttributeError, "Data not set for bar chart!" - need_subticks = True - dim = len(shape(self.inputdata)) cshape = shape(self.chartdata) - print cshape if dim == 1: colors = self.gen_colors(cshape[2]) colors = [ [ colors ] * cshape[1] ] * cshape[0] - need_subticks = False if dim == 2: colors = self.gen_colors(cshape[0]) @@ -144,15 +141,19 @@ class BarChart(ChartOptions): outer_axes = None inner_axes = None - if need_subticks: - self.metaaxes = self.figure.add_axes(self.figure_size) + 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] + .075 + size[1] = self.figure_size[1] + .05 size[2] = self.figure_size[2] - size[3] = self.figure_size[3] - .075 + size[3] = self.figure_size[3] - .05 self.axes = self.figure.add_axes(size) outer_axes = self.metaaxes inner_axes = self.axes @@ -202,10 +203,12 @@ class BarChart(ChartOptions): 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: inner_axes.set_xticks(arange((cshape[0] + 1)*cshape[2])*width + 2*center) self.xsubticks.append('') inner_axes.set_xticklabels(self.xsubticks * cshape[2], fontsize=8) + if self.legend is not None: if dim == 1: lbars = bars[0][0] @@ -279,7 +282,7 @@ if __name__ == '__main__': chart1.xticks = [ 'xtick%d' % x for x in xrange(myshape[0]) ] chart1.title = 'this is the title' chart1.figure_size = [0.1, 0.2, 0.7, 0.85 ] - if len(myshape) > 1: + if len(myshape) > 2: chart1.xsubticks = [ '%d' % x for x in xrange(myshape[1]) ] chart1.graph() chart1.savefig('/tmp/test1.png') -- cgit v1.2.3 From fcb9718dcd45240f447c91cac02185ff7985b197 Mon Sep 17 00:00:00 2001 From: Lisa Hsu Date: Fri, 24 Feb 2006 18:08:14 -0500 Subject: 1) make it pretty for large clusters 2) make subticks vertical so they can be longer 3) make inner and outer axes farther apart to make room for subtick's vertical labels --HG-- extra : convert_revision : 91a1aab3f1078921edd53428e6712744210c9f1b --- util/stats/barchart.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'util/stats/barchart.py') diff --git a/util/stats/barchart.py b/util/stats/barchart.py index 3f202d9bf..dd3bf0180 100644 --- a/util/stats/barchart.py +++ b/util/stats/barchart.py @@ -151,9 +151,9 @@ class BarChart(ChartOptions): self.metaaxes.set_yticks([]) size = [0] * 4 size[0] = self.figure_size[0] - size[1] = self.figure_size[1] + .05 + size[1] = self.figure_size[1] + .12 size[2] = self.figure_size[2] - size[3] = self.figure_size[3] - .05 + size[3] = self.figure_size[3] - .12 self.axes = self.figure.add_axes(size) outer_axes = self.metaaxes inner_axes = self.axes @@ -163,12 +163,9 @@ class BarChart(ChartOptions): inner_axes = self.axes bars_in_group = len(self.chartdata) - if bars_in_group < 5: - width = 1.0 / ( bars_in_group + 1) - center = width / 2 - else: - width = .8 / bars_in_group - center = .1 + + width = 1.0 / ( bars_in_group + 1) + center = width / 2 bars = [] for i,stackdata in enumerate(self.chartdata): @@ -179,7 +176,7 @@ class BarChart(ChartOptions): ind = arange(len(bardata)) + i * width + center bar = self.axes.bar(ind, bardata, width, bottom=bottom, color=colors[i][j]) - if dim != 1: + if self.xsubticks is not None: self.metaaxes.bar(ind, [0] * len(bardata), width) stack.append(bar) bottom += bardata @@ -207,7 +204,7 @@ class BarChart(ChartOptions): if self.xsubticks is not None: inner_axes.set_xticks(arange((cshape[0] + 1)*cshape[2])*width + 2*center) self.xsubticks.append('') - inner_axes.set_xticklabels(self.xsubticks * cshape[2], fontsize=8) + inner_axes.set_xticklabels(self.xsubticks * cshape[2], fontsize=7, rotation=90) if self.legend is not None: if dim == 1: @@ -281,7 +278,6 @@ if __name__ == '__main__': chart1.legend = [ 'x%d' % x for x in xrange(myshape[-1]) ] chart1.xticks = [ 'xtick%d' % x for x in xrange(myshape[0]) ] chart1.title = 'this is the title' - chart1.figure_size = [0.1, 0.2, 0.7, 0.85 ] if len(myshape) > 2: chart1.xsubticks = [ '%d' % x for x in xrange(myshape[1]) ] chart1.graph() @@ -298,4 +294,4 @@ if __name__ == '__main__': chart2.savefig('/tmp/test2.png') chart2.savefig('/tmp/test2.ps') - pylab.myshow() +# pylab.show() -- cgit v1.2.3 From 46189e9e2b4d36cb2aa3587919cb4d532118fa9c Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Sat, 25 Feb 2006 23:48:13 -0500 Subject: better colors for barcharts util/stats/barchart.py: If there are fewer than 5 colors, pick from a subset of 5 so there is more consistency in colors between graphs --HG-- extra : convert_revision : 6cf64c2f8ed81e714e24a3ebe5a7a60ca168b231 --- util/stats/barchart.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'util/stats/barchart.py') diff --git a/util/stats/barchart.py b/util/stats/barchart.py index dd3bf0180..87d0eb5bb 100644 --- a/util/stats/barchart.py +++ b/util/stats/barchart.py @@ -47,8 +47,11 @@ class BarChart(ChartOptions): cmap = matplotlib.cm.get_cmap(self.colormap) if count == 1: return cmap([ 0.5 ]) - else: - return cmap(arange(count) / float(count - 1)) + + if count < 5: + return cmap(arange(5) / float(4))[:count] + + return cmap(arange(count) / float(count - 1)) # The input data format does not match the data format that the # graph function takes because it is intuitive. The conversion -- cgit v1.2.3 From cf3667a0e435fe707cf318caecc151c4b311b755 Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Sun, 26 Feb 2006 00:11:54 -0500 Subject: add error bars and more options for legend placement util/stats/barchart.py: Add support for error bars util/stats/barchart.py: add support to choose between a legend inside or outside the figure. --HG-- extra : convert_revision : 14273e385c106bf27a2013991f9f34ca6551b96c --- util/stats/barchart.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) (limited to 'util/stats/barchart.py') diff --git a/util/stats/barchart.py b/util/stats/barchart.py index 87d0eb5bb..f26a88d51 100644 --- a/util/stats/barchart.py +++ b/util/stats/barchart.py @@ -42,6 +42,8 @@ class BarChart(ChartOptions): super(BarChart, self).__init__(default, **kwargs) self.inputdata = None self.chartdata = None + self.inputerr = None + self.charterr = None def gen_colors(self, count): cmap = matplotlib.cm.get_cmap(self.colormap) @@ -92,6 +94,32 @@ class BarChart(ChartOptions): data = property(get_data, set_data) + 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 get_err(self): + return self.inputerr + + err = property(get_err, set_err) + # Graph the chart data. # Input is a 3d matrix that describes a plot that has multiple # groups, multiple bars in each group, and multiple values stacked @@ -126,6 +154,9 @@ class BarChart(ChartOptions): 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] @@ -177,8 +208,11 @@ class BarChart(ChartOptions): 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]) + color=colors[i][j], yerr=yerr) if self.xsubticks is not None: self.metaaxes.bar(ind, [0] * len(bardata), width) stack.append(bar) @@ -218,8 +252,12 @@ class BarChart(ChartOptions): number = len(bars[0]) lbars = [ bars[0][number - j - 1][0] for j in xrange(number)] - self.figure.legend(lbars, self.legend, self.legend_loc, - prop=FontProperties(size=self.legend_size)) + 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) -- cgit v1.2.3 From b7e4d16ea94bbfe49ac3baa5e73f04d44ee2f2f0 Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Sun, 26 Feb 2006 00:35:10 -0500 Subject: code cleanup util/stats/barchart.py: clean up some of lisa's messy code remove trailing whitespace while I'm at it. --HG-- extra : convert_revision : f2fe6777fb4b458fa1d5b5b743f6274014c229ad --- util/stats/barchart.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'util/stats/barchart.py') diff --git a/util/stats/barchart.py b/util/stats/barchart.py index f26a88d51..5d6dd0ab1 100644 --- a/util/stats/barchart.py +++ b/util/stats/barchart.py @@ -177,7 +177,8 @@ class BarChart(ChartOptions): 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) + 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 @@ -239,9 +240,11 @@ class BarChart(ChartOptions): outer_axes.set_xticklabels(self.xticks) if self.xsubticks is not None: - inner_axes.set_xticks(arange((cshape[0] + 1)*cshape[2])*width + 2*center) + numticks = (cshape[0] + 1) * cshape[2] + inner_axes.set_xticks(arange(numticks) * width + 2 * center) self.xsubticks.append('') - inner_axes.set_xticklabels(self.xsubticks * cshape[2], fontsize=7, rotation=90) + inner_axes.set_xticklabels(self.xsubticks * cshape[2], fontsize=7, + rotation=90) if self.legend is not None: if dim == 1: @@ -281,7 +284,7 @@ class BarChart(ChartOptions): ylabel = [] #if self.ylabel: # ylabel = [ self.ylabel[i] ] - f.write(', '.join(ylabel + [ '%f' % val for val in row]) + '\n') + f.write(', '.join(ylabel + [ '%f' % v for v in row]) + '\n') if dim == 3: f.write("don't do 3D csv files\n") pass -- cgit v1.2.3