Package Biskit :: Module MatrixPlot
[hide private]
[frames] | no frames]

Source Code for Module Biskit.MatrixPlot

  1  ## Automatically adapted for numpy.oldnumeric Mar 26, 2007 by alter_code1.py 
  2   
  3  ## 
  4  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  5  ## Copyright (C) 2004-2006 Wolfgang Rieping 
  6  ## 
  7  ## This program is free software; you can redistribute it and/or 
  8  ## modify it under the terms of the GNU General Public License as 
  9  ## published by the Free Software Foundation; either version 2 of the 
 10  ## License, or any later version. 
 11  ## 
 12  ## This program is distributed in the hope that it will be useful, 
 13  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 15  ## General Public License for more details. 
 16  ## 
 17  ## You find a copy of the GNU General Public License in the file 
 18  ## license.txt along with this program; if not, write to the Free 
 19  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 20   
 21  ## last $Author: graik $ 
 22  ## last $Date: 2007/03/26 18:40:40 $ 
 23  ## $Revision: 2.18 $ 
 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   
39 -class Legend(FramedPlot):
40 """ 41 Class to create a legend to use with a Matrix plot. 42 """ 43
44 - def __init__(self, values):
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 ## number of labeled ticks in legend 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
86 -class MatrixPlot(FramedPlot):
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
151 - def __make_legend(self):
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 ## TESTING 171 ############# 172 import Biskit.test as BT 173
174 -class Test(BT.BiskitTest):
175 """Test class """ 176
177 - def test_MatrixPlot( self ):
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