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 Plot a 2D matrix (up to 100 x 100)
26 """
27
28 import numpy.oldnumeric as N
29
30 from Biskit import ColorSpectrum, EHandler
31 import Biskit.tools as T
32
33 T.tryImport( 'biggles', 'FramedPlot', namespace=globals() )
34 try:
35 import biggles
36 except:
37 biggles = False
38
40 """
41 Class to create a legend to use with a Matrix plot.
42 """
43
45 """
46 @param values: color mapping for each color used
47 @type values: [(float, int)]
48 """
49 if not biggles:
50 raise ImportError, 'biggles module could not be imported.'
51
52 FramedPlot.__init__(self)
53
54 values = N.array(values)
55
56 self.frame.draw_spine = 1
57
58 n_values = 4
59 step = len(values) / (n_values - 1) + 1
60
61 indices = range(0, len(values), step)
62 indices.append(len(values) - 1)
63
64 labels = ['%.1f' % values[i, 0] for i in indices]
65
66 self.y.ticks = len(labels)
67 self.y1.ticklabels = labels
68 self.y2.draw_ticks = 0
69 self.x.draw_ticks = 0
70 self.x.ticklabels = []
71
72 i = 2
73 x = (2, 3)
74
75 for value, color in values:
76
77 y1 = (i, i)
78 y2 = (i + 1, i + 1)
79
80 cell = biggles.FillBetween(x, y1, x, y2, color = int(color))
81 self.add(cell)
82
83 i += 1
84
85
87 """
88 Class to plot the values of a matix, the rows and the columns
89 will be plotted along the x- and y-axis, respectively. The value
90 of each cell will be illutrated using the selected color range.
91 """
92
93 - def __init__(self, matrix, mesh = 0, palette = "plasma", legend = 0):
94 """
95 @param matrix: the 2-D array to plot
96 @type matrix: array
97 @param mesh: create a plot with a dotted mesh
98 @type mesh: 1|0
99 @param palette: color palette name see L{Biskit.ColorSpectrum}
100 @type palette: str
101 @param legend: create a legend (scale) showing the walues of the
102 different colors in the plot.
103 @type legend: 1|0
104
105 @return: biggles plot object, view with biggles.FramedPlot.show() or
106 save with biggles.FramedPlot.write_eps(file_name).
107 @rtype: biggles.FramedPlot
108 """
109 if not biggles:
110 raise ImportError, 'biggles module could not be imported.'
111
112 FramedPlot.__init__(self)
113
114 self.palette = ColorSpectrum( palette )
115
116 self.matrix = self.palette.color_array( matrix )
117 s = N.shape( self.matrix )
118
119 for i in range(s[0]):
120 for j in range(s[1]):
121
122 col = self.matrix[i,j]
123
124 x1 = (j, j + 1)
125 y1 = (i, i)
126 y2 = (i + 1, i + 1)
127
128 cell = biggles.FillBetween(x1, y1, x1, y2, color = col)
129
130 self.add(cell)
131
132 if mesh:
133
134 for i in range(s[0] + 1):
135 self.add(biggles.LineY(i, linetype='dotted'))
136
137 for i in range(s[1] + 1):
138 self.add(biggles.LineX(i, linetype='dotted'))
139
140 if legend:
141
142 legend = self.__make_legend()
143
144 self.add(legend)
145
146 self.add(biggles.PlotBox((-0.17, -0.1), (1.25, 1.1)))
147
148 self.aspect_ratio = 1.0
149
150
152 """
153 Create and position the legend.
154
155 @return: biggles legend object
156 @rtype: biggles.Inset
157 """
158 l = self.palette.legend()
159
160 legend = Legend( l )
161
162 inset = biggles.Inset((1.1, 0.60), (1.2, .97), legend)
163
164 return inset
165
166
167
168
169
170
171
172 import Biskit.test as BT
173
174 -class Test(BT.BiskitTest):
175 """Test class """
176
178 """MatrixPlot test"""
179 n = 30
180
181 z = N.zeros((n,n), N.Float)
182
183 for i in range(N.shape(z)[0]):
184 for j in range(N.shape(z)[1]):
185 z[i,j] = N.exp(-0.01*((i-n/2)**2+(j-n/2)**2))
186
187 self.p = MatrixPlot(z, palette='sausage', legend=1)
188
189 if self.local or self.VERBOSITY > 2:
190 self.p.show()
191
192 self.assert_( self.p is not None )
193
194
195
196 if __name__ == '__main__':
197
198 BT.localTest()
199