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);
120 p = patch(XX',YY',CC);
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'));
128 elseif isequal(plot_params.plot_type, 'contour')
133 lines = sum(grid.CX==xmin);
134 cols = sum(grid.CY==ymin);
135 xstep = (xmax-xmin)/(cols-1);
136 ystep = (ymax-ymin)/(lines-1);
138 XX = xmin:xstep:xmax;
139 YY = ymin:ystep:ymax;
140 cdata = reshape(data,lines,cols)';
142 F=TriScatteredInterp(CX,CY,data);
143 XX = xmin:xstep/4:xmax;
144 YY = ymin:ystep/4:ymax;
145 cdata = F(meshgrid(XX,YY));
147 if plot_params.contour_lines > 0
148 [h,p] = contour(XX,YY,cdata,plot_params.contour_lines);
150 [h,p] = contour(XX,YY,cdata);
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')
159 lines = sum(grid.CX==xmin);
160 cols = sum(grid.CY==ymin);
161 xstep = (xmax-xmin)/(cols-1);
162 ystep = (ymax-ymin)/(lines-1);
164 XX = xmin:xstep:xmax;
165 YY = ymin:ystep:ymax;
166 cdata = reshape(data,lines,cols)';
168 F=TriScatteredInterp(CX,CY,data);
169 XX = xmin:xstep/4:xmax;
170 YY = ymin:ystep/4:ymax;
171 cdata = F(meshgrid(XX,YY));
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,:));
186 if isfield(plot_params,'line_width')
187 set(p, 'LineWidth', plot_params.line_width);
190 if plot_params.axis_equal
195 if isequal(plot_params.plot_type, 'patch') && plot_params.no_lines
196 set(p,'linestyle','none');
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);
204 if isfield(plot_params,'colormap')
205 set(gcf,'Colormap',plot_params.colormap);
207 if plot_params.show_colorbar
208 colorbar(plot_params.colorbar_location);
A cartesian rectangular grid in two dimensions with axis parallel elements.