enhance_figures.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. function enhance_figures(fig_handle, varargin)
  2. %% Enhance figures after creation
  3. % This function may be applied on all standard plots. It changes the
  4. % appearence to create consistent figures to integrate in text files. Lines
  5. % and axes are plotted thicker and texts are enlarged. Titles and subtitles
  6. % are removed if not specified otherwise (for the titles).
  7. % -----
  8. % Input [optional arguments are name value pairs]
  9. % -----
  10. % fig_handle the figure handle of the figure that should
  11. % be enhanced
  12. % 'KeepTitles' If set like that, plot titles won't be
  13. % (optional) removed. Subtitles will still be removed.
  14. %
  15. % The function can handle tiled layouts, simple plots, scatter plots,
  16. % error bar plots and stacked plots
  17. %
  18. % ------
  19. % Output
  20. % ------
  21. % no output, existing figure is modified
  22. % -------------------------------------------------------------------------
  23. % Program by Bjarne Schultze [last modified 07.03.2022]
  24. % -------------------------------------------------------------------------
  25. %% Preparations
  26. % set the master values for the main features
  27. masterFontSize = 12; masterLineWidth = 1; masterTitleSizeMulti = 1.3;
  28. masterAxColor = [0,0,0];
  29. % define a new color order
  30. masterColorOrder = [0 0.34 0.64; 1 0.7 0; 0.35 0.73 0; 0.5 0 0.6; ...
  31. 0.2 0.7 0.94];
  32. % read user input for a request to keep the plot title(s)
  33. if isequal(nargin,2) && isequal(varargin{1}, 'KeepTitles')
  34. titles = 1;
  35. else
  36. titles = 0;
  37. end
  38. %% Modifying the figure
  39. % resize the figure
  40. fig_handle.Position = [328, 197, 891, 562];
  41. % get the handle to the child/children of the figure
  42. children = fig_handle.Children;
  43. % For a tiled layout ------------------------------------------------------
  44. if isequal(children(1).Type, 'tiledlayout')
  45. % minizize white space around tiles
  46. children.TileSpacing = 'compact';
  47. children.Padding = 'compact';
  48. % get the child/children of the tiledlayout (usually axes)
  49. grand_children = children.Children;
  50. % iterate through the tiledlayout children
  51. for child = 1:length(grand_children)
  52. % ensure that the grandchild has the right type
  53. if isequal(grand_children(child).Type, 'axes')
  54. % adapt size of labels and axes lines and title
  55. grand_children(child).FontSize = masterFontSize;
  56. grand_children(child).LineWidth = masterLineWidth;
  57. grand_children(child).TitleFontSizeMultiplier = ...
  58. masterTitleSizeMulti;
  59. grand_children(child).TitleHorizontalAlignment = 'left';
  60. % change axes colors to black
  61. grand_children(child).XColor = masterAxColor;
  62. grand_children(child).YColor = masterAxColor;
  63. grand_children(child).ZColor = masterAxColor;
  64. % set new color order
  65. grand_children(child).ColorOrder = masterColorOrder;
  66. % remove subtitle and, if requested, tilte
  67. if ~titles
  68. grand_children(child).Title = [];
  69. end
  70. grand_children(child).Subtitle = [];
  71. % get the child of the axes (usually line or scatter)
  72. ggrand_child = grand_children(child).Children;
  73. % preassign vectors to search for the biggest and smallest Y
  74. % value among all data traces (lines) in the following
  75. maxY = -100;
  76. minY = 100;
  77. % set/renew the flag for the case a histogram is found
  78. histFlag = 0;
  79. for line = 1:length(ggrand_child)
  80. % ensure the grand-grandchild has the correct type
  81. if isequal(ggrand_child(line).Type,'line')
  82. % adapt the line width of the plot line
  83. ggrand_child(line).LineWidth = masterLineWidth;
  84. % search for bissgest and smallest y value
  85. if max(ggrand_child(line).YData) > maxY
  86. maxY = max(ggrand_child(line).YData);
  87. end
  88. if min(ggrand_child(line).YData) < minY
  89. minY = min(ggrand_child(line).YData);
  90. end
  91. elseif isequal(ggrand_child(line).Type,'histogram')
  92. histFlag = 1;
  93. % adapt the line width of the lines around the bars
  94. ggrand_child(line).LineWidth = masterLineWidth;
  95. ggrand_child(line).FaceAlpha = 0.8;
  96. elseif isequal(ggrand_child(line).Type, 'scatter')
  97. % adapt the line width of the plot line
  98. ggrand_child(line).LineWidth = masterLineWidth;
  99. % adpat the marker size
  100. ggrand_child(line).SizeData = 55;
  101. else
  102. histFlag = 1;
  103. continue
  104. end
  105. end
  106. % adjust the scaling of the y axis
  107. if ~histFlag && ~isequal(ggrand_child(line).Type, 'scatter')
  108. rangeY = maxY - minY;
  109. maxY = maxY + rangeY*0.1;
  110. minY = minY - rangeY*0.1;
  111. grand_children(child).YLim = [minY, maxY];
  112. end
  113. elseif isequal(grand_children(child).Type, 'stackedplot')
  114. % get the stacked plot handle
  115. stckplt = grand_children(child);
  116. % adapt the size for plot lines and text
  117. stckplt.FontSize = masterFontSize;
  118. stckplt.LineWidth = masterLineWidth;
  119. % get the corresponding axes
  120. ax = findobj(stckplt.NodeChildren, 'Type','Axes');
  121. % rotate the y-axes labels by 90 degrees like in a normal plot
  122. set([ax.YLabel],'Rotation',90,'HorizontalAlignment', ...
  123. 'Center','VerticalAlignment', 'Bottom');
  124. % adapt the size for axis lines and the title
  125. ax(2).LineWidth = 1; ax(1).LineWidth = masterLineWidth;
  126. ax(2).TitleFontSizeMultiplier = masterTitleSizeMulti;
  127. ax(2).TitleHorizontalAlignment = 'left';
  128. % change axes colors to black
  129. ax(2).XColor = masterAxColor; ax(1).XColor = masterAxColor;
  130. ax(2).YColor = masterAxColor; ax(1).YColor = masterAxColor;
  131. % adapt the y-axis scaling
  132. rangeY = max(abs(stckplt.YData));
  133. maxY = max(stckplt.YData) + 0.05*rangeY;
  134. minY = min(stckplt.YData) - 0.05*rangeY;
  135. ax(2).YLim = [minY(1),maxY(1)]; ax(1).YLim = [minY(2),maxY(2)];
  136. else
  137. continue
  138. end
  139. end
  140. % For a 'normal' 1x1 layout -----------------------------------------------
  141. elseif isequal(children(end).Type, 'axes')
  142. % remove subtitles and, if requested, titles
  143. if ~titles
  144. children(end).Title = [];
  145. end
  146. children(end).Subtitle = [];
  147. % adapt the axes line width and text size
  148. children(end).LineWidth = masterLineWidth;
  149. children(end).FontSize = masterFontSize;
  150. children(end).TitleFontSizeMultiplier = masterTitleSizeMulti;
  151. children(end).TitleHorizontalAlignment = 'left';
  152. % change the axes colors
  153. children(end).XColor = masterAxColor;
  154. children(end).YColor = masterAxColor;
  155. children(end).ZColor = masterAxColor;
  156. % set new color order
  157. children(end).ColorOrder = masterColorOrder;
  158. % get the grand child/children of the axes
  159. grand_children = children(end).Children;
  160. for line = 1:length(grand_children)
  161. % ensure a compatible type
  162. if isequal(grand_children(line).Type, 'line') || ...
  163. isequal(grand_children(line).Type, 'errorbar') || ...
  164. isequal(grand_children(line).Type, 'constantline')
  165. % adapt the line width
  166. grand_children(line).LineWidth = masterLineWidth;
  167. elseif isequal(grand_children(line).Type,'scatter')
  168. % adapt the line width
  169. grand_children(line).LineWidth = masterLineWidth;
  170. % adpat the marker size
  171. grand_children(line).SizeData = 55;
  172. else
  173. continue
  174. end
  175. end
  176. end
  177. % end of function definition
  178. end