-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHarryPlotter.m
315 lines (294 loc) · 11.7 KB
/
HarryPlotter.m
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% HarryPlotter
% version 1.0.0
%
% A simple plotting library for MATLAB to minimize wasted white space
% around multiple plots on the same figure. Usage instructions available at
% https://github.com/OrcaPanda/HarryPlotter
%
% Created by: Jianwei Sun
%
% GitHub: https://github.com/OrcaPanda/HarryPlotter
% MATLAB Central File Exchange: https://www.mathworks.com/matlabcentral/fileexchange/71730-harryplotter
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
classdef HarryPlotter < handle
properties(Access = public)
% Padding between all drawable areas in plot (pixels)
padding = 10;
name;
end
properties(Access = private)
fig;
% Plotting grid and size
rows;
cols;
gridMatrix;
% Plot handles
plots = [];
% Size of each drawable block. Recalculated on each resize
blockWidth;
blockHeight;
end
methods(Access = public)
%
% Constructor
%
function obj = HarryPlotter(arg1, arg2, arg3)
% Determine how the constructor is called
switch(nargin)
case 1
name = arg1;
rows = 1;
cols = 1;
case 2
name = "No Name";
rows = arg1;
cols = arg2;
case 3
name = arg1;
rows = arg2;
cols = arg3;
otherwise
name = "No Name";
rows = 1;
cols = 1;
end
% Figure name
if(ischar(name))
name = string(name);
end
if(isstring(name))
obj.name = name;
else
error("Name must be a string.");
end
% Figure grid matrix
if(isnumeric(rows) && isnumeric(cols))
if(rows >= 1 && cols >= 1)
obj.rows = rows;
obj.cols = cols;
obj.gridMatrix = zeros(round(rows), round(cols));
else
error("Rows and Cols must be greater than or equal to 1.");
end
else
error("Rows and Cols must be numbers.");
end
% Create a new figure and get the handle. Note, calling gcf
% will just return the currently open figure
obj.fig = figure;
obj.fig.Name = obj.name;
% Calculate the plotting block sizes
obj.blockWidth = floor((obj.fig.Position(3) - (obj.cols + 1) * obj.padding) / (obj.cols));
obj.blockHeight = floor((obj.fig.Position(4) - (obj.rows + 1) * obj.padding) / (obj.rows));
% Register the callback to redraw the plot on each resize
obj.fig.SizeChangedFcn = @obj.redraw;
% Register the callback when the figure window is closed
obj.fig.CloseRequestFcn = @obj.on_window_close;
if(rows == 1 && cols == 1)
obj.subplot(1,1,1,1);
end
end
%
% Destructor
%
function delete(obj)
fprintf(['Deleted "', char(obj.name), '".\n']);
delete(obj.fig);
end
%
% Call this function to update the figure
%
function update(obj)
set(findall(obj.fig, '-property', 'FontSize'), 'FontSize', 14);
graphicsObjects = findall(obj.fig, '-property', 'LineWidth');
for i = 1:length(graphicsObjects)
if(isa(graphicsObjects(i), 'matlab.graphics.chart.primitive.Line'))
graphicsObjects(i).LineWidth = 2;
end
end
obj.redraw();
end
%
% Set the current plotting axes
%
function ax = subplot(obj, varargin)
if(isempty(varargin))
availability = false;
for r = 1:obj.rows
for c = 1:obj.cols
if(obj.gridMatrix(r,c) == 0)
availability = true;
row = r;
col = c;
break;
end
end
if(availability)
break;
end
end
if(~availability)
error("Plot is full.");
end
dimensions = [row, col, 1, 1];
elseif(length(varargin) == 2)
dimensions = [varargin{1}, varargin{2}, 1, 1];
elseif(length(varargin) == 4)
dimensions = [varargin{1}, varargin{2}, varargin{3}, varargin{4}];
else
error("Incorrect number of arguments.");
end
row = dimensions(1);
col = dimensions(2);
if(length(dimensions) == 4)
height = dimensions(3);
width = dimensions(4);
else
height = 1;
width = 1;
end
% Check index bounds
if(row < 1 || row > obj.rows || col < 1 || col > obj.cols || (row + height - 1) > obj.rows || (col + width - 1) > obj.cols)
error("Selected plotting area has index errors.");
end
% Check if region already contains another plot
for r = row:(row + height - 1)
for c = col:(col + width - 1)
if(obj.gridMatrix(r,c) > 0)
error("Selected plotting area already contains another plot.");
end
end
end
% Create the plot at the specified location and mark the area as unavilable
ax = axes;
ax.Units = 'pixels';
for r = row:(row + height - 1)
for c = col:(col + width - 1)
obj.gridMatrix(r,c) = length(obj.plots) + 1;
end
end
% Store handle information
newPlotIndex = length(obj.plots) + 1;
obj.plots(newPlotIndex).handle = ax;
obj.plots(newPlotIndex).start = [row, col];
obj.plots(newPlotIndex).size = [height, width];
% Update the plot
obj.redraw();
end
%
% Retrieve a particular axis
%
function handle = get_axis(obj, index)
if(~isnumeric(index) || index < 1 || index > length(obj.plots))
error("Invalid argument.");
end
handle = obj.plots(index).handle;
end
%
% Clear all axes
%
function clear_all(obj)
for index = length(obj.plots):-1:1
obj.remove_axis(index);
end
end
%
% Remove an axis or axes
%
function clear(obj, varargin)
switch length(varargin)
case 0
if(~isempty(obj.plots))
obj.remove_axis(length(obj.plots));
end
case 1
index = varargin{1};
if(~isnumeric(index) || index < 1 || index > length(obj.plots))
error("When providing single argument, it must be a valid index");
end
obj.remove_axis(index);
case 2
if(varargin{1} < 1 || varargin{1} > obj.rows || varargin{2} < 1 || varargin{2} > obj.cols)
error("Index exceeds figure dimensions");
end
if(obj.gridMatrix(varargin{1},varargin{2}) > 0)
obj.remove_axis(obj.gridMatrix(varargin{1},varargin{2}));
end
case 4
if(varargin{1} < 1 || varargin{1} > obj.rows || varargin{2} < 1 || varargin{2} > obj.cols || varargin{3} < 1 || varargin{4} < 1)
error("Index exceeds figure dimensions");
end
if((varargin{1} + varargin{3} - 1) > obj.rows)
varargin{3} = obj.rows - varargin{1} + 1;
end
if((varargin{2} + varargin{4} - 1) > obj.cols)
varargin{4} = obj.cols - varargin{2} + 1;
end
indicesToRemove = [];
for r = varargin{1}:(varargin{1} + varargin{3} - 1)
for c = varargin{2}:(varargin{2} + varargin{4} - 1)
indicesToRemove(end + 1) = obj.gridMatrix(r,c);
end
end
indicesToRemove = sort(unique(indicesToRemove), 'descend');
for i = 1:length(indicesToRemove)
obj.remove_axis(indicesToRemove(i));
end
otherwise
error("Invalid number of arguments.");
end
obj.redraw();
end
end
methods(Access = private)
%
% Deleting an axis and all associated properties
%
function remove_axis(obj, index)
if(index == 0)
return;
end
delete(obj.plots(index).handle);
for r = obj.plots(index).start(1):(obj.plots(index).start(1) + obj.plots(index).size(1) - 1)
for c = obj.plots(index).start(2):(obj.plots(index).start(2) + obj.plots(index).size(2) - 1)
obj.gridMatrix(r,c) = 0;
end
end
for r = 1:obj.rows
for c = 1:obj.cols
if(obj.gridMatrix(r,c) >= index)
obj.gridMatrix(r,c) = obj.gridMatrix(r,c) - 1;
end
end
end
obj.plots(index) = [];
end
%
% Closing figure window
%
function on_window_close(obj, varargin)
delete(obj);
end
%
% Window size change callback function to redraw the positions of all axes
%
function obj = redraw(obj, varargin)
% Calculate the new block sizes
obj.blockWidth = floor((obj.fig.Position(3) - (obj.cols + 1) * obj.padding) / (obj.cols));
obj.blockHeight = floor((obj.fig.Position(4) - (obj.rows + 1) * obj.padding) / (obj.rows));
% Loop through all the plots and draw them
for i = 1:length(obj.plots)
obj.plots(i).handle.Position = [(obj.plots(i).start(2) - 1) * (obj.blockWidth + obj.padding) + obj.padding + obj.plots(i).handle.TightInset(1), ...
(obj.rows - obj.plots(i).size(1) - obj.plots(i).start(1) + 1) * (obj.blockHeight + obj.padding) + obj.padding + obj.plots(i).handle.TightInset(2), ...
obj.plots(i).size(2) * obj.blockWidth + (obj.plots(i).size(2) - 1) * obj.padding - (obj.plots(i).handle.TightInset(1) + obj.plots(i).handle.TightInset(3)), ...
obj.plots(i).size(1) * obj.blockHeight + (obj.plots(i).size(1) - 1) * obj.padding - (obj.plots(i).handle.TightInset(2) + obj.plots(i).handle.TightInset(4)) ...
];
grid(obj.plots(i).handle, 'on');
box(obj.plots(i).handle, 'on');
end
end
end
end