Menu

[r142]: / trunk / htdocs / examples / table_demo.py  Maximize  Restore  History

Download this file

128 lines (104 with data), 3.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import matplotlib
#matplotlib.use('PS')
#import matplotlib.backends.backend_ps as backend_ps
#backend_ps.defaultPaperSize = 11,8.5
from RandomArray import exponential
from Numeric import sum
from matplotlib.matlab import *
from matplotlib.table import Table
from matplotlib.cell import Cell
from matplotlib.backend_bases import arg_to_rgb
axes([0.2, 0.3, 0.6, 0.5])
means = {
'Quake': 1e6,
'Wind': 7e5,
'Flood': 5e5,
'Freeze': 4e5,
'Hail': 3e5,
}
perils = means.keys()
perils.sort()
my_colours=(
[0,0,1],
[.5,0,1],
[1,0,1],
[1,0,.5],
[1,0,0])
def fix_colours(colours):
results = []
for colour in colours:
results.append(pastel(colour))
return results
def pastel(colour):
""" Convert colour into a nice pastel shade"""
weight = 2.4
rgb = asarray(arg_to_rgb(colour))
# scale colour
maxc = max(rgb)
if maxc < 1.0 and maxc > 0:
# scale colour
scale = 1.0 / maxc
rgb = rgb * scale
# now decrease saturation
total = sum(rgb)
slack = 0
for x in rgb:
slack += 1.0 - x
# want to increase weight from total to weight
# pick x s.t. slack * x == weight - total
# x = (weight - total) / slack
x = (weight - total) / slack
rgb = [c + (x * (1.0-c)) for c in rgb]
return rgb
my_colours = fix_colours(my_colours)
rows = len(my_colours)
plot_data = {}
for peril in perils:
data = exponential(means[peril], rows)
data = sort(data)
plot_data[peril] = data
N = len(perils)
yoff = array([0.0] * N)
ind = arange(N) # the x locations for the groups
ind = ind + .3
width = 0.4 # the width of the bars
labels = []
colours = []
# add the data to the plot
for row in xrange(rows):
data = [plot_data[peril][row] for peril in perils]
data = data - yoff
patches = bar(ind, data, width, bottom=yoff, color=my_colours[row])
yoff = data + yoff
labels.append(['%d' % (val / 1000,) for val in yoff])
colours.append(patches[0].get_facecolor())
labels.append(perils)
colours.reverse()
set(gca(), 'xticks', [])
set(gca(), 'yticklabels', [])
table = Table(gca(), loc='bottom')
labels.reverse()
width = 1.0 / N
height = table._approx_text_height()
loc='center'
pastels = colours
# add the table
for row, pastel in zip(range(len(labels)), ['w'] + pastels):
for col in xrange(N):
table.add_cell(row, col, width, height, text=labels[row][col],
loc=loc, facecolor=pastel)
loc='right'
# add row labels to the table
row_labels = []
for x in range(rows):
row_labels.append('%d year' % ((rows / (1.0 + x)) + 0.5,))
for colour, pastel, label, row in zip(colours, pastels,
row_labels, range(len(labels))):
table.add_cell(row+1, -1, width, height,
text=label, loc='left',
facecolor=pastel)
# set row label width auto-magically
table.auto_set_column_width(-1)
gca().add_table(table)
savefig('table2')
show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.