Commit f5911223 authored by Jesse Heckman's avatar Jesse Heckman

major update draft + graphics

parent a5e77167
...@@ -22,7 +22,7 @@ function draft(obj) ...@@ -22,7 +22,7 @@ function draft(obj)
set(t,'FontSize',20); set(t,'FontSize',20);
ax = gobjects(0); ax = gobjects(0);
dsz = size(obj); objsz = size(obj);
%% Super labels %% Super labels
% Check if labels are the same, and prep super labels % Check if labels are the same, and prep super labels
...@@ -34,35 +34,50 @@ function draft(obj) ...@@ -34,35 +34,50 @@ function draft(obj)
end end
obj(1).labels.supx = lab_x; obj(1).labels.supx = lab_x;
obj(1).labels.supy = lab_y; obj(1).labels.supy = lab_y;
%% Create (sub)plots
% Draft each subplot
% Select compare axis.
objsz = numel(obj);
cmpsz = 1;
tmpV = [];
if isfield(obj(1).pva,'axcomp'); tmpV = obj(1).pva.axcomp; end %% Set Compare axis
if ~isempty(tmpV) && objsz == 1; cmpsz = length(unique(tmpV.feature)); end cmp = length(unique(obj(1).pva.axcomp.feature));
cmpsz = [1 1];
nObj = numel(obj)*cmp;
if min(objsz)>1
cmp = 1;
else
if objsz(1)<objsz(2)
% horizontal direction
cmpsz(1) = cmp;
else
% vertical direction
cmpsz(2) = cmp;
end
end
for iCmp = 1:cmpsz %% Create (sub)plots
% Draft each subplot
% Make Compare Axes
% // NOTE THAT THIS IS VERY INEFFICIENT: DO 1) MAKE AXCMP DEFAULT 1 for iObj = 1:numel(obj)
r = 1; % Repeat for comparing axis
c = 1; for iCmp = 1:cmp
ax(iCmp) = subplot(r,c,iCmp); cObj = (iObj-1)*cmp + iCmp;
for iObj = 1:objsz
% Make Axes % Make Axes
iAx = pb_invidx([dsz(1), dsz(2)],iObj); Ax = pb_invidx([objsz(1)*cmpsz(1), objsz(2)*cmpsz(2)],cObj); % reverse get axes index
ax(iAx) = subplot(dsz(1),dsz(2),iObj); Ax2Obj = pb_invidx([objsz(1), objsz(2)],iObj); % reverse get object index
ax(Ax) = subplot(objsz(1)*cmpsz(1),objsz(2)*cmpsz(2),cObj); % make axis
if obj(iAx).pva.subtitle; title(obj(iAx).pva.subtitle); end if obj(iObj).pva.subtitle; title(obj(Ax2Obj).pva.subtitle); end
axis(obj(iAx).pva.axis); axis(obj(Ax2Obj).pva.axis);
hold on; hold on;
% Select data
uFeat = unique(obj(1).pva.axcomp.feature);
sel = obj(1).pva.axcomp.feature == uFeat(iCmp);
D = obj(Ax2Obj).pva;
D.x = D.x(sel);
D.y = D.y(sel);
D.color = D.color(sel);
% Set colors % Set colors
nCol = 1; nCol = 1;
if ~obj(iObj).pva.continious if ~obj(iObj).pva.continious
...@@ -71,17 +86,16 @@ function draft(obj) ...@@ -71,17 +86,16 @@ function draft(obj)
% Plot all graphs % Plot all graphs
for iDP = 1:length(obj(iObj).dplot) for iDP = 1:length(obj(iObj).dplot)
d = obj(iAx).pva; obj(iObj).dplot{iDP}(obj,D);
obj(iObj).dplot{iDP}(obj,d);
end end
% Set labels % Set labels
if ~obj(1).labels.supx; xlabel(obj(iAx).labels.xlab); end if ~obj(1).labels.supx; xlabel(obj(Ax2Obj).labels.xlab); end
if ~obj(1).labels.supy; ylabel(obj(iAx).labels.ylab); end if ~obj(1).labels.supy; ylabel(obj(Ax2Obj).labels.ylab); end
% Make scientific notations on the axes. % Make scientific notations on the axes.
ax(iAx).YAxis.Exponent = length(num2str(max(abs(round(ax(iAx).YLim)))))-1; ax(Ax).YAxis.Exponent = length(num2str(max(abs(round(ax(Ax).YLim)))))-1;
ax(iAx).XAxis.Exponent = length(num2str(max(abs(round(ax(iAx).XLim)))))-1; ax(Ax).XAxis.Exponent = length(num2str(max(abs(round(ax(Ax).XLim)))))-1;
end end
end end
...@@ -91,8 +105,8 @@ function draft(obj) ...@@ -91,8 +105,8 @@ function draft(obj)
% TO DO: % TO DO:
% 1. Optionalize subplots to have squared/linked/fixed axis % 1. Optionalize subplots to have squared/linked/fixed axis
% 2. Scale and move axis to make graph nice, pleasing and non-overlapping % 2. Scale and move axis to make graph nice, pleasing and non-overlapping
make_axes(obj,ax); obj.make_axes(ax);
make_suplabel(obj); obj.make_suplabel;
end end
...@@ -101,17 +115,4 @@ end ...@@ -101,17 +115,4 @@ end
% Part of Programmeer Beer Toolbox (PBToolbox) % % Part of Programmeer Beer Toolbox (PBToolbox) %
% Written by: Jesse J. Heckman (2019) % % Written by: Jesse J. Heckman (2019) %
% % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
\ No newline at end of file
% % FIX COLOR REPEAT ONLY FOR COLOR SPLITTING DATA
% colors = pb_selectcolor(nCol,obj(iAx).pva.def);
% uD = unique(obj(iAx).pva.color);
% for iCol = 1:nCol
% d = obj(iAx).pva;
% d.ind = d.color == uD(iCol);
% d.color = colors(iCol,:);
% end
function draft(obj)
% PB_DRAFT>DRAFT
%
% OBJ.DRAFT will draw a figure from a pb_draft-object.
%
% See also PB_DRAFT, SET_LABELS, SET_TITLE, SET_GRID, PRINT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl
%% Create figure
% Select parent/make figure
if isempty(obj(1).parent)
for iObj = 1:numel(obj)
obj(iObj).parent = gcf;
end
end
% Set figure settings
set(obj(1).parent,'color','w');
t = sgtitle(obj(1).parent,obj(1).title);
set(t,'FontSize',20);
ax = gobjects(0);
dsz = size(obj);
%% Super labels
% Check if labels are the same, and prep super labels
lab_x = true; lab_y = true;
for iObj = 1:numel(obj)
if ~strcmp(obj(1).labels.xlab, obj(iObj).labels.xlab); lab_x = false; end
if ~strcmp(obj(1).labels.ylab, obj(iObj).labels.ylab); lab_y = false; end
end
obj(1).labels.supx = lab_x;
obj(1).labels.supy = lab_y;
%% Create (sub)plots
% Draft each subplot
% Create comparing axis
cmp = length(unique(obj(1).pva.axcomp.feature));
if min(dsz)>1
cmp = 1;
if dsz(1)>dsz(2)
obj(1).pva.axcomp.orientation = 'vertical';
else
obj(1).pva.axcomp.orientation = 'horizontal';
end
end
nObj = numel(obj)*cmp;
for iObj = 1:numel(obj)
% Repeat for comparing axis
for iCmp = 1:cmp
% Make Axes
iAx = pb_invidx([dsz(1), dsz(2)],iObj);
ax(iAx) = subplot(dsz(1),dsz(2),iObj);
if obj(iAx).pva.subtitle; title(obj(iAx).pva.subtitle); end
axis(obj(iAx).pva.axis);
hold on;
% Set colors
nCol = 1;
if ~obj(iObj).pva.continious
nCol = length(unique(obj(iObj).pva.color));
end
% Plot all graphs
for iDP = 1:length(obj(iObj).dplot)
d = obj(iAx).pva;
obj(iObj).dplot{iDP}(obj,d);
end
% Set labels
if ~obj(1).labels.supx; xlabel(obj(iAx).labels.xlab); end
if ~obj(1).labels.supy; ylabel(obj(iAx).labels.ylab); end
% Make scientific notations on the axes.
ax(iAx).YAxis.Exponent = length(num2str(max(abs(round(ax(iAx).YLim)))))-1;
ax(iAx).XAxis.Exponent = length(num2str(max(abs(round(ax(iAx).XLim)))))-1;
end
end
%% Adjust axis handles
% Set super labels
% TO DO:
% 1. Optionalize subplots to have squared/linked/fixed axis
% 2. Scale and move axis to make graph nice, pleasing and non-overlapping
make_axes(obj,ax);
make_suplabel(obj);
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% %
% Part of Programmeer Beer Toolbox (PBToolbox) %
% Written by: Jesse J. Heckman (2019) %
% %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
\ No newline at end of file
...@@ -25,6 +25,7 @@ classdef pb_draft < matlab.mixin.Copyable ...@@ -25,6 +25,7 @@ classdef pb_draft < matlab.mixin.Copyable
pva; pva;
labels; labels;
grid;
dplot = {}; dplot = {};
......
...@@ -63,7 +63,7 @@ function h = bubble_plot(~,data,p) ...@@ -63,7 +63,7 @@ function h = bubble_plot(~,data,p)
x = x(sel); x = x(sel);
y = y(sel); y = y(sel);
SZ = ceil(100*M); SZ = ceil(50*M);
[~,~,idx] = unique(M); [~,~,idx] = unique(M);
col = statcolor(max(idx),[],[],[],'def',p.def); col = statcolor(max(idx),[],[],[],'def',p.def);
C = col(idx,:); C = col(idx,:);
......
...@@ -5,9 +5,10 @@ function plot_dline(obj,varargin) ...@@ -5,9 +5,10 @@ function plot_dline(obj,varargin)
% %
% See also PB_DRAFT % See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl % PBToolbox (2019): JJH: j.heckman@donders.ru.nl
p.v = varargin; p.v = varargin;
p.lim = [obj(1).pva.limits.x obj(1).pva.limits.y];
obj.dplot = vertcat(obj.dplot,{@(dobj,data)dline(dobj,data,p)}); obj.dplot = vertcat(obj.dplot,{@(dobj,data)dline(dobj,data,p)});
end end
...@@ -17,7 +18,7 @@ function h = dline(~,~,p) ...@@ -17,7 +18,7 @@ function h = dline(~,~,p)
style = pb_keyval('style',p.v,'k--'); style = pb_keyval('style',p.v,'k--');
visibility = pb_keyval('visibility',p.v,'off'); visibility = pb_keyval('visibility',p.v,'off');
h = pb_dline('style',style,'visibility',visibility); h = pb_dline('style',style,'lim',p.lim,'visibility',visibility);
end end
......
...@@ -5,9 +5,10 @@ function plot_hline(obj,varargin) ...@@ -5,9 +5,10 @@ function plot_hline(obj,varargin)
% %
% See also PB_DRAFT % See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl % PBToolbox (2019): JJH: j.heckman@donders.ru.nl
p.v = varargin; p.v = varargin;
p.lim = obj(1).pva.limits.x;
obj.dplot = vertcat(obj.dplot,{@(dobj,data)hline(dobj,data,p)}); obj.dplot = vertcat(obj.dplot,{@(dobj,data)hline(dobj,data,p)});
end end
...@@ -28,7 +29,7 @@ function h = hline(~,data,p) ...@@ -28,7 +29,7 @@ function h = hline(~,data,p)
point = mode(data.y); point = mode(data.y);
end end
end end
h = pb_hline(point,'style',style); h = pb_hline(point,'lim',p.lim,'style',style);
end end
......
...@@ -29,9 +29,9 @@ function h = rawdata(~,data,p,colorindex) ...@@ -29,9 +29,9 @@ function h = rawdata(~,data,p,colorindex)
%% MAKE SELECTION FOR COLOR! %% MAKE SELECTION FOR COLOR!
sel = data.color == colorindex; sel = data.color == colorindex;
x = data.x(sel); x = data.x(sel);
y = data.y(sel); y = data.y(sel);
color = pb_selectcolor(length(p.ncol),data.def); color = pb_selectcolor(length(p.ncol),data.def);
......
...@@ -5,9 +5,10 @@ function plot_vline(obj,varargin) ...@@ -5,9 +5,10 @@ function plot_vline(obj,varargin)
% %
% See also PB_DRAFT % See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl % PBToolbox (2019): JJH: j.heckman@donders.ru.nl
p.v = varargin; p.v = varargin;
p.lim = obj(1).pva.limits.y;
obj.dplot = vertcat(obj.dplot,{@(dobj,data)vline(dobj,data,p)}); obj.dplot = vertcat(obj.dplot,{@(dobj,data)vline(dobj,data,p)});
end end
...@@ -28,7 +29,7 @@ function h = vline(~,data,p) ...@@ -28,7 +29,7 @@ function h = vline(~,data,p)
point = mode(data.x); point = mode(data.x);
end end
end end
h = pb_vline(point,'style',style); h = pb_vline(point,'lim',p.lim,'style',style);
end end
......
...@@ -14,11 +14,12 @@ function print(obj,varargin) ...@@ -14,11 +14,12 @@ function print(obj,varargin)
fn = pb_keyval('fn',v,defaultname(cdir)); fn = pb_keyval('fn',v,defaultname(cdir));
fit = pb_keyval('fit',v,'-bestfit'); fit = pb_keyval('fit',v,'-bestfit');
format = pb_keyval('format',v,'-dpdf'); format = pb_keyval('format',v,'-dpdf');
render = pb_keyval('render',v,'-painters');
disp = pb_keyval('disp',v,false); disp = pb_keyval('disp',v,false);
figure(h); figure(h);
loc = [cdir fn]; loc = [cdir fn];
print(fit,loc,format); print(fit,loc,format,render);
if disp if disp
open([loc '.' format(3:end)]); open([loc '.' format(3:end)]);
......
function make_axes(obj,ax,varargin) function make_axes(obj,ax,varargin)
% PB_DRAFT>SET_LEGEND % PB_DRAFT>MAKE_AXES
% %
% OBJ.SET_LEGEND(varargin) rescale, size and store axes handles.. % OBJ.SET_LEGEND(varargin) rescale, size and store axes handles..
% %
% See also PB_DRAFT % See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl % PBToolbox (2019): JJH: j.heckman@donders.ru.nl
obj(1).h_ax_plot = ax; linkaxes(ax,'xy');
% TODO: LINK AXES AND FIND OPTIMAL AXE TICKS
positions = zeros(length(ax),4); positions = zeros(length(ax),4);
for iAx = 1:length(ax) for iAx = 1:length(ax)
axis(ax(iAx));
xt = xticks;
yt = yticks;
setAx = ax(iAx);
xlim(ax(iAx),[xt(1) xt(end)]);
ylim(ax(iAx),[yt(1) yt(end)]);
positions(iAx,:) = ax(iAx).Position(:); positions(iAx,:) = ax(iAx).Position(:);
if obj(1).grid.bool
f = obj(1).grid.features;
setAx.Box = f.Box;
setAx.TickDir = f.TickDir;
setAx.TickLength = f.TickLength;
setAx.XMinorTick = f.XMinorTick;
setAx.YMinorTick = f.YMinorTick;
setAx.YGrid = f.YGrid;
setAx.XGrid = f.XGrid;
setAx.YColor = f.YColor;
setAx.XColor = f.XColor;
setAx.FontSize = f.FontSize;
setAx.YDir = f.YDir;
setAx.LineWidth = f.LineWidth;
end
end end
obj(1).labels.ypos = min(positions(:,2)); obj(1).labels.ypos = min(positions(:,2));
obj(1).labels.xpos = min(positions(:,1)); obj(1).labels.xpos = min(positions(:,1));
obj(1).labels.pos = [min(positions(:,1))/1.33 min(positions(:,2))/1.33 max(positions(:,1)+positions(:,3)) max(positions(:,2)+positions(:,4))]; obj(1).labels.pos = [min(positions(:,1))/1.33 min(positions(:,2))/1.33 max(positions(:,1)+positions(:,3)) max(positions(:,2)+positions(:,4))];
obj(1).h_ax_plot = ax;
end end
......
function make_suplabel(obj,varargin) function make_suplabel(obj,varargin)
% PB_DRAFT>MAKE_SUPLABEL % PB_DRAFT>MAKE_SUPLABEL
% %
% OBJ.SET_LEGEND(varargin) Make superlabels handle. % OBJ.MAKE_SUPLABEL(varargin) make superlabels handle.
% %
% See also PB_DRAFT % See also PB_DRAFT
......
...@@ -11,20 +11,36 @@ function parse_va(obj,varargin) ...@@ -11,20 +11,36 @@ function parse_va(obj,varargin)
%% READ DATA %% READ DATA
% Read Data, and set keyvals % Read Data, and set keyvals
% Read keyvals
v = varargin; v = varargin;
pva.x = pb_keyval('x',v,[]); pva.x = pb_keyval('x',v,[]);
pva.y = pb_keyval('y',v,[]); pva.y = pb_keyval('y',v,[]);
pva.z = pb_keyval('z',v,[]); pva.z = pb_keyval('z',v,[]);
pva.def = pb_keyval('def',v,1); pva.def = pb_keyval('def',v,1);
pva.color = pb_keyval('color',v,ones(length(pva.x),1)); pva.color = pb_keyval('color',v,ones(length(pva.x),1));
pva.axis = pb_keyval('axis',v,'square'); pva.axis = pb_keyval('axis',v);
pva.subtitle = pb_keyval('subtitle',v); pva.subtitle = pb_keyval('subtitle',v);
pva.linkax = pb_keyval('link',v,true);
% Check data limits
[x,y] = setlim(pva);
pva.limits.x = x;
pva.limits.y = y;
% Check square axis
if isempty(pva.axis)
if abs(max(x)-max(y)) / max([x,y])<.5
pva.axis = 'square';
end
end
% Set layout
pva.colscheme = pb_selectcolor(length(unique(pva.color)),pva.def); pva.colscheme = pb_selectcolor(length(unique(pva.color)),pva.def);
pva.axcomp = axcmp(pva.x);
obj.labels.xlab = pb_keyval('xlab',v,''); obj.labels.xlab = pb_keyval('xlab',v,'');
obj.labels.ylab = pb_keyval('ylab',v,''); obj.labels.ylab = pb_keyval('ylab',v,'');
obj.grid.bool = false;
% Check data continuity for colouring % Check data continuity for colouring
pva.continious = false; pva.continious = false;
...@@ -35,6 +51,41 @@ function parse_va(obj,varargin) ...@@ -35,6 +51,41 @@ function parse_va(obj,varargin)
obj.pva = pva; obj.pva = pva;
end end
function ac = axcmp(data)
% Writes empty axcomp data struct
ac.prefix = '';
ac.feature = ones(1,length(data));
ac.n = length(unique(ac.feature));
end
function [x,y] = setlim(data)
% Sets the limits for axes
x = [min(data.x) max(data.x)];
y = [min(data.y) max(data.y)];
xr = x(2)-x(1);
yr = y(2)-y(1);
% minimum
if abs(min(x))/xr < .1; x(1) = 0; end
if abs(min(y))/yr < .1; y(1) = 0; end
% maximum
tmp = max([x,y]);
order = 0;
scale = .1;
if tmp<1; scale = 10; end
while tmp < .1 || tmp > 1
tmp = tmp*scale;
order = order+1;
end
tmp = round(tmp*100)/100;
x(2) = tmp * inv(scale)^order;
y(2) = tmp * inv(scale)^order;
end
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% % % %
......
...@@ -6,7 +6,7 @@ function set_axcomp(obj,parameter,varargin) ...@@ -6,7 +6,7 @@ function set_axcomp(obj,parameter,varargin)
% %
% See also PB_DRAFT % See also PB_DRAFT
% PBToolbox (2018): JJH: j.heckman@donders.ru.nl % PBToolbox (2019): JJH: j.heckman@donders.ru.nl
if nargin <2 || isempty(parameter); return; end if nargin <2 || isempty(parameter); return; end
...@@ -30,47 +30,3 @@ end ...@@ -30,47 +30,3 @@ end
% % % %
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% p=inputParser;
% my_addParameter(p,'scale','fixed'); %options 'free' 'free_x' 'free_y' 'independent'
% my_addParameter(p,'space','fixed'); %'free_x','free_y','free'
% my_addParameter(p,'force_ticks',false);
% my_addParameter(p,'column_labels',true);
% my_addParameter(p,'row_labels',true);
% parse(p,varargin{:});
%
% obj.facet_scale=p.Results.scale;
% obj.facet_space=p.Results.space;
% obj.column_labels=p.Results.column_labels;
% obj.row_labels=p.Results.row_labels;
%
% if strcmp(obj.facet_scale,'independent') %Force ticks by default in that case
% obj.force_ticks=true;
% else
% obj.force_ticks=p.Results.force_ticks;
% end
%
% %Handle case where facet_grid is called after update()
% if obj.updater.updated
% if isnumeric(obj.aes.row) && isnumeric(obj.aes.column) && all(obj.aes.row==1) && all(obj.aes.column==1)
% if isempty(obj.aes.row) && isempty(obj.aes.column)
% %User probably tried to update all the data
% obj.updater.facet_updated=0;
% else
% %We go from one to multiple facets
% obj.updater.facet_updated=1;
% end
% else
% if isempty(row) && isempty(col)
% %We go from multiple to one facet