rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
plot_element_data.m
1 function p = plot_element_data(grid,data,plot_params)
2 %function p = plot_element_data(grid,data[,plot_params])
3 % plot method for a 2d polygonal grid and elementwise constant data
4 % routine can be used for triangular and rectangular grids.
5 %
6 % By default a patch plot is performed.
7 %
8 % parameters:
9 % data : is assumed to be a vector of length 'grid.nelements' with
10 % element-wise scalar values
11 %
12 % return values:
13 % p: is the list of handles to the graphics primitives
14 %
15 % optional fields of plot_params:
16 % shrink_factor : if this flag is given, the elements are plotted
17 % shrinked
18 % axis_equal : if this flag is set, set axis to equal scale
19 % no_lines : if this flag is set, no lines are drawn
20 % show_colorbar : if this flag is set, a colorbar is drawn (default
21 % 1)
22 % colorbar_location : string specifying the position of the colorbar,
23 % e.g. 'South','EastOutside' (default), etc.
24 % clim : if this 2-vector is set, the colorbar is set to
25 % these values
26 % geometry_transformation: type of transformation function that is to be
27 % applied on the geometry (default 'none')
28 % postprocess : function 'data = f(data,[CX,CY],plot_params)'
29 % postprocessing the given data before it is plotted.
30 % colormap : user-defined colormap
31 % plot_type : string sepcifying wether a 'patch' (default) or a
32 % 'contour' plot is created.
33 % line_width : specifyies the line width of plotted lines, e.g.
34 % in a contour plot.
35 % contour_lines : number of contour_lines if plot_type is set to
36 % 'contour'. The default is '0' and means that the
37 % number is determined automatically.
38 %
39 
40 % Bernard Haasdonk 9.5.2007
41 
42 if nargin<3
43  plot_params = [];
44 end;
45 
46 if ~(isfield(plot_params,'shrink_factor'))
47  plot_params.shrink_factor = 1.0;
48 end;
49 
50 if ~(isfield(plot_params,'axis_equal'))
51  plot_params.axis_equal = 0;
52 end;
53 
54 if ~(isfield(plot_params,'no_lines'))
55  plot_params.no_lines = 0;
56 end;
57 
58 if ~(isfield(plot_params,'show_colorbar'))
59  plot_params.show_colorbar = 1;
60 end;
61 
62 if ~(isfield(plot_params,'colorbar_location'))
63  plot_params.colorbar_location = 'EastOutside';
64 end;
65 
66 if ~(isfield(plot_params,'plot_type'))
67  plot_params.plot_type = 'patch';
68 end
69 
70 if ~(isfield(plot_params,'contour_lines'))
71  plot_params.contour_lines = 0;
72 end
73 
74 if ~(isfield(plot_params,'geometry_transformation'))
75  plot_params.geometry_transformation = 0;
76 end;
77 
78 if (length(data)~=grid.nelements)
79  error('length of data does not match number of elements!');
80 end;
81 
82 nneigh = grid.nneigh;
83 
84 % compute vertex coordinates and scale
85 XX = grid.X(grid.VI(:));
86 %XX = XX(:);
87 YY = grid.Y(grid.VI(:));
88 %YY = YY(:)
89 CX = grid.CX(:);
90 CY = grid.CY(:);
91 
92 DS = grid.DS;
93 
94 if (plot_params.geometry_transformation)
95  [XX,YY] = geometry_transformation(XX,YY,plot_params);
96  [CX,CY] = geometry_transformation(CX,CY,plot_params);
97 end;
98 
99 if isfield(plot_params, 'postprocess')
100  data = plot_params.postprocess(data,[CX,CY],plot_params);
101 end
102 
103 if isequal(plot_params.plot_type, 'patch')
104  XX = reshape(XX,size(grid.VI)); % nelements*4 matrix
105  YY = reshape(YY,size(grid.VI)); % nelements*4 matrix
106  CXX = repmat(CX,1,nneigh);
107  CYY = repmat(CY,1,nneigh);
108 
109  % scale coordinates
110  XX = (XX - CXX) * plot_params.shrink_factor + CXX;
111  YY = (YY - CYY) * plot_params.shrink_factor + CYY;
112 
113  % set patch colors for every VERTEX to the value of the function in the CELL
114  % center
115  CC = repmat(data(:)',nneigh,1);
116 
117  c = jet(256);
118  colormap(c);
119 
120  p = patch(XX',YY',CC);
121 
122  % Change the axes object's background to the background of figure object it
123  % the patch is embedded in.
124  ax = get(p, 'Parent');
125  fi = gcf; %get(ax, 'Parent');
126  set(ax, 'Color', get(fi, 'Color'));
127 
128 elseif isequal(plot_params.plot_type, 'contour')
129  xmin = min(grid.CX);
130  xmax = max(grid.CX);
131  ymin = min(grid.CY);
132  ymax = max(grid.CY);
133  lines = sum(grid.CX==xmin);
134  cols = sum(grid.CY==ymin);
135  xstep = (xmax-xmin)/(cols-1);
136  ystep = (ymax-ymin)/(lines-1);
137  if isequal(class(grid), 'rectgrid')
138  XX = xmin:xstep:xmax;
139  YY = ymin:ystep:ymax;
140  cdata = reshape(data,lines,cols)';
141  else
142  F=TriScatteredInterp(CX,CY,data);
143  XX = xmin:xstep/4:xmax;
144  YY = ymin:ystep/4:ymax;
145  cdata = F(meshgrid(XX,YY));
146  end
147  if plot_params.contour_lines > 0
148  [h,p] = contour(XX,YY,cdata,plot_params.contour_lines);
149  else
150  [h,p] = contour(XX,YY,cdata);
151  end
152  line([xmin xmin], [ymin ymin],'Color',[0.9 0.9 0.9]);
153  line([xmax xmax], [ymax ymax],'Color',[0.9 0.9 0.9]);
154 elseif isequal(plot_params.plot_type, 'mesh')
155  xmin = min(grid.CX);
156  xmax = max(grid.CX);
157  ymin = min(grid.CY);
158  ymax = max(grid.CY);
159  lines = sum(grid.CX==xmin);
160  cols = sum(grid.CY==ymin);
161  xstep = (xmax-xmin)/(cols-1);
162  ystep = (ymax-ymin)/(lines-1);
163  if isequal(class(grid), 'rectgrid')
164  XX = xmin:xstep:xmax;
165  YY = ymin:ystep:ymax;
166  cdata = reshape(data,lines,cols)';
167  else
168  F=TriScatteredInterp(CX,CY,data);
169  XX = xmin:xstep/4:xmax;
170  YY = ymin:ystep/4:ymax;
171  cdata = F(meshgrid(XX,YY));
172  end
173  p = surf(XX,YY,cdata');
174  set(p, 'edgecolor', 'none');
175  cm=colormap([0.7 0.7 0.7; 0.7 0.7 0.7]);
176  set(gca,'View',[-2,65]);
177  set(gca,'ZLim',[0,1]);
178  set(gca,'YLim',[0,1]);
179 % cm=colormap('gray');
180 % colormap(cm(end:-1:1,:));
181  camlight headlight;
182  lighting gouraud;
183 
184 end
185 
186 if isfield(plot_params,'line_width')
187  set(p, 'LineWidth', plot_params.line_width);
188 end
189 
190 if plot_params.axis_equal
191  axis equal;
192  axis tight;
193 end;
194 
195 if isequal(plot_params.plot_type, 'patch') && plot_params.no_lines
196  set(p,'linestyle','none');
197 end;
198 
199 if isfield(plot_params,'clim') && ~isempty(plot_params.clim)
200  if all(imag(plot_params.clim) == 0)
201  set(gca,'Clim',plot_params.clim);
202  end
203 end;
204 if isfield(plot_params,'colormap')
205  set(gcf,'Colormap',plot_params.colormap);
206 end;
207 if plot_params.show_colorbar
208  colorbar(plot_params.colorbar_location);
209 end;
210 
A cartesian rectangular grid in two dimensions with axis parallel elements.
Definition: rectgrid.m:17