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.
6 % By
default a patch plot is performed.
9 % data : is assumed to be a vector of length
'grid.nelements' with
10 % element-wise scalar values
13 % p: is the list of handles to the graphics primitives
15 % optional fields of plot_params:
16 % shrink_factor : if this flag is given, the elements are plotted
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
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
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.
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.
40 % Bernard Haasdonk 9.5.2007
46 if ~(isfield(plot_params,
'shrink_factor'))
47 plot_params.shrink_factor = 1.0;
50 if ~(isfield(plot_params,
'axis_equal'))
51 plot_params.axis_equal = 0;
54 if ~(isfield(plot_params,
'no_lines'))
55 plot_params.no_lines = 0;
58 if ~(isfield(plot_params,
'show_colorbar'))
59 plot_params.show_colorbar = 1;
62 if ~(isfield(plot_params,
'colorbar_location'))
63 plot_params.colorbar_location =
'EastOutside';
66 if ~(isfield(plot_params,
'plot_type'))
67 plot_params.plot_type =
'patch';
70 if ~(isfield(plot_params,
'contour_lines'))
71 plot_params.contour_lines = 0;
74 if ~(isfield(plot_params,
'geometry_transformation'))
75 plot_params.geometry_transformation = 0;
78 if (length(data)~=grid.nelements)
79 error(
'length of data does not match number of elements!');
84 % compute vertex coordinates and scale
85 XX = grid.X(grid.VI(:));
87 YY = grid.Y(grid.VI(:));
94 if (plot_params.geometry_transformation)
95 [XX,YY] = geometry_transformation(XX,YY,plot_params);
96 [CX,CY] = geometry_transformation(CX,CY,plot_params);
99 if isfield(plot_params, 'postprocess')
100 data = plot_params.postprocess(data,[CX,CY],plot_params);
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);
110 XX = (XX - CXX) * plot_params.shrink_factor + CXX;
111 YY = (YY - CYY) * plot_params.shrink_factor + CYY;
113 % set patch colors for every VERTEX to the value of the function in the CELL
115 CC = repmat(data(:)',nneigh,1);
117 p = patch(XX',YY',CC);
119 % Change the axes
object's background to the background of figure
object it
120 % the patch is embedded in.
121 ax = get(p, 'Parent');
122 fi = gcf; %get(ax, 'Parent');
123 set(ax, 'Color', get(fi, 'Color'));
125 elseif isequal(plot_params.plot_type, 'contour')
130 lines = sum(grid.CX==xmin);
131 cols = sum(grid.CY==ymin);
132 xstep = (xmax-xmin)/(cols-1);
133 ystep = (ymax-ymin)/(lines-1);
135 XX = xmin:xstep:xmax;
136 YY = ymin:ystep:ymax;
137 cdata = reshape(data,lines,cols)';
139 F=TriScatteredInterp(CX,CY,data);
140 XX = xmin:xstep/4:xmax;
141 YY = ymin:ystep/4:ymax;
142 cdata = F(meshgrid(XX,YY));
144 if plot_params.contour_lines > 0
145 [h,p] = contour(XX,YY,cdata,plot_params.contour_lines);
147 [h,p] = contour(XX,YY,cdata);
149 line([xmin xmin], [ymin ymin],'Color',[0.9 0.9 0.9]);
150 line([xmax xmax], [ymax ymax],'Color',[0.9 0.9 0.9]);
151 elseif isequal(plot_params.plot_type, 'mesh')
156 lines = sum(grid.CX==xmin);
157 cols = sum(grid.CY==ymin);
158 xstep = (xmax-xmin)/(cols-1);
159 ystep = (ymax-ymin)/(lines-1);
161 XX = xmin:xstep:xmax;
162 YY = ymin:ystep:ymax;
163 cdata = reshape(data,lines,cols)';
165 F=TriScatteredInterp(CX,CY,data);
166 XX = xmin:xstep/4:xmax;
167 YY = ymin:ystep/4:ymax;
168 cdata = F(meshgrid(XX,YY));
170 p = surf(XX,YY,cdata');
171 set(p, 'edgecolor', 'none');
172 cm=colormap([0.7 0.7 0.7; 0.7 0.7 0.7]);
173 set(gca,'View',[-2,65]);
174 set(gca,'ZLim',[0,1]);
175 set(gca,'YLim',[0,1]);
176 % cm=colormap('gray');
177 % colormap(cm(end:-1:1,:));
183 if isfield(plot_params,'line_width')
184 set(p, 'LineWidth', plot_params.line_width);
187 if plot_params.axis_equal
192 if isequal(plot_params.plot_type, 'patch') && plot_params.no_lines
193 set(p,'linestyle','none');
196 if isfield(plot_params,'clim') && ~isempty(plot_params.clim)
197 if all(imag(plot_params.clim) == 0)
198 set(gca,'Clim',plot_params.clim);
201 if isfield(plot_params,'colormap')
202 set(gcf,'Colormap',plot_params.colormap);
204 if plot_params.show_colorbar
205 colorbar(plot_params.colorbar_location);