How to make professional looking plots in MATLAB: Plus Zoom plots

 


Sometimes you need to have a professional-looking plot in Matlab that is in vector format, follows latex visuals and is ready for use in publications and manuscripts.

The following code can be a starting point to have a professional-looking plot in Matlab that meets the above requirements.

Please see the GitHub code for the latest version of this repository. {alertWarning}

Note that the code is written in Matlab R2020b. {alertWarning}

clc; % clear workspace clear; % clear command window close all % close all open figures %% preparing data x = 0:0.01:10; y1 = sin(x); y2 = cos(x); %% plotting titles_font_size =15; % stores title font size labels_font_size=20; % stores labels font size font_name = 'Times'; % stores font name line_width=1.5; % stores line width (thickness of trend lines) f=figure('visible','on'); % adds a new figure set(gcf, 'Units', 'Normalized', 'OuterPosition', [0.2, 0.2, 0.6, 0.7]); % set figure size set(gcf, 'Color', 'w') %set the figure background to white instead of default grey plot(x, y1,'r-', x, y2,'b-', 'LineWidth', line_width) % plot data title('$Sin(x) + Cos(x)\ trends$','Fontsize',labels_font_size,'Fontname',font_name, 'interpreter','latex') % add title xlabel('$X\ [-]$','Fontsize',labels_font_size,'Fontname',font_name, 'interpreter','latex') % add x label ylabel('$Output\ Y\ [-]$','Fontsize',labels_font_size,'Fontname',font_name, 'interpreter','latex') % add y label grid on % add grid lines set(gca,'GridLineStyle','--') % set grid line style legend('$Sin(x)$','$Cos(x)$', 'interpreter','latex') % add legend set(gca,'Fontsize',labels_font_size,'Fontname',font_name,'ycolor','k') % set label font name and size ax = gca; % get the current axis ti = ax.TightInset; % find current axis tight layout ax.Position = [ti(1), ti(2), 1 - ti(1) - ti(3), 1 - ti(2) - ti(4)]; % remove figures extra empty padding around axis saveas(gcf,'plot.svg') % save figure in svg (vector) format in the current directory exportgraphics(gcf,'plot.pdf') % save figure in svg (vector) format in the current directory, only in MATLAB 2020+ for cleaner looks {codeBox}
You can use exportgraphics(object,filename) as in MATLAB 2020+ for cleaner looks in saved figures. Note that you cannot use exportgraphics() with SVG format yet but you can export as PDF and use InkScape to convert it to SVG. {alertInfo}

 This would result in a figure similar to below


The full code, data, and sample plot used for this tutorial can be found below

{getButton} $text={Download the code package GitHub} $icon={download}

If you need to add zoomPlots to your figures, you can use the custom function written by Dr. Rahimi (zoomPlot.m) to easily add zoomed axes to your current axis that can make the content of your plots easier to read and clearer to follow.

{getButton} $text={Download the code package GitHub} $icon={download}

Additionally, if you need to add zoom plots to your figure, you can see the interactive tool from the MATLAB central by Kepeng Qiu called ironically, Zoomplot



Post a Comment

Previous Post Next Post