Add basic bar chart support
This commit is contained in:
parent
37546c5021
commit
88cd5c32e6
1 changed files with 41 additions and 4 deletions
45
smem
45
smem
|
|
@ -365,8 +365,6 @@ def showtable(rows, fields, columns, sort):
|
||||||
|
|
||||||
if options.pie:
|
if options.pie:
|
||||||
columns.append(options.pie)
|
columns.append(options.pie)
|
||||||
if options.bar:
|
|
||||||
columns.append(options.bar)
|
|
||||||
|
|
||||||
for n in columns:
|
for n in columns:
|
||||||
if n not in fields:
|
if n not in fields:
|
||||||
|
|
@ -392,6 +390,9 @@ def showtable(rows, fields, columns, sort):
|
||||||
if options.pie:
|
if options.pie:
|
||||||
showpie(l, sort)
|
showpie(l, sort)
|
||||||
return
|
return
|
||||||
|
elif options.bar:
|
||||||
|
showbar(l, columns, sort)
|
||||||
|
return
|
||||||
|
|
||||||
if not options.no_header:
|
if not options.no_header:
|
||||||
print header
|
print header
|
||||||
|
|
@ -413,14 +414,15 @@ def showtable(rows, fields, columns, sort):
|
||||||
print format % tuple([f(v) for f,v in zip(formatter, t)])
|
print format % tuple([f(v) for f,v in zip(formatter, t)])
|
||||||
|
|
||||||
def showpie(l, sort):
|
def showpie(l, sort):
|
||||||
if (l[0][0] < l[-1][0]):
|
|
||||||
l.reverse()
|
|
||||||
try:
|
try:
|
||||||
import pylab
|
import pylab
|
||||||
except ImportError:
|
except ImportError:
|
||||||
sys.stderr.write("pie chart requires matplotlib\n")
|
sys.stderr.write("pie chart requires matplotlib\n")
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|
||||||
|
if (l[0][0] < l[-1][0]):
|
||||||
|
l.reverse()
|
||||||
|
|
||||||
labels = [r[1][-1] for r in l]
|
labels = [r[1][-1] for r in l]
|
||||||
values = [r[0] for r in l] # sort field
|
values = [r[0] for r in l] # sort field
|
||||||
|
|
||||||
|
|
@ -448,6 +450,41 @@ def showpie(l, sort):
|
||||||
pylab.title('%s by %s' % (options.pie, sort))
|
pylab.title('%s by %s' % (options.pie, sort))
|
||||||
pylab.show()
|
pylab.show()
|
||||||
|
|
||||||
|
def showbar(l, columns, sort):
|
||||||
|
try:
|
||||||
|
import pylab, numpy
|
||||||
|
except ImportError:
|
||||||
|
sys.stderr.write("bar chart requires matplotlib\n")
|
||||||
|
sys.exit(-1)
|
||||||
|
|
||||||
|
if (l[0][0] < l[-1][0]):
|
||||||
|
l.reverse()
|
||||||
|
|
||||||
|
rc = []
|
||||||
|
key = []
|
||||||
|
for n in range(len(columns)):
|
||||||
|
try:
|
||||||
|
if columns[n] in 'pid user group'.split():
|
||||||
|
continue
|
||||||
|
float(l[0][1][n])
|
||||||
|
rc.append(n)
|
||||||
|
key.append(columns[n])
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
width = 1.0 / (len(rc) + 1)
|
||||||
|
offset = width / 2
|
||||||
|
|
||||||
|
pl = []
|
||||||
|
ind = numpy.arange(len(l))
|
||||||
|
for n in xrange(len(rc)):
|
||||||
|
pl.append(pylab.bar(ind + offset + width * n,
|
||||||
|
[x[1][rc[n]] for x in l], width))
|
||||||
|
|
||||||
|
#plt.xticks(ind + .5, )
|
||||||
|
pylab.legend([p[0] for p in pl], key)
|
||||||
|
pylab.show()
|
||||||
|
|
||||||
parser = optparse.OptionParser("%prog [options]")
|
parser = optparse.OptionParser("%prog [options]")
|
||||||
parser.add_option("-H", "--no-header", action="store_true",
|
parser.add_option("-H", "--no-header", action="store_true",
|
||||||
help="disable header line")
|
help="disable header line")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue