-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.m
More file actions
169 lines (130 loc) · 8.19 KB
/
script.m
File metadata and controls
169 lines (130 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
clc; % clear workspace
clear; % clear command window
close all % close all open figures
%% preparing data
x = 0:0.01:2;
y1 = sin(10*x);
y2 = cos(20*x);
%% plotting properties
titles_font_size =15; % stores title font size
labels_font_size=20; % stores labels font size
legend_font_size = 13; % stores legend font size
font_name = 'Times'; % stores font name
line_width=1.5; % stores line width (thickness of trend lines)
num_of_x_grid = 10;
legend_line_width = 14;
legened_marker_size = 5;
legend_columns = 2;
%% plotting single plots
fig=figure('visible','on'); % adds a new figure with pre-defined position
set(fig, 'Units', 'Normalized', 'OuterPosition', [0.2, 0.2, 0.6, 0.7]); % set figure size
set(fig, 'Color', 'w') %set the figure background to white instead of default grey
% use this in MATLAB 2021+ for perfect tight layout results
tiledlayout(1,1, "TileSpacing","tight","Padding","tight")
nexttile
plot(x, y1,'r-','Displayname','$Sin(x)$', 'LineWidth', line_width) % plot data 1
hold on
plot(x, y2,'b-','Displayname','$Cos(x)$', 'LineWidth', line_width) % plot data 2
title('$Sin(x) + Cos(x)\ trends$', 'interpreter','latex','FontSize',titles_font_size) % add title
xlabel('$X\ [-]$', 'interpreter','latex','FontSize',labels_font_size) % add x label
ylabel('$Output\ Y\ [-]$', 'interpreter','latex','FontSize',labels_font_size) % add y label
grid on % add grid lines
set(gca,'GridLineStyle','--') % set grid line style
leg = legend('interpreter','latex','location','southwest', ...
'Orientation','horizontal','NumColumns',legend_columns,'FontSize',legend_font_size); % add legend
leg.ItemTokenSize = [legend_line_width,legened_marker_size]; %set legend marker size and line width, default is line_width=30, marker_size=18 for chaning the line size in legend
%set(gca,'Fontsize',labels_font_size,'Fontname',font_name,'ycolor','k') % set label font name and size
%if you want to change the font name and size in all elements of the figure
%use the target in the fontsize and fontname as fig, otherwise use the
%target as individual axes
fontsize(fig,labels_font_size,"points") % set label font name and size
fontname(fig,font_name) % set label font name and size
% use only one of the following options
ax(1) = gca; % get the current axis
ylim([-5,2])
ax(1).YAxis.Exponent = 1; %if you need to make the axis numbers exponent form
ytickformat('%.1f') % the format for the exponent form on the axis ticks
box on % set axis box on/off
% if you wish to add a zoom plot use
% note that if you leave the 2nd, 3rd, and 4th, elements in the function as
% empty (i.e.,[]) then the function will work interactively to help you
% place your zoomed axes on your main axis. It will also give you the final
% elements in the command window so you can use it in future to hard-code
% them in your main script after you identify the elements interactively
zoomPlot(ax(1),[],[],[],"showTicks","off");
set(fig, 'PaperPositionMode', 'auto') % note that needs to be set to avoid undesired print/save results
saveas(fig,'plot_single.svg') % save figure in svg (vector) format in the current directory
saveas(fig,'plot_single.png') % save figure in png format in the current directory
%% plotting multiple (sub) plots
% for this purpose we can use a great function developed by Eduard Reitmann
% note that you have to make your figure invisible first for this function
% to work properly and after all your work is done make it visible again
fig=figure('visible','off'); % adds a new figure with pre-defined position
set(fig, 'Units', 'Normalized', 'OuterPosition', [0.2, 0.2, 0.6, 0.7]); % set figure size
set(fig, 'Color', 'w') %set the figure background to white instead of default grey
% use this in MATLAB 2021+ for perfect tight layout results
tiledlayout(2,2, "TileSpacing","tight","Padding","tight")
ax(1) = nexttile;
plot(x, y1,'r-','Displayname','$Sin(x)$', 'LineWidth', line_width) % plot data 1
title('$Sin(x)\ trends$', 'interpreter','latex','FontSize',titles_font_size) % add title
xlabel('$X\ [-]$', 'interpreter','latex','FontSize',labels_font_size) % add x label
ylabel('$Output\ Y\ [-]$', 'interpreter','latex','FontSize',labels_font_size) % add y label
grid on % add grid lines
set(gca,'GridLineStyle','--') % set grid line style
ax(1).YAxis.Exponent = 1; %if you need to make the axis numbers exponent form
ytickformat('%.1f') % the format for the exponent form on the axis ticks
leg = legend('interpreter','latex','location','best','Orientation','horizontal', ...
'NumColumns',legend_columns,'FontSize',legend_font_size); % add legend
leg.ItemTokenSize = [legend_line_width,legened_marker_size]; %set legend marker size and line width, default is line_width=30, marker_size=18 for chaning the line size in legend
%if you want to change the font name and size in all elements of the figure
%use the target in the fontsize and fontname as fig, otherwise use the
%target as individual axes
fontsize(ax(1),labels_font_size,"points") % set label font name and size
fontname(ax(1),font_name) % set label font name and size
ax(2) = nexttile;
plot(x, y2,'b-','Displayname','$Cos(x)$', 'LineWidth', line_width) % plot data 2
title('$Cos(x)\ trends$', 'interpreter','latex','FontSize',titles_font_size) % add title
xlabel('$X\ [-]$', 'interpreter','latex','FontSize',labels_font_size) % add x label
ylabel('$Output\ Y\ [-]$', 'interpreter','latex','FontSize',labels_font_size) % add y label
grid on % add grid lines
set(gca,'GridLineStyle','--') % set grid line style
ax(2).YAxis.Exponent = 1; %if you need to make the axis numbers exponent form
ytickformat('%.1f') % the format for the exponent form on the axis ticks
leg = legend('interpreter','latex','location','best','Orientation','horizontal', ...
'NumColumns',legend_columns,'FontSize',legend_font_size); % add legend
leg.ItemTokenSize = [legend_line_width,legened_marker_size]; %set legend marker size and line width, default is line_width=30, marker_size=18 for chaning the line size in legend
%if you want to change the font name and size in all elements of the figure
%use the target in the fontsize and fontname as fig, otherwise use the
%target as individual axes
fontsize(ax(2),labels_font_size,"points") % set label font name and size
fontname(ax(2),font_name) % set label font name and size
ax(3) = nexttile([1,2]);
plot(x, y1+y2,'g-','Displayname','$Sin(x)+Cos(x)$', 'LineWidth', line_width) % plot data 2
title('$Sin(x) + Cos(x)\ trends$', 'interpreter','latex','FontSize',titles_font_size) % add title
xlabel('$X\ [-]$', 'interpreter','latex','FontSize',labels_font_size) % add x label
ylabel('$Output\ Y\ [-]$', 'interpreter','latex','FontSize',labels_font_size) % add y label
grid on % add grid lines
set(gca,'GridLineStyle','--') % set grid line style
leg = legend('interpreter','latex','location','best', ...
'Orientation','horizontal','NumColumns',legend_columns,'FontSize',legend_font_size); % add legend
leg.ItemTokenSize = [legend_line_width,legened_marker_size]; %set legend marker size and line width, default is line_width=30, marker_size=18 for chaning the line size in legend
%if you want to change the font name and size in all elements of the figure
%use the target in the fontsize and fontname as fig, otherwise use the
%target as individual axes
fontsize(ax(3),labels_font_size,"points") % set label font name and size
fontname(ax(3),font_name) % set label font name and size
ylim([-5,2])
ax(3).YAxis.Exponent = 1; %if you need to make the axis numbers exponent form
ytickformat('%.1f') % the format for the exponent form on the axis ticks
box on % set axis box on/off
% if you wish to add a zoom plot use
% note that if you leave the 2nd, 3rd, and 4th, elements in the function as
% empty (i.e.,[]) then the function will work interactively to help you
% place your zoomed axes on your main axis. It will also give you the final
% elements in the command window so you can use it in future to hard-code
% them in your main script after you identify the elements interactively
%zoomPlot(ax,[],[],[],"showTicks","off");
set(fig,'Visible','on')
set(fig, 'PaperPositionMode', 'auto') % note that needs to be set to avoid undesired print/save results
saveas(fig,'plot_multiple.svg') % save figure in svg (vector) format in the current directory
saveas(fig,'plot_multiple.png') % save figure in png format in the current directory