forked from piermorel/gramm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request piermorel#27 from Nicholas-Schaub/master
Added geom_polygons()
- Loading branch information
Showing
5 changed files
with
241 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
function obj=geom_polygon(obj,varargin) | ||
% geom_polygon Create reference polygons in each facet | ||
% | ||
% This function allows to draw polygons in the background of each facet. | ||
% Inputs are given as 'name',value pairs: | ||
% | ||
% 'x' Cell array of vectors containing x coordinates | ||
% 'y' Cell array of vectors containing y coordinates | ||
% | ||
% When both 'x' and 'y' are provided, the lenght of each cell array should | ||
% correspond to the number of desired polygons n_polygons. The i_th cell of each cell array | ||
% should contain equally-sized vectors which correspond to the coordinates of | ||
% the vertices of the i_th polygon. | ||
% | ||
% When only 'x' (or only 'y') is given, geom_polygon() draws vertical (or | ||
% horizontal) rectangles that span the whole plot. In that case, 'x' (or 'y') should be a cell array which | ||
% length corresponds to the number of desired polygons n_polygons. Each cell of the | ||
% cell array should contain a vector of length 2 with the horizontal (or | ||
% vertical) start and end coordinates of the polygons. | ||
% | ||
% Optional inputs (defaults) can be specified for all polygons in the call | ||
% at once or specifically for each polygon: | ||
% | ||
% 'alpha' (0.2) fill alpha (length 1 or n_polygons) | ||
% 'color' ([0 0 0]) RGB fill color of the polygon ( 1 x 3 or | ||
% n_polygons x 3 ). Or color index with automatic colors (1 x | ||
% 1 or n_polygons x 1 integers) | ||
% 'line_color' ([0 0 0]) RGB line color of the polygon ( 1 x 3 or | ||
% n_polygons x 3 ). Or color index with automatic colors (1 x | ||
% 1 or n_polygons x 1 integers) | ||
% 'line_style' ({'none'}) line style of the polygon (length 1 or n_polygons) | ||
% | ||
|
||
|
||
% created: 2017-Mar-03 | ||
% author: Nicholas J. Schaub, Ph.D. | ||
% email: [email protected] | ||
% | ||
% modified: 2017-Mar-12, 2017-Apr-18, Pierre Morel | ||
|
||
|
||
% Parse inputs and set defaults | ||
|
||
p=inputParser; | ||
|
||
my_addParameter(p,'x',{}); | ||
my_addParameter(p,'y',{}); | ||
my_addParameter(p,'alpha',0.2); | ||
my_addParameter(p,'color',[0 0 0]); | ||
my_addParameter(p,'line_color',[0 0 0]); | ||
my_addParameter(p,'line_style',{'none'}); | ||
my_addParameter(p,'extent',2); | ||
parse(p,varargin{:}); | ||
|
||
temp_results=p.Results; | ||
|
||
|
||
% Check inputs | ||
if isempty(temp_results.x) && isempty(temp_results.y) | ||
warning('Both x and y are not provided. Will not draw polygons.') | ||
return | ||
end | ||
|
||
if ~iscell(temp_results.x) || ~iscell(temp_results.y) | ||
warning('Either x or y is not a cell. Will not draw polygons.') | ||
return | ||
end | ||
|
||
%If one of the xy input is omitted, we fill it with an cell full of empty | ||
%arrays | ||
if isempty(temp_results.x) | ||
N=length(temp_results.y); | ||
temp_results.x=repmat({[]},N,1); | ||
elseif isempty(temp_results.y) | ||
N=length(temp_results.x); | ||
temp_results.y=repmat({[]},N,1); | ||
else | ||
N=length(temp_results.x); | ||
end | ||
|
||
|
||
if length(temp_results.x) ~= length(temp_results.y) | ||
warning('The number of elements in x does not match y. Will not draw polygons.') | ||
return | ||
end | ||
|
||
%Check number of vertices | ||
nvx=cellfun(@length,temp_results.x); | ||
nvy=cellfun(@length,temp_results.y); | ||
|
||
%Which cases are allowed for omitted inputs | ||
to_complete = (nvx==2 & nvy==0) | (nvx==0 & nvy==2); | ||
|
||
if ~isequal(nvx(~to_complete),nvy(~to_complete)) | ||
warning('The number of x-coords does not match the number of y-coords for each polygon. Will not draw polygons.') | ||
return | ||
end | ||
|
||
|
||
|
||
|
||
%Expand the inputs for which single entries were given to the number of | ||
%polygons | ||
to_adjust={'alpha','color','line_color','line_style','extent'}; | ||
for k=1:length(to_adjust) | ||
if size(temp_results.(to_adjust{k}),1)==1 | ||
temp_results.(to_adjust{k}) = repmat(temp_results.(to_adjust{k}),N,1); | ||
end | ||
end | ||
|
||
% Add polygon settings to object, used when draw() is called | ||
to_fill=fieldnames(temp_results); | ||
for obj_ind=1:numel(obj) | ||
|
||
obj(obj_ind).polygon.on = true; | ||
for k=1:length(to_fill) | ||
obj(obj_ind).polygon.(to_fill{k})=vertcat(obj(obj_ind).polygon.(to_fill{k}),temp_results.(to_fill{k})) ; | ||
end | ||
|
||
|
||
% Include these options to be compatible with get_colormap | ||
% * could be changed to give increased control of polygon coloring | ||
color_opts=fieldnames(obj(obj_ind).color_options); | ||
for k=1:length(color_opts) | ||
obj(obj_ind).polygon.color_options.(color_opts{k})= obj(obj_ind).color_options.(color_opts{k}); | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
function draw_polygons(obj) | ||
% draw_polygons | ||
% | ||
% Handles the actual drawing of polygons when the draw() function is | ||
% called. | ||
|
||
% created: 2017-Mar-03 | ||
% author: Nicholas J. Schaub, Ph.D. | ||
% email: [email protected] | ||
% | ||
% modified: 2017-Mar-12, Pierre Morel | ||
|
||
%If we get indices for color or line_color, use get_line_colormap to get actual line_colors | ||
if size(obj.polygon.color,2)==1 | ||
cmap=get_colormap(max(obj.polygon.color),1,obj.polygon.color_options); | ||
obj.polygon.color=cmap(obj.polygon.color,:); | ||
end | ||
|
||
if size(obj.polygon.line_color,2)==1 | ||
cmap=get_colormap(max(obj.polygon.line_color),1,obj.polygon.color_options); | ||
obj.polygon.line_color=cmap(obj.polygon.line_color,:); | ||
end | ||
|
||
for poly_ind = 1:length(obj.polygon.x) | ||
|
||
tmp_x=obj.polygon.x{poly_ind}; | ||
tmp_y=obj.polygon.y{poly_ind}; | ||
|
||
%Handle cases of omitted x or y values | ||
if isempty(tmp_x) | ||
tmp_xl=[obj.var_lim.minx obj.var_lim.maxx]; | ||
tmp_extent=(tmp_xl(2)-tmp_xl(1))*obj.polygon.extent(poly_ind)/2; | ||
xl=[mean(tmp_xl)-tmp_extent mean(tmp_xl)+tmp_extent]; | ||
tmp_y=[tmp_y(1) tmp_y(2) tmp_y(2) tmp_y(1)]; | ||
tmp_x=[xl(1) xl(1) xl(2) xl(2)]; | ||
end | ||
if isempty(tmp_y) | ||
tmp_yl=[obj.var_lim.miny obj.var_lim.maxy]; | ||
tmp_extent=(tmp_yl(2)-tmp_yl(1))*obj.polygon.extent(poly_ind)/2; | ||
yl=[mean(tmp_yl)-tmp_extent mean(tmp_yl)+tmp_extent]; | ||
tmp_x=[tmp_x(1) tmp_x(2) tmp_x(2) tmp_x(1)]; | ||
tmp_y=[yl(1) yl(1) yl(2) yl(2)]; | ||
end | ||
|
||
p = patch(tmp_x,tmp_y,obj.polygon.color(poly_ind,:),... | ||
'Parent',obj.facet_axes_handles(obj.current_row,obj.current_column),... | ||
'FaceColor',obj.polygon.color(poly_ind,:),... | ||
'FaceAlpha',obj.polygon.alpha(poly_ind),... | ||
'EdgeColor',obj.polygon.line_color(poly_ind,:),... | ||
'LineStyle',obj.polygon.line_style{poly_ind}); | ||
|
||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters