From d3130d6ba4314c3a3654c7dee6f3c7ad5fc94a29 Mon Sep 17 00:00:00 2001 From: Julie-Fabre Date: Fri, 3 Nov 2023 16:37:32 +0000 Subject: [PATCH] added name/value pairs + more features in prettify_plot --- examples/uglyCodeExample.m | 49 +++- prettify_colors/prettify_rgb.asv | 286 ++++++++++++++++++ prettify_colors/prettify_rgb.m | 286 ++++++++++++++++++ prettify_plot/prettify_plot.asv | 255 +++++++++++++++++ prettify_plot/prettify_plot.m | 461 +++++++++++++++++------------- prettify_plot/prettify_plot_old.m | 200 +++++++++++++ 6 files changed, 1324 insertions(+), 213 deletions(-) create mode 100644 prettify_colors/prettify_rgb.asv create mode 100644 prettify_colors/prettify_rgb.m create mode 100644 prettify_plot/prettify_plot.asv create mode 100644 prettify_plot/prettify_plot_old.m diff --git a/examples/uglyCodeExample.m b/examples/uglyCodeExample.m index 0d6171d..c369fa4 100644 --- a/examples/uglyCodeExample.m +++ b/examples/uglyCodeExample.m @@ -1,13 +1,36 @@ -function [f,r,t]=uglyCodeExample(n) -%comment -if n<0,error('Negative!');elseif isinf(n),error('Too big!'),end -if ~(isnumeric(n )&&isscalar( n)&&mod(n,1)== 0),error('Not a pos int.');end,r=1;for i=1:n; -r=r*i;end -f=0; -g=1;for j=1:n; -t=f; -f=f+g; -g=t;end -t=0;for h=1:n; -t=t+h;end,k=3;while k= 1.5 + options.TextColor = [0, 0, 0]; + else + options.TextColor = [1, 1, 1]; + end +end +% Get handles for current figure and axis +currFig = gcf; + + +% Set color properties for figure and axis +set(currFig, 'color', options.FigureColor); + +% get axes children +all_axes = find(arrayfun(@(x) contains(currFig.Children(x).Type, 'axes'), 1:size(currFig.Children, 1))); + +% update font +fontname(options.Font) + +% update (sub)plot properties +for iAx = 1:size(all_axes, 2) + thisAx = all_axes(iAx); + currAx = currFig.Children(thisAx); + set(currAx, 'color', options.FigureColor); + if ~isempty(currAx) + + % Set grid/box/tick options + set(currAx, 'TickDir', options.AxisTicks) + set(currAx, 'Box', options.AxisBox) + set(currAx, 'Grid', options.AxisGrid) + if strcmp(options.AxisAspectRatio + set(currAx, ) + + % Set text properties + set(currAx.XLabel, 'FontSize', options.LabelFontSize, 'Color', options.TextColor); + if strcmp(currAx.YAxisLocation, 'left') % if there is both a left and right yaxis, keep the colors + set(currAx.YLabel, 'FontSize', options.LabelFontSize); + else + set(currAx.YLabel, 'FontSize', options.LabelFontSize, 'Color', options.TextColor); + end + if strcmp(options.BoldTitle, 'on') + set(currAx.Title, 'FontSize', options.TitleFontSize, 'Color', options.TextColor, ... + 'FontWeight', 'Bold') + else + set(currAx.Title, 'FontSize', options.TitleFontSize, 'Color', options.TextColor, ... + 'FontWeight', 'Normal'); + end + set(currAx, 'FontSize', options.GeneralFontSize, 'GridColor', options.TextColor, ... + 'YColor', options.TextColor, 'XColor', options.TextColor, ... + 'MinorGridColor', options.TextColor); + if ~isempty(currAx.Legend) + set(currAx.Legend, 'Color', options.FigureColor, 'TextColor', options.TextColor) + end + + % Adjust properties of line children within the plot + childLines = findall(currAx, 'Type', 'line'); + for thisLine = childLines' + if strcmp('.', get(thisLine, 'Marker')) + set(thisLine, 'MarkerSize', options.PointSize); + end + if strcmp('-', get(thisLine, 'LineStyle')) + set(thisLine, 'LineWidth', options.LineThickness); + end + end + + % Adjust properties of errorbars children within the plot + childErrBars = findall(currAx, 'Type', 'ErrorBar'); + for thisErrBar = childErrBars' + if strcmp('.', get(thisErrBar, 'Marker')) + set(thisErrBar, 'MarkerSize', options.PointSize); + end + if strcmp('-', get(thisErrBar, 'LineStyle')) + set(thisErrBar, 'LineWidth', options.LineThickness); + end + end + + % Get x and y limits + xlims_subplot(iAx, :) = currAx.XLim; + ylims_subplot(iAx, :) = currAx.YLim; + + % Get plot position + pos_subplot(iAx, :) = currAx.Position(1:2); % [left bottom width height + if ~isempty(currAx.Legend) + if options.LegendReplace + prettify_legend(currAx) + else + legend('Location', options.LegendLocation) + end + end + end +end + + +% make x and y lims the same +if ismember(options.XLimits, {'all', 'row', 'col'}) || ismember(options.YLimits, {'all', 'row', 'col'}) + % get rows and cols + col_subplots = unique(pos_subplot(:, 1)); + row_subplots = unique(pos_subplot(:, 2)); + + col_xlims = arrayfun(@(x) [min(min(xlims_subplot(pos_subplot(:, 1) == col_subplots(x), :))), ... + max(max(xlims_subplot(pos_subplot(:, 1) == col_subplots(x), :)))], 1:size(col_subplots, 1), 'UniformOutput', false); + row_xlims = arrayfun(@(x) [min(min(xlims_subplot(pos_subplot(:, 2) == row_subplots(x), :))), ... + max(max(xlims_subplot(pos_subplot(:, 2) == row_subplots(x), :)))], 1:size(row_subplots, 1), 'UniformOutput', false); + col_ylims = arrayfun(@(x) [min(min(ylims_subplot(pos_subplot(:, 1) == col_subplots(x), :))), ... + max(max(ylims_subplot(pos_subplot(:, 1) == col_subplots(x), :)))], 1:size(col_subplots, 1), 'UniformOutput', false); + row_ylims = arrayfun(@(x) [min(min(ylims_subplot(pos_subplot(:, 2) == row_subplots(x), :))), ... + max(max(ylims_subplot(pos_subplot(:, 2) == row_subplots(x), :)))], 1:size(row_subplots, 1), 'UniformOutput', false); + + for iAx = 1:size(all_axes, 2) + thisAx = all_axes(iAx); + currAx = currFig.Children(thisAx); + if ~isempty(currAx) + if ismember(options.XLimits, {'all'}) + set(currAx, 'Xlim', [min(min(xlims_subplot)), max(max(xlims_subplot))]); + end + if ismember(options.YLimits, {'all'}) + set(currAx, 'Ylim', [min(min(ylims_subplot)), max(max(ylims_subplot))]); + end + if ismember(options.XLimits, {'row'}) + set(currAx, 'Xlim', row_xlims{pos_subplot(iAx, 2) == row_subplots}); + end + if ismember(options.YLimits, {'row'}) + set(currAx, 'Ylim', row_ylims{pos_subplot(iAx, 2) == row_subplots}); + end + if ismember(options.XLimits, {'col'}) + set(currAx, 'Xlim', col_xlims{pos_subplot(iAx, 1) == col_subplots}); + end + if ismember(options.YLimits, {'col'}) + set(currAx, 'Ylim', col_ylims{pos_subplot(iAx, 1) == col_subplots}); + end + end + % set legend position + if ~isempty(currAx.Legend) + if options.LegendReplace + prettify_legend(currAx) + else + legend('Location', options.LegendLocation) + end + end + end +end +end + + diff --git a/prettify_plot/prettify_plot.m b/prettify_plot/prettify_plot.m index 178a1b6..5202fd2 100644 --- a/prettify_plot/prettify_plot.m +++ b/prettify_plot/prettify_plot.m @@ -1,200 +1,261 @@ -function prettify_plot(sameXLimits, sameYLimits, figureColor, legendAsTxt, titleFontSize, labelFontSize, ... - generalFontSize, pointSize, lineThickness, textColor) -% make current figure pretty -% ------ -% Inputs -% ------ -% - sameXLimits: string. Either: -% - 'none': don't change any of the xlimits -% - 'all': set all xlimits to the same values -% - 'row': set all xlimits to the same values for each subplot row -% - 'col': set all xlimits to the same values for each subplot col -% - sameYLimits: string. Either: -% - 'none': don't change any of the xlimits -% - 'all': set all xlimits to the same values -% - 'row': set all xlimits to the same values for each subplot row -% - 'col': set all xlimits to the same values for each subplot col -% - figureColor: string (e.g. 'w', 'k', ..) or RGB value defining the plots -% background color. -% - legendAsTxt: 1 if you want the legend box to be replace by text -% directly plotted on the figure, next to the each subplot's line/point -% - titleFontSize: double -% - labelFontSize: double -% - generalFontSize: double -% - pointSize: double -% - lineThickness: double -% - textColor: double -% ------ -% to do: -% - option to adjust vertical and horiz. lines -% - padding -% - fit data to plot (adjust lims) -% - font -% - padding / suptitles -% ------ -% Julie M. J. Fabre - -% Set default parameter values -if nargin < 1 || isempty(sameXLimits) - sameXLimits = 'none'; -end -if nargin < 2 || isempty(sameYLimits) - sameYLimits = 'none'; -end -if nargin < 3 || isempty(figureColor) - figureColor = 'w'; -end -if nargin < 4 || isempty(legendAsTxt) - legendAsTxt = 0; -end -if nargin < 5 || isempty(titleFontSize) - titleFontSize = 18; -end -if nargin < 6 || isempty(labelFontSize) - labelFontSize = 15; -end -if nargin < 7 || isempty(generalFontSize) - generalFontSize = 13; -end -if nargin < 8 || isempty(pointSize) - pointSize = 15; -end -if nargin < 9 || isempty(lineThickness) - lineThickness = 2; -end -if nargin < 10 || isempty(textColor) - % Set default font color based on the input color - switch figureColor - case 'k' - textColor = 'w'; - case 'none' - textColor = [0.7, 0.7, 0.7]; % Gray - otherwise - textColor = 'k'; - end -end - -% Get handles for current figure and axis -currFig = gcf; - - -% Set color properties for figure and axis -set(currFig, 'color', figureColor); - -% get axes children -all_axes = find(arrayfun(@(x) contains(currFig.Children(x).Type, 'axes'), 1:size(currFig.Children, 1))); - -for iAx = 1:size(all_axes, 2) - thisAx = all_axes(iAx); - currAx = currFig.Children(thisAx); - set(currAx, 'color', figureColor); - if ~isempty(currAx) - % Set font properties for the axis - try - set(currAx.XLabel, 'FontSize', labelFontSize, 'Color', textColor); - if strcmp(currAx.YAxisLocation, 'left') % if there is both a left and right yaxis, keep the colors - set(currAx.YLabel, 'FontSize', labelFontSize); - else - set(currAx.YLabel, 'FontSize', labelFontSize, 'Color', textColor); - end - set(currAx.Title, 'FontSize', titleFontSize, 'Color', textColor); - set(currAx, 'FontSize', generalFontSize, 'GridColor', textColor, ... - 'YColor', textColor, 'XColor', textColor, ... - 'MinorGridColor', textColor); - if ~isempty(currAx.Legend) - set(currAx.Legend, 'Color', figureColor, 'TextColor', textColor) - end - - % Adjust properties of line children within the plot - childLines = findall(currAx, 'Type', 'line'); - for thisLine = childLines' - if strcmp('.', get(thisLine, 'Marker')) - set(thisLine, 'MarkerSize', pointSize); - end - if strcmp('-', get(thisLine, 'LineStyle')) - set(thisLine, 'LineWidth', lineThickness); - end - end - - % Adjust properties of errorbars children within the plot - childErrBars = findall(currAx, 'Type', 'ErrorBar'); - for thisErrBar = childErrBars' - if strcmp('.', get(thisErrBar, 'Marker')) - set(thisErrBar, 'MarkerSize', pointSize); - end - if strcmp('-', get(thisErrBar, 'LineStyle')) - set(thisErrBar, 'LineWidth', lineThickness); - end - end - - % Get x and y limits - xlims_subplot(iAx, :) = currAx.XLim; - ylims_subplot(iAx, :) = currAx.YLim; - - % Get plot position - pos_subplot(iAx, :) = currAx.Position(1:2); % [left bottom width height - if ~isempty(currAx.Legend) - if legendAsTxt == 1 - prettify_legend(currAx) - else - legend('Location', 'best') - end - end - catch - end - end -end - - -% make x and y lims the same -if ismember(sameXLimits, {'all', 'row', 'col'}) || ismember(sameYLimits, {'all', 'row', 'col'}) - % get rows and cols - col_subplots = unique(pos_subplot(:, 1)); - row_subplots = unique(pos_subplot(:, 2)); - - col_xlims = arrayfun(@(x) [min(min(xlims_subplot(pos_subplot(:, 1) == col_subplots(x), :))), ... - max(max(xlims_subplot(pos_subplot(:, 1) == col_subplots(x), :)))], 1:size(col_subplots, 1), 'UniformOutput', false); - row_xlims = arrayfun(@(x) [min(min(xlims_subplot(pos_subplot(:, 2) == row_subplots(x), :))), ... - max(max(xlims_subplot(pos_subplot(:, 2) == row_subplots(x), :)))], 1:size(row_subplots, 1), 'UniformOutput', false); - col_ylims = arrayfun(@(x) [min(min(ylims_subplot(pos_subplot(:, 1) == col_subplots(x), :))), ... - max(max(ylims_subplot(pos_subplot(:, 1) == col_subplots(x), :)))], 1:size(col_subplots, 1), 'UniformOutput', false); - row_ylims = arrayfun(@(x) [min(min(ylims_subplot(pos_subplot(:, 2) == row_subplots(x), :))), ... - max(max(ylims_subplot(pos_subplot(:, 2) == row_subplots(x), :)))], 1:size(row_subplots, 1), 'UniformOutput', false); - - for iAx = 1:size(all_axes, 2) - thisAx = all_axes(iAx); - currAx = currFig.Children(thisAx); - if ~isempty(currAx) - %try - if ismember(sameXLimits, {'all'}) - set(currAx, 'Xlim', [min(min(xlims_subplot)), max(max(xlims_subplot))]); - end - if ismember(sameYLimits, {'all'}) - set(currAx, 'Ylim', [min(min(ylims_subplot)), max(max(ylims_subplot))]); - end - if ismember(sameXLimits, {'row'}) - set(currAx, 'Xlim', row_xlims{pos_subplot(iAx, 2) == row_subplots}); - end - if ismember(sameYLimits, {'row'}) - set(currAx, 'Ylim', row_ylims{pos_subplot(iAx, 2) == row_subplots}); - end - if ismember(sameXLimits, {'col'}) - set(currAx, 'Xlim', col_xlims{pos_subplot(iAx, 1) == col_subplots}); - end - if ismember(sameYLimits, {'col'}) - set(currAx, 'Ylim', col_ylims{pos_subplot(iAx, 1) == col_subplots}); - end - %catch - %end - end - % set legend position - if ~isempty(currAx.Legend) - if legendAsTxt == 1 - prettify_legend(currAx) - else - legend('Location', 'best') - end - end - end -end -end +function prettify_plot(varargin) +% make current figure pretty +% ------ +% Inputs: Name - Pair arguments +% ------ +% - XLimits: string or number. +% If a string, either: +% - 'keep': don't change any of the xlimits +% - 'same': set all xlimits to the same values +% - 'row': set all xlimits to the same values for each subplot row +% - 'col': set all xlimits to the same values for each subplot col +% If a number, 1 * 2 double setting the minimum and maximum values +% - YLimits: string or number. +% If a string, either: +% - 'keep': don't change any of the ylimits +% - 'same': set all ylimits to the same values +% - 'row': set all ylimits to the same values for each subplot row +% - 'col': set all ylimits to the same values for each subplot col +% If a number, 1 * 2 double setting the minimum and maximum values +% - FigureColor: string (e.g. 'w', 'k', 'Black', ..) or RGB value defining the plots +% background color. +% - TextColor: string (e.g. 'w', 'k', 'Black', ..) or RGB value defining the plots +% text color. +% - LegendLocation: string determining where the legend is. Either: +% 'north' Inside top of axes +% 'south' Inside bottom of axes +% 'east' Inside right of axes +% 'west' Inside left of axes +% 'northeast' Inside top-right of axes (default for 2-D axes) +% 'northwest' Inside top-left of axes +% 'southeast' Inside bottom-right of axes +% 'southwest' Inside bottom-left of axes +% 'northoutside' Above the axes +% 'southoutside' Below the axes +% 'eastoutside' To the right of the axes +% 'westoutside' To the left of the axes +% 'northeastoutside' Outside top-right corner of the axes (default for 3-D axes) +% 'northwestoutside' Outside top-left corner of the axes +% 'southeastoutside' Outside bottom-right corner of the axes +% 'southwestoutside' Outside bottom-left corner of the axes +% 'best' Inside axes where least conflict occurs with the plot data at the time that you create the legend. If the plot data changes, you might need to reset the location to 'best'. +% 'bestoutside' Outside top-right corner of the axes (when the legend has a vertical orientation) or below the axes (when the legend has a horizontal orientation) +% - LegendReplace: boolean, if you want the legend box to be replace by text +% directly plotted on the figure, next to the each subplot's line/point +% - titleFontSize: double +% - labelFontSize: double +% - generalFontSize: double +% - Font: string. See listfonts() for a list of all available fonts +% - pointSize: double +% - lineThickness: double +% - AxisTicks +% - AxisBox +% - AxisAspectRatio 'equal', 'square', 'image' +% - AxisTightness 'tickaligned' 'tight', 'padded' +% ------ +% to do: +% - option to adjust vertical and horiz. lines +% - padding +% - fit data to plot (adjust lims) +% - padding / suptitles +% ------ +% Julie M. J. Fabre + +% Set default parameter values +options = struct('XLimits', 'keep', ... + 'YLimits', 'keep', ... + 'FigureColor', [1, 1, 1], ... + 'TextColor', [0, 0, 0], ... + 'LegendLocation', 'best', ... + 'LegendReplace', false, ... + 'TitleFontSize', 15, ... + 'LabelFontSize', 15, ... + 'GeneralFontSize', 15, ... + 'Font', 'Arial', ... + 'BoldTitle', 'off', ... + 'PointSize', 15, ... + 'LineThickness', 2, ... + 'AxisTicks', 'out', ... + 'AxisBox', 'off', ... + 'AxisGrid', 'off',... + 'AxisAspectRatio', 'keep',... + 'AxisTightness', 'keep'); + +% read the acceptable names +optionNames = fieldnames(options); + +% count arguments +nArgs = length(varargin); +if round(nArgs/2) ~= nArgs / 2 + error('prettify_plot() needs propertyName/propertyValue pairs') +end + +for iPair = reshape(varargin, 2, []) % pair is {propName;propValue} + inputName = lower(iPair{1}); % make case insensitive + + if any(strcmp(inputName, optionNames)) + % overwrite options. If you want you can test for the right class here + % Also, if you find out that there is an option you keep getting wrong, + % you can use "if strcmp(inpName,'problemOption'),testMore,end"-statements + options.(inputName) = iPair{2}; + else + error('%s is not a recognized parameter name', inputName) + end +end + +% Check Name/Value pairs make sense +if ischar(options.FigureColor) || isstring(options.FigureColor) %convert to rgb + options.FigureColor = rgb(options.FigureColor); +end +if ischar(options.TextColor) || isstring(options.TextColor) %convert to rgb + options.TextColor = rgb(options.TextColor); +end +if sum(options.FigureColor-options.TextColor) <= 1.5 %check background and text and sufficiently different + if sum(options.FigureColor) >= 1.5 + options.TextColor = [0, 0, 0]; + else + options.TextColor = [1, 1, 1]; + end +end +% Get handles for current figure and axis +currFig = gcf; + + +% Set color properties for figure and axis +set(currFig, 'color', options.FigureColor); + +% get axes children +all_axes = find(arrayfun(@(x) contains(currFig.Children(x).Type, 'axes'), 1:size(currFig.Children, 1))); + +% update font +fontname(options.Font) + +% update (sub)plot properties +for iAx = 1:size(all_axes, 2) + thisAx = all_axes(iAx); + currAx = currFig.Children(thisAx); + set(currAx, 'color', options.FigureColor); + if ~isempty(currAx) + + % Set grid/box/tick options + set(currAx, 'TickDir', options.AxisTicks) + set(currAx, 'Box', options.AxisBox) + %set(currAx, 'Grid', options.AxisGrid) + if strcmp(options.AxisAspectRatio, 'keep') == 0 + axis(currAx, options.AxisAspectRatio) + end + if strcmp(options.AxisTightness, 'keep') == 0 + axis(currAx, options.AxisTightness) + end + + % Set text properties + set(currAx.XLabel, 'FontSize', options.LabelFontSize, 'Color', options.TextColor); + if strcmp(currAx.YAxisLocation, 'left') % if there is both a left and right yaxis, keep the colors + set(currAx.YLabel, 'FontSize', options.LabelFontSize); + else + set(currAx.YLabel, 'FontSize', options.LabelFontSize, 'Color', options.TextColor); + end + if strcmp(options.BoldTitle, 'on') + set(currAx.Title, 'FontSize', options.TitleFontSize, 'Color', options.TextColor, ... + 'FontWeight', 'Bold') + else + set(currAx.Title, 'FontSize', options.TitleFontSize, 'Color', options.TextColor, ... + 'FontWeight', 'Normal'); + end + set(currAx, 'FontSize', options.GeneralFontSize, 'GridColor', options.TextColor, ... + 'YColor', options.TextColor, 'XColor', options.TextColor, ... + 'MinorGridColor', options.TextColor); + if ~isempty(currAx.Legend) + set(currAx.Legend, 'Color', options.FigureColor, 'TextColor', options.TextColor) + end + + % Adjust properties of line children within the plot + childLines = findall(currAx, 'Type', 'line'); + for thisLine = childLines' + if strcmp('.', get(thisLine, 'Marker')) + set(thisLine, 'MarkerSize', options.PointSize); + end + if strcmp('-', get(thisLine, 'LineStyle')) + set(thisLine, 'LineWidth', options.LineThickness); + end + end + + % Adjust properties of errorbars children within the plot + childErrBars = findall(currAx, 'Type', 'ErrorBar'); + for thisErrBar = childErrBars' + if strcmp('.', get(thisErrBar, 'Marker')) + set(thisErrBar, 'MarkerSize', options.PointSize); + end + if strcmp('-', get(thisErrBar, 'LineStyle')) + set(thisErrBar, 'LineWidth', options.LineThickness); + end + end + + % Get x and y limits + xlims_subplot(iAx, :) = currAx.XLim; + ylims_subplot(iAx, :) = currAx.YLim; + + % Get plot position + pos_subplot(iAx, :) = currAx.Position(1:2); % [left bottom width height + if ~isempty(currAx.Legend) + if options.LegendReplace + prettify_legend(currAx) + else + legend('Location', options.LegendLocation) + end + end + end +end + + +% make x and y lims the same +if ismember(options.XLimits, {'all', 'row', 'col'}) || ismember(options.YLimits, {'all', 'row', 'col'}) + % get rows and cols + col_subplots = unique(pos_subplot(:, 1)); + row_subplots = unique(pos_subplot(:, 2)); + + col_xlims = arrayfun(@(x) [min(min(xlims_subplot(pos_subplot(:, 1) == col_subplots(x), :))), ... + max(max(xlims_subplot(pos_subplot(:, 1) == col_subplots(x), :)))], 1:size(col_subplots, 1), 'UniformOutput', false); + row_xlims = arrayfun(@(x) [min(min(xlims_subplot(pos_subplot(:, 2) == row_subplots(x), :))), ... + max(max(xlims_subplot(pos_subplot(:, 2) == row_subplots(x), :)))], 1:size(row_subplots, 1), 'UniformOutput', false); + col_ylims = arrayfun(@(x) [min(min(ylims_subplot(pos_subplot(:, 1) == col_subplots(x), :))), ... + max(max(ylims_subplot(pos_subplot(:, 1) == col_subplots(x), :)))], 1:size(col_subplots, 1), 'UniformOutput', false); + row_ylims = arrayfun(@(x) [min(min(ylims_subplot(pos_subplot(:, 2) == row_subplots(x), :))), ... + max(max(ylims_subplot(pos_subplot(:, 2) == row_subplots(x), :)))], 1:size(row_subplots, 1), 'UniformOutput', false); + + for iAx = 1:size(all_axes, 2) + thisAx = all_axes(iAx); + currAx = currFig.Children(thisAx); + if ~isempty(currAx) + if ismember(options.XLimits, {'all'}) + set(currAx, 'Xlim', [min(min(xlims_subplot)), max(max(xlims_subplot))]); + end + if ismember(options.YLimits, {'all'}) + set(currAx, 'Ylim', [min(min(ylims_subplot)), max(max(ylims_subplot))]); + end + if ismember(options.XLimits, {'row'}) + set(currAx, 'Xlim', row_xlims{pos_subplot(iAx, 2) == row_subplots}); + end + if ismember(options.YLimits, {'row'}) + set(currAx, 'Ylim', row_ylims{pos_subplot(iAx, 2) == row_subplots}); + end + if ismember(options.XLimits, {'col'}) + set(currAx, 'Xlim', col_xlims{pos_subplot(iAx, 1) == col_subplots}); + end + if ismember(options.YLimits, {'col'}) + set(currAx, 'Ylim', col_ylims{pos_subplot(iAx, 1) == col_subplots}); + end + end + % set legend position + if ~isempty(currAx.Legend) + if options.LegendReplace + prettify_legend(currAx) + else + legend('Location', options.LegendLocation) + end + end + end +end +end + + diff --git a/prettify_plot/prettify_plot_old.m b/prettify_plot/prettify_plot_old.m new file mode 100644 index 0000000..e930edf --- /dev/null +++ b/prettify_plot/prettify_plot_old.m @@ -0,0 +1,200 @@ +function prettify_plot_old(sameXLimits, sameYLimits, figureColor, legendAsTxt, titleFontSize, labelFontSize, ... + generalFontSize, pointSize, lineThickness, textColor) +% make current figure pretty +% ------ +% Inputs +% ------ +% - sameXLimits: string. Either: +% - 'none': don't change any of the xlimits +% - 'all': set all xlimits to the same values +% - 'row': set all xlimits to the same values for each subplot row +% - 'col': set all xlimits to the same values for each subplot col +% - sameYLimits: string. Either: +% - 'none': don't change any of the xlimits +% - 'all': set all xlimits to the same values +% - 'row': set all xlimits to the same values for each subplot row +% - 'col': set all xlimits to the same values for each subplot col +% - figureColor: string (e.g. 'w', 'k', ..) or RGB value defining the plots +% background color. +% - legendAsTxt: 1 if you want the legend box to be replace by text +% directly plotted on the figure, next to the each subplot's line/point +% - titleFontSize: double +% - labelFontSize: double +% - generalFontSize: double +% - pointSize: double +% - lineThickness: double +% - textColor: double +% ------ +% to do: +% - option to adjust vertical and horiz. lines +% - padding +% - fit data to plot (adjust lims) +% - font +% - padding / suptitles +% ------ +% Julie M. J. Fabre + +% Set default parameter values +if nargin < 1 || isempty(sameXLimits) + sameXLimits = 'none'; +end +if nargin < 2 || isempty(sameYLimits) + sameYLimits = 'none'; +end +if nargin < 3 || isempty(figureColor) + figureColor = 'w'; +end +if nargin < 4 || isempty(legendAsTxt) + legendAsTxt = 0; +end +if nargin < 5 || isempty(titleFontSize) + titleFontSize = 15; +end +if nargin < 6 || isempty(labelFontSize) + labelFontSize = 15; +end +if nargin < 7 || isempty(generalFontSize) + generalFontSize = 15; +end +if nargin < 8 || isempty(pointSize) + pointSize = 15; +end +if nargin < 9 || isempty(lineThickness) + lineThickness = 2; +end +if nargin < 10 || isempty(textColor) + % Set default font color based on the input color + switch figureColor + case 'k' + textColor = 'w'; + case 'none' + textColor = [0.7, 0.7, 0.7]; % Gray + otherwise + textColor = 'k'; + end +end + +% Get handles for current figure and axis +currFig = gcf; + + +% Set color properties for figure and axis +set(currFig, 'color', figureColor); + +% get axes children +all_axes = find(arrayfun(@(x) contains(currFig.Children(x).Type, 'axes'), 1:size(currFig.Children, 1))); + +for iAx = 1:size(all_axes, 2) + thisAx = all_axes(iAx); + currAx = currFig.Children(thisAx); + set(currAx, 'color', figureColor); + if ~isempty(currAx) + % Set font properties for the axis + try + set(currAx.XLabel, 'FontSize', labelFontSize, 'Color', textColor); + if strcmp(currAx.YAxisLocation, 'left') % if there is both a left and right yaxis, keep the colors + set(currAx.YLabel, 'FontSize', labelFontSize); + else + set(currAx.YLabel, 'FontSize', labelFontSize, 'Color', textColor); + end + set(currAx.Title, 'FontSize', titleFontSize, 'Color', textColor); + set(currAx, 'FontSize', generalFontSize, 'GridColor', textColor, ... + 'YColor', textColor, 'XColor', textColor, ... + 'MinorGridColor', textColor); + if ~isempty(currAx.Legend) + set(currAx.Legend, 'Color', figureColor, 'TextColor', textColor) + end + + % Adjust properties of line children within the plot + childLines = findall(currAx, 'Type', 'line'); + for thisLine = childLines' + if strcmp('.', get(thisLine, 'Marker')) + set(thisLine, 'MarkerSize', pointSize); + end + if strcmp('-', get(thisLine, 'LineStyle')) + set(thisLine, 'LineWidth', lineThickness); + end + end + + % Adjust properties of errorbars children within the plot + childErrBars = findall(currAx, 'Type', 'ErrorBar'); + for thisErrBar = childErrBars' + if strcmp('.', get(thisErrBar, 'Marker')) + set(thisErrBar, 'MarkerSize', pointSize); + end + if strcmp('-', get(thisErrBar, 'LineStyle')) + set(thisErrBar, 'LineWidth', lineThickness); + end + end + + % Get x and y limits + xlims_subplot(iAx, :) = currAx.XLim; + ylims_subplot(iAx, :) = currAx.YLim; + + % Get plot position + pos_subplot(iAx, :) = currAx.Position(1:2); % [left bottom width height + if ~isempty(currAx.Legend) + if legendAsTxt == 1 + prettify_legend(currAx) + else + legend('Location', 'best') + end + end + catch + end + end +end + + +% make x and y lims the same +if ismember(sameXLimits, {'all', 'row', 'col'}) || ismember(sameYLimits, {'all', 'row', 'col'}) + % get rows and cols + col_subplots = unique(pos_subplot(:, 1)); + row_subplots = unique(pos_subplot(:, 2)); + + col_xlims = arrayfun(@(x) [min(min(xlims_subplot(pos_subplot(:, 1) == col_subplots(x), :))), ... + max(max(xlims_subplot(pos_subplot(:, 1) == col_subplots(x), :)))], 1:size(col_subplots, 1), 'UniformOutput', false); + row_xlims = arrayfun(@(x) [min(min(xlims_subplot(pos_subplot(:, 2) == row_subplots(x), :))), ... + max(max(xlims_subplot(pos_subplot(:, 2) == row_subplots(x), :)))], 1:size(row_subplots, 1), 'UniformOutput', false); + col_ylims = arrayfun(@(x) [min(min(ylims_subplot(pos_subplot(:, 1) == col_subplots(x), :))), ... + max(max(ylims_subplot(pos_subplot(:, 1) == col_subplots(x), :)))], 1:size(col_subplots, 1), 'UniformOutput', false); + row_ylims = arrayfun(@(x) [min(min(ylims_subplot(pos_subplot(:, 2) == row_subplots(x), :))), ... + max(max(ylims_subplot(pos_subplot(:, 2) == row_subplots(x), :)))], 1:size(row_subplots, 1), 'UniformOutput', false); + + for iAx = 1:size(all_axes, 2) + thisAx = all_axes(iAx); + currAx = currFig.Children(thisAx); + if ~isempty(currAx) + %try + if ismember(sameXLimits, {'all'}) + set(currAx, 'Xlim', [min(min(xlims_subplot)), max(max(xlims_subplot))]); + end + if ismember(sameYLimits, {'all'}) + set(currAx, 'Ylim', [min(min(ylims_subplot)), max(max(ylims_subplot))]); + end + if ismember(sameXLimits, {'row'}) + set(currAx, 'Xlim', row_xlims{pos_subplot(iAx, 2) == row_subplots}); + end + if ismember(sameYLimits, {'row'}) + set(currAx, 'Ylim', row_ylims{pos_subplot(iAx, 2) == row_subplots}); + end + if ismember(sameXLimits, {'col'}) + set(currAx, 'Xlim', col_xlims{pos_subplot(iAx, 1) == col_subplots}); + end + if ismember(sameYLimits, {'col'}) + set(currAx, 'Ylim', col_ylims{pos_subplot(iAx, 1) == col_subplots}); + end + %catch + %end + end + % set legend position + if ~isempty(currAx.Legend) + if legendAsTxt == 1 + prettify_legend(currAx) + else + legend('Location', 'best') + end + end + end +end +end