123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- function enhance_figures(fig_handle, varargin)
- %% Enhance figures after creation
- % This function may be applied on all standard plots. It changes the
- % appearence to create consistent figures to integrate in text files. Lines
- % and axes are plotted thicker and texts are enlarged. Titles and subtitles
- % are removed if not specified otherwise (for the titles).
- % -----
- % Input [optional arguments are name value pairs]
- % -----
- % fig_handle the figure handle of the figure that should
- % be enhanced
- % 'KeepTitles' If set like that, plot titles won't be
- % (optional) removed. Subtitles will still be removed.
- %
- % The function can handle tiled layouts, simple plots, scatter plots,
- % error bar plots and stacked plots
- %
- % ------
- % Output
- % ------
- % no output, existing figure is modified
- % -------------------------------------------------------------------------
- % Program by Bjarne Schultze [last modified 07.03.2022]
- % -------------------------------------------------------------------------
- %% Preparations
- % set the master values for the main features
- masterFontSize = 12; masterLineWidth = 1; masterTitleSizeMulti = 1.3;
- masterAxColor = [0,0,0];
- % define a new color order
- masterColorOrder = [0 0.34 0.64; 1 0.7 0; 0.35 0.73 0; 0.5 0 0.6; ...
- 0.2 0.7 0.94];
- % read user input for a request to keep the plot title(s)
- if isequal(nargin,2) && isequal(varargin{1}, 'KeepTitles')
- titles = 1;
- else
- titles = 0;
- end
- %% Modifying the figure
- % resize the figure
- fig_handle.Position = [328, 197, 891, 562];
- % get the handle to the child/children of the figure
- children = fig_handle.Children;
- % For a tiled layout ------------------------------------------------------
- if isequal(children(1).Type, 'tiledlayout')
- % minizize white space around tiles
- children.TileSpacing = 'compact';
- children.Padding = 'compact';
- % get the child/children of the tiledlayout (usually axes)
- grand_children = children.Children;
- % iterate through the tiledlayout children
- for child = 1:length(grand_children)
- % ensure that the grandchild has the right type
- if isequal(grand_children(child).Type, 'axes')
- % adapt size of labels and axes lines and title
- grand_children(child).FontSize = masterFontSize;
- grand_children(child).LineWidth = masterLineWidth;
- grand_children(child).TitleFontSizeMultiplier = ...
- masterTitleSizeMulti;
- grand_children(child).TitleHorizontalAlignment = 'left';
- % change axes colors to black
- grand_children(child).XColor = masterAxColor;
- grand_children(child).YColor = masterAxColor;
- grand_children(child).ZColor = masterAxColor;
- % set new color order
- grand_children(child).ColorOrder = masterColorOrder;
- % remove subtitle and, if requested, tilte
- if ~titles
- grand_children(child).Title = [];
- end
- grand_children(child).Subtitle = [];
-
- % get the child of the axes (usually line or scatter)
- ggrand_child = grand_children(child).Children;
- % preassign vectors to search for the biggest and smallest Y
- % value among all data traces (lines) in the following
- maxY = -100;
- minY = 100;
- % set/renew the flag for the case a histogram is found
- histFlag = 0;
- for line = 1:length(ggrand_child)
- % ensure the grand-grandchild has the correct type
- if isequal(ggrand_child(line).Type,'line')
- % adapt the line width of the plot line
- ggrand_child(line).LineWidth = masterLineWidth;
- % search for bissgest and smallest y value
- if max(ggrand_child(line).YData) > maxY
- maxY = max(ggrand_child(line).YData);
- end
- if min(ggrand_child(line).YData) < minY
- minY = min(ggrand_child(line).YData);
- end
- elseif isequal(ggrand_child(line).Type,'histogram')
- histFlag = 1;
- % adapt the line width of the lines around the bars
- ggrand_child(line).LineWidth = masterLineWidth;
- ggrand_child(line).FaceAlpha = 0.8;
- elseif isequal(ggrand_child(line).Type, 'scatter')
- % adapt the line width of the plot line
- ggrand_child(line).LineWidth = masterLineWidth;
- % adpat the marker size
- ggrand_child(line).SizeData = 55;
- else
- histFlag = 1;
- continue
- end
- end
- % adjust the scaling of the y axis
- if ~histFlag && ~isequal(ggrand_child(line).Type, 'scatter')
- rangeY = maxY - minY;
- maxY = maxY + rangeY*0.1;
- minY = minY - rangeY*0.1;
- grand_children(child).YLim = [minY, maxY];
- end
- elseif isequal(grand_children(child).Type, 'stackedplot')
- % get the stacked plot handle
- stckplt = grand_children(child);
- % adapt the size for plot lines and text
- stckplt.FontSize = masterFontSize;
- stckplt.LineWidth = masterLineWidth;
- % get the corresponding axes
- ax = findobj(stckplt.NodeChildren, 'Type','Axes');
- % rotate the y-axes labels by 90 degrees like in a normal plot
- set([ax.YLabel],'Rotation',90,'HorizontalAlignment', ...
- 'Center','VerticalAlignment', 'Bottom');
- % adapt the size for axis lines and the title
- ax(2).LineWidth = 1; ax(1).LineWidth = masterLineWidth;
- ax(2).TitleFontSizeMultiplier = masterTitleSizeMulti;
- ax(2).TitleHorizontalAlignment = 'left';
- % change axes colors to black
- ax(2).XColor = masterAxColor; ax(1).XColor = masterAxColor;
- ax(2).YColor = masterAxColor; ax(1).YColor = masterAxColor;
- % adapt the y-axis scaling
- rangeY = max(abs(stckplt.YData));
- maxY = max(stckplt.YData) + 0.05*rangeY;
- minY = min(stckplt.YData) - 0.05*rangeY;
- ax(2).YLim = [minY(1),maxY(1)]; ax(1).YLim = [minY(2),maxY(2)];
- else
- continue
- end
- end
- % For a 'normal' 1x1 layout -----------------------------------------------
- elseif isequal(children(end).Type, 'axes')
- % remove subtitles and, if requested, titles
- if ~titles
- children(end).Title = [];
- end
- children(end).Subtitle = [];
- % adapt the axes line width and text size
- children(end).LineWidth = masterLineWidth;
- children(end).FontSize = masterFontSize;
- children(end).TitleFontSizeMultiplier = masterTitleSizeMulti;
- children(end).TitleHorizontalAlignment = 'left';
- % change the axes colors
- children(end).XColor = masterAxColor;
- children(end).YColor = masterAxColor;
- children(end).ZColor = masterAxColor;
- % set new color order
- children(end).ColorOrder = masterColorOrder;
-
- % get the grand child/children of the axes
- grand_children = children(end).Children;
- for line = 1:length(grand_children)
- % ensure a compatible type
- if isequal(grand_children(line).Type, 'line') || ...
- isequal(grand_children(line).Type, 'errorbar') || ...
- isequal(grand_children(line).Type, 'constantline')
- % adapt the line width
- grand_children(line).LineWidth = masterLineWidth;
- elseif isequal(grand_children(line).Type,'scatter')
- % adapt the line width
- grand_children(line).LineWidth = masterLineWidth;
- % adpat the marker size
- grand_children(line).SizeData = 55;
- else
- continue
- end
- end
- end
- % end of function definition
- end
|