How can I set subplot size in MATLAB figure? - matlab

I often need to plot 10 images together, but using this code results in small images :
img = rand(400,600);
for i=1:10
subplot(2,5,i);
imshow(img);
title(['Image ' int2str(i)]);
end
As you can see, the images do not use all available space in the screen. How can I increase the size, or decrease the padding/margin between them?
Thanks for any help.

I don't believe there is an easy way to do it. There are two options:
First, use the position part of the subplot:
>> subplot(2,5, i, [l, b, w, h])
and calculate the left, bottom, width, height.
Or, get the handle of the returned axis:
>> h(i) = subplot(2,5,i);
and then modify the axis afterward.
>> set(h(1), 'position', [l, b, w, h] );
There are a number of pages that will give more detail, e.g., http://www.briandalessandro.com/blog/how-to-make-a-borderless-subplot-of-images-in-matlab/
[update]
The code below gives a little more detail on who you can do something like what you are looking for. It is a tad tedious. The 0.95 and 0.02 are just to give a little padding. They are nothing magical. :-)
One other thing to note is I would really encourage you to use "ii" as your index variable (or something else) as "i" is defined as sqrt(-1). It is a good convention not to use "i" and "j" as index variables (especially in Matlab).
img = rand(400,600);
figure(1);
clf();
hold on;
% Get the width and height of the figure
lbwh = get(1, 'position');
figw = lbwh(3);
figh = lbwh(4);
% Number of rows and columns of axes
ncols = 5;
nrows = 2;
% w and h of each axis in normalized units
axisw = (1 / ncols) * 0.95
axish = (1 / nrows) * 0.95
for ii=1:10
% calculate the row and column of the subplot
row = floor( ii/(ncols+1) ) + 1
col = mod( ii-1, ncols ) + 1
% calculate the left, bottom coordinate of this subplot
axisl = (axisw+0.02) * (col-1)
axisb = (axish+0.02) * (row-1)
% plot the subplot
h= subplot('position', [axisl, axisb, axisw, axish] );
imshow(img);
title(['Image ' int2str(ii)]);
pause
end
You will have to play with it to make it do exactly what you want. And "help" is your friend.

I have this requirement often and the most efficient way for me to achieve it is using the third party subplot_tight function, which is a more-or-less slot-in replacement for subplot. At its simplest you can do
figure(1); clf
subplot_tight(1,2,1, [0.05 0.05])
%normal plot stuff
where the two parameters in the fourth argument control the fraction of visible space around the image.

Based on the answer of #brechmos, when your subplot number is more than 10 subplot, then his code will trigger a error.
% calculate the row and column of the subplot
row = floor( ii/(ncols+1) ) + 1
col = mod( ii-1, ncols ) + 1
e.g. 4X5 cells, then subplot 11 will be wrongly interpreted as (2, 1), but not (3,1).
Replace it with the code below can fix it:
% calculate current row and column of the subplot
row = floor( (i-0.5)/ncols ) + 1;
col = mod(i-(row-1)*ncols, ncols+1);

You can use figure properties option once you generate the plot. Click on the subplot which you want to resize. From property editor select 'more properties' option. There if you scroll you will see 'Position' tab. You can change those values to see how the subplot moves and thus adjust subplot according to your preference.

Related

Separating axes from plot area in MATLAB

I find that data points that lie on or near the axes are difficult to see. The obvious fix, of course, is to simply change the plot area using axis([xmin xmax ymin ymax]), but this is not preferable in all cases; for example, if the x axis is time, then moving the minimum x value to -1 to show activity at 0 does not make sense.
Instead, I was hoping to simply move the x and y axes away from the plot area, like I have done here:
left: MATLAB generated, right: desired (image editing software)
Is there a way to automatically do this in MATLAB? I thought there might be a way to do it by using the outerposition axes property (i.e., set it to [0 0 0.9 0.9] and drawing new axes where they originally were?), but I didn't get anywhere with that strategy.
The answers here already show you most of the way - here is the last step to separate the x and y axle as per the example you put together.
f = figure ( 'color', 'white' );
% create the axes and set some properties
ax = axes ( 'parent', f, 'box', 'off', 'nextplot', 'add', 'XMinorTick', 'on', 'YMinorTick', 'on' );
% plot some data
plot ( ax, 0:10, [0:10].^2, 'rx-' )
% modify the x and y limits to below the data (by a small amount)
ax.XLim(1) = ax.XLim(1)-(ax.XTick(2)-ax.XTick(1))/4;
ax.YLim(1) = ax.YLim(1)-(ax.YTick(2)-ax.YTick(1))/4;
% Set the tick direction
ax.TickDir = 'out';
% draw the plot to generate the undocumented vertex data var
drawnow()
%% R2015a
% X, Y and Z row of the start and end of the individual axle.
ax.XRuler.Axle.VertexData(1,1) = 0;
ax.YRuler.Axle.VertexData(2,1) = 0;
%% R2015b
% extract the x axis vertext data
% X, Y and Z row of the start and end of the individual axle.
vd = get(ax.XAxis.Axle,'VertexData');
% reset the zero value
vd(1,1) = 0;
% Update the vertex data
set(ax.XAxis.Axle,'VertexData',vd);
% repeat for Y (set 2nd row)
vd = get(ax.YAxis.Axle,'VertexData');
vd(2,1) = 0;
set(ax.YAxis.Axle,'VertexData',vd);
Edit: The vertex is something that Matlab recreates whenever the axes/figure changes size or if you zoom or pan for example.
You can try to counteract this (remember you are using undocumented features here) by adding a listener to attempt to capture this. We can use the MarkedClean event which is called quite a lot of times.
addlistener ( ax, 'MarkedClean', #(obj,event)resetVertex(ax) );
Where you resetVertex function is something like: (R2015b shown only)
Edit 2 added the code to turn off the minor ticks below 0.
function resetVertex ( ax )
% extract the x axis vertext data
% X, Y and Z row of the start and end of the individual axle.
ax.XAxis.Axle.VertexData(1,1) = 0;
% repeat for Y (set 2nd row)
ax.YAxis.Axle.VertexData(2,1) = 0;
% You can modify the minor Tick values by modifying the vertex data
% for them, e.g. remove any minor ticks below 0
ax.XAxis.MinorTickChild.VertexData(:,ax.XAxis.MinorTickChild.VertexData(1,:)<0) = [];
ax.YAxis.MinorTickChild.VertexData(:,ax.YAxis.MinorTickChild.VertexData(2,:)<0) = [];
end
Note: this uses undocumented features -> so may only work in certain versions of Matlab (I have added the code for r2015a & r2015b) and Matlab may recreate the vertex data depending on what you do with the plots..
Here is a simple way for achieving that:
% some data:
x = 1:100;
f=#(x) 5.*x;
y=f(x)+rand(1,length(x))*50;
close all
% plotting:
f1 = figure('Color','white');
ax = axes;
plot(ax,x,y,'o');
% 'clean' the data area a little bit:
box off
ax.TickDir = 'out';
% pushing the axis a bit forward:
lims = axis;
pos = ax.Position;
axis([lims(1)-ax.XTick(2)/5 lims(2)+0.1 lims(3)-ax.YTick(2)/5 lims(4)+0.1])
% Create lines
firstXtick = 0.013; %this value need to be adjusted only once per figure
firstYtick = 0.023; %this value need to be adjusted only once per figure
lx = annotation(f1,'line',[pos(1) pos(1)+firstXtick],...
[pos(2) pos(2)],'Color',[1 1 1],'LineWidth',1);
ly = annotation(f1,'line',[pos(1) pos(1)],...
[pos(2) pos(2)+firstYtick],'Color',[1 1 1],'LineWidth',1);
Which yields this figure:
The only thing to adjust here, once per type of figure, is firstXtick and firstYtick values, that have to be fine tuned to the specific axis. After setting them to the correct value the figure can be resized with no problem. Zoom and pan require a little fixes.
You can start your axes from less than zero and then remove the less than zero ticks from your plot. e.g.
plot(0:3:30,0:3:30); %Some random data for plotting
h = gca;
axis([-1 30 -1 30]); %Setting the axis from less than zero
box off; %Removing box
h.TickDir = 'out'; %Setting Direction of ticks to outwards
h.XTickLabel(1)= {' '}; %Removing the first tick of X-axis
h.YTickLabel(1)= {' '}; %Removing the first tick of Y-axis
With this code, you'll get this result:
This may have a drawback, sometimes, that zero ticks may also get removed (as you can see in above figure). This is because the plot had set the first ticks of axes equal to zero. This can be avoided using if condition. So, the code can be modified as below:
plot(0:3:30,0:3:30);
h = gca;
axis([-1 30 -1 30]);
box off;
h.TickDir = 'out';
if str2num(cell2mat(h.XTickLabel(1))) <0
h.XTickLabel(1)= {' '};
end
if str2num(cell2mat(h.YTickLabel(1))) <0
h.YTickLabel(1)= {' '};
end
The above code will yield the following result:-
Also note that, for your case, since your axes ticks are very less, -1 may not be much suitable for the starting value of axes and you may need to use -0.1 instead i.e. axis([-0.1 30 -0.1 30]);
With a slight modification of #matlabgui's answer you can track the (major) tick limits:
ax = gca();
% Set the tick direction
ax.TickDir = 'out';
% Make sure this stays when saving, zooming, etc
addlistener ( ax, 'MarkedClean', #(obj,event) change_ticks(ax) );
% Draw the plot to generate the undocumented vertex data variable
% and call callback for the first time
drawnow();
The callback
function change_ticks( ax )
% Modify ruler
ax.XRuler.Axle.VertexData(1,1) = ax.XTick(1);
ax.XRuler.Axle.VertexData(1,2) = ax.XTick(end);
ax.YRuler.Axle.VertexData(2,1) = ax.YTick(1);
ax.YRuler.Axle.VertexData(2,2) = ax.YTick(end);
end
I haven't test extensively but seems to work for custom ticks too. This nicely cuts the rulers not only on zero but beyond the fist and last tick. This was tested in Matlab 2019a on Windows and ax.XRuler.Axle.VertexData works just fine. Note this is only for major ticks!

In MATLAB: How can XData and YData be updated with a changing number of lines?

I am looking for a way to add vertical dividing lines separating consecutive days in a dynamically updating plot of animal migration data (location vs. time). Part of the challenge is that the number of these dividers changes as the plot domain expands to display more temporal data: As the number of days in the plot increases from 3 to 5, for example, the number of dividers increases by 2.
A minimal code example, written in MATLAB, is shown below:
xcols = [1; 1];
ycols = [0; 1];
figure(4)
clf
h.divs = plot(xcols,ycols,':k');
xlabel('time')
ylabel('location')
for ii=2:6
xcols(:,end+1) = [ii; ii];
ycols(:,end+1) = [0; 1];
% set(h.divs, 'XData', xcols, 'YData', ycols);
% set(h.divs, {'XData'}, num2cell(xcols',2), {'YData'}, num2cell(ycols',2));
drawnow
pause(1)
end
The problem centers on the two lines that have been commented out. If I comment in the first of these to try to update XData and YData with each new set of dividers (given as the 2 x DividerCount matrices xcols and ycols), then I receive an error that these inputs "must be a vector of numeric type". If I instead comment in the second line as way of using cell arrays to get around this (per this Stack Overflow post and this MATLAB Newsgroup post), then the code returns an error that "cell array handle dimension must match handle vector length" as soon as the number of dividers changes.
Hacky solutions are certainly possible. For example, the dividers can be plotted as a single line of horizontal and vertical segments, where the horizontal segments are placed above and below the y-axis limits of the plot. Or a fixed number of dividers can be used, with some of the dividers plotted outside the x-axis limits of the plot. The question is whether there is a non-hacky approach – one that can plot a potentially changing number of lines of identical style in the same figure with each pass of the loop.
Assume we have at the beginning
xcols = [1:3; 1:3];
ycols = [0 0 0; 1 1 1];
then h.divs = plot(xcols,ycols,':k'); will create 3 line objects.
Using
set(h.divs,{'XData'},num2cell(xcols',2),{'YData'},num2cell(ycols',2));
with size(xcols, 2)>3 will fail, because this assumes there are more than 3 graphics objects to set values for (but numel(h.divs) is still 3).
So do we have to create a new plot for every divider?
No!, because if we insert NaNs into our data, we can introduce gaps into lines.
As a first iteration, we could use this:
xcols = [1 1];
ycols = [0 1];
figure(4)
clf
h.divs = plot(xcols,ycols,':k');
for ii=2:6
xcols(end+(1:3)) = [nan ii ii];
ycols(end+(1:3)) = [nan 0 1];
set(h.divs, 'XData', xcols, 'YData', ycols);
drawnow
pause(1)
end
Starting with one divider which we plot, we then grow the data vectors (one-dimensional!) with a new value pair separated from the old data by a nan.
This of course grows the data vectors in the loop, which is not so great. Instead, if we know how many dividers there will be, we can preallocate with NaNs and only fill in new data in the loop:
n_div = 6;
xcols = nan(3 * n_div, 1);
ycols = nan(3 * n_div, 1);
figure(4)
clf
h.divs = plot(xcols,ycols,':k');
for ii=1:6
xcols((ii - 1)*3 + (1:2)) = [ii ii];
ycols((ii - 1)*3 + (1:2)) = [0 1];
set(h.divs, 'XData', xcols, 'YData', ycols);
drawnow
pause(1)
end
This has also the benefit that we can assign XData and YData separately, as in the new handle syntax:
h.divs.XData = xcols;
h.divs.YData = cols;
(or even better: h.divs.XData((ii - 1)*3 + (1:2)) = [ii ii];), because the lengths don't change.
You can start with divider definition:
divider.x=[];
divider.y=[];
divider.counter=0;
h.div=line('xdata',divider.x,'ydata',divider.y,'linestyle',':','color','k');
Which will draw no line, but handle will be set.
Then, perhaps in some loop, you can call:
divider.counter=divider.counter+1; % increase the counter
divider.x=[divider.x,nan,divider.counter*[1 1]]; % append X coords of new divider line
divider.y=[divider.y,nan,divider.counter*[0 1]]; % append Y coords of new divider line
set(h.div,'xdata',divider.x,'ydata',divider.y) % update dividers
drawnow % update figure immediately
This approach works because NaN value can be passed to line function but will not be plotted and neither will be lines from neighbour points.

How can thin horizontal lines be added between each row in a grayscale image?

I need to create an nth-order Hadamard matrix, row double it, within each row randomly permute the elements of the matrix, and then display it. So far, I have accomplished all of these things. What I end up with when I imshow(matrix) is a nice picture of black and white boxes. But I haven't figured out how to insert a fine line to divide each row. I can create something like the first image on the left, but not the image on the right (these are Figures 1 and 2 from this paper)
Any help or comments would be thoroughly appreciated.
I've found using vector approaches (e.g., patch and rectangle) for this sort of problem unnecessarily challenging. I think that it's more straightforward to build a new image. This avoids floating-point rounding issues and other things that crop up with vector graphics. My solution below relies on some functions in the Image Processing Toolbox, but is simple and fast:
% Create data similarly to #TryHard
H = hadamard(48);
C = (1+[H;-H])/2;
rng(0); % Set seed
C(:) = C(randperm(numel(C))); % For demo, just permute all values, not rows
% Scale image and lines
scl = 10; % Amount to vertically scale each row
pad = 2; % Number of pixels to add between each row
C = imresize(C,scl,'nearest');
C = blockproc(C,[scl size(C,2)],#(x)[x.data;zeros(pad,size(C,2))]);
C = C(1:end-pad,:); % Remove last line added
% Dispay image
imshow(C)
This results in an image like this
The scl and pad parameters can be easily adjusted to obtain different sizes and relative sizes. You can call imresize(...,'nearest') again after adding the lines to further scale the image if desired. The blocproc line could potentially be made more efficient with various options (see the help). It could also be replaced by calls to im2col and col2im, which possibly could be faster, if messier.
I did not try the code, but I think that something like that should work:
sizeOfACube = 6;
numberOfRows = 47;
RGB = imread('image.png');
RGB = imresize(A, [(numRows+numberOfRows) numCols]);
for i=1:1:NumberOfRows
RGB(i*6,:,:) = 0;
end
imagesc(RGB);
imwrite(RGB,'newImage.png');
with:
sizeOfAcube the size of one cube on the QRcode.
numRows and numCols the number of Rows and Column of the original image.
One solution is to use patches, for instance as follows:
% set up example array
xl = 24; yl = xl;
[X Y] = find(hadamard(xl)==1);
% generate figure
figure, hold on
for ii=1:length(X)
patch(X(ii) + [0 0 1 1],Y(ii) + [0.1 0.9 0.9 0.1],[1 1 1],'Edgecolor',[1 1 1])
end
axis([0 xl+1 0 yl+1])
axis('square')
The patch command patch(x,y, color) accepts the vertices of the polygon element as x and y. In this example you can modify the term [0.1 0.9 0.9 0.1] to set the thickness of the bounding black line.
This generates
Edited
For the particular instance provided by the OP:
H=Hadamard(48); %# now to row-double the matrix
A=(1+H)/2;
B=(1-H)/2;
C=[A; B]; %# the code below randomly permutes elements within the rows of the matrix
[nRows,nCols] = size(C);
[junk,idx] = sort(rand(nRows,nCols),2); %# convert column indices into linear indices
idx = (idx-1)*nRows + ndgrid(1:nRows,1:nCols); %# rearrange whatever matrix
E = C;
E(:) = E(idx);
[X Y] = find(logical(E));
xl = length(X);
yl = length(Y);
figure, hold on
for ii=1:xl
rectangle('Position',[X(ii) Y(ii)+.2 1 0.8],'facecolor',[1 1 1],'edgecolor',[1 1 1])
end
axis([0 max(X)+1 0 max(Y)+1])
axis('square')
set(gca,'color',[0 0 0])
set(gca,'XTickLabel',[],'YTickLabel',[],'XTick',[],'YTick',[])
This example uses rectangle instead of patch to generate sharp corners.
The image:

Matlab: Overlapping subplot titles

I'm using subplot which contains three different plots. Each plot has its own labels and title.
The problem is that I have to maximize the plot when I save it. Otherwise, the texts will overlap each other.
When I maximize it, the subplot's label text will appear little blurry in the image, even if I use ESP format or any vector format.
How can I resolve this issue?
For the title overlap issues, you can produce multiple lines of title text use a cell array of strings as the input parameter of title():
title_text = {'first line', 'second line', 'third line'};
title(title_text);
And it works for label text too.
In addition to Da Kuang's answer, if you would like to keep your titles and labels on the same line, you could change the font size
a = axes;
t = title('My Really Long Title');
l = xlabel('My Really Long x label')
set(t, 'FontSize', 8)
set(l, 'FontSize', 8)
I'm not sure why your labels are blurry, but I can help with the overlap.
I never use subplot when I want to save images (eg. for a paper). What I do instead is create each axes individually, which allows a lot more control over each of them.
Below is a rather general example, which illustrates how to generate an arbitrary grid of axes with much finer control over their placement than subplot allows. Of course, with only 3 axes, you don't really need the loop, but I'm sure you can adapt this to fit your needs.
% first create the figure
figPos = [200 200 800 500];
figure('Color', 'w', 'Position', figPos)
% next, determine how much padding you want on each side of the axes, and in
% between axes. I usually play around with these, and the figure size until
% the layout looks correct.
leftPadding = 50/figPos(3); % the space at the left of the figure
rightPadding = 25/figPos(3); % the space at the right of the figure
horizPadding = 80/figPos(3); % the space between axes (horizontally)
topPadding = 30/figPos(4); % the space at the top of the figure
bottomPadding = 50/figPos(4); % the space at the bottom of the figure
vertPadding = 120/figPos(4); % the space between axes (vertically)
% set up the grid size
nHorizAxes = 2;
nVertAxes = 3;
% figure out how big each axes should be
horizPlotSpace = 1-leftPadding-rightPadding-(nHorizAxes-1)*horizPadding;
vertPlotSpace = 1-topPadding-bottomPadding-(nVertAxes-1)*vertPadding;
width = horizPlotSpace/nHorizAxes;
height = vertPlotSpace/nVertAxes;
myAxes = zeros(nVertAxes, nHorizAxes);
% create some sample data to plot for illustrative purposes
x = linspace(0, 2*pi);
y = sin(x);
for iRow = 1:nVertAxes
for iCol = 1:nHorizAxes
% calculate the position
left = leftPadding+(iCol-1)*(width+horizPadding);
bottom = bottomPadding+(iRow-1)*(height+vertPadding);
position = [left bottom width height];
myAxes(iRow, iCol) = axes('Position', position);
plot(x, y)
xlabel('Test Label')
ylabel('Test Label')
title(sprintf('axes(%d, %d)', iRow, iCol))
end
end
Those answers should help but here are some other things to try, depending on the cause of the overlapping text:
Change the figure's size so there's room for the text. For example:
set(gcf, 'PaperSize', [5 7])
Change the size of the subplots.
s = get(gca, 'Position');
set(gca, 'Position', [s(1), s(2), s(3), s(4) * 0.5])
MATLAB (R2021b) appears to stop updating the size of subplots after the axes function is used to set the current axes. The following code causes the title to be cut off.
sp1 = subplot(2, 1, 1);
sp2 = subplot(2, 1, 2);
axes(sp1) % Set the current axes to the first subplot.
title(sprintf('Hello\nCruel\nWorld'))
On the other hand, if title is called immediately after the first subplot is opened, without using axes, then the title has sufficient space to be completely visible.
sp1 = subplot(2, 1, 1);
title(sprintf('Hello\nCruel\nWorld'))
sp2 = subplot(2, 1, 2);
As a workaround, if you need to set the values for a prior subplot, you can simply pass sp1 as the first argument of the desired function.
sp1 = subplot(2, 1, 1);
sp2 = subplot(2, 1, 2);
title(sp1, sprintf('Hello\nCruel\nWorld'))
Simple you can use below function :
plt.tight_layout()
description: The tight_layout() function in pyplot module of matplotlib library is used to automatically adjust subplot parameters to give specified padding.
note : always use this function before plt.show() function.

How do I label two vectors in Matlab?

I have a 2 column matrix(called M, which I visualize as two vectors using Matlab's plot command(plot(M)). I have two issues:
I want to label the vectors themselves on the plot.
I want to label each row of the matrix(i.e. each vector component) on the plot.
How would I go about doing those things?
An example:
M = cumsum(rand(10,2) - 0.5);
x = 1:size(M,1);
plot(x, M(:,1), 'b.-', x, M(:,2), 'g.-')
legend('M1', 'M2')
for i=x
text(i+0.1, M(i,1), sprintf('%.2f', M(i,1)), 'FontSize',7, 'Color','b');
text(i+0.1, M(i,2), sprintf('%.2f', M(i,2)), 'FontSize',7, 'Color','g');
end
Alternatively, you can use:
datacursormode()
which will enable the user to just point and click on points to see the data labels.
You may need to tweak this to get the positions of the labels exactly how you want them, but something like this will do the trick.
M = [1 2; 3 4; 5 6]
plot(M)
nrows = size(M, 1);
ncols = size(M, 2);
x = repmat(nrows - .3, 1, ncols);
y = M(end, :) - .3;
labels = cellstr([repmat('Col', ncols, 1), num2str((1:ncols)')]);
text(x, y, labels)
You can label each axis with the function:
xlabel('label')
ylabel('label')
These can also take cell arguments, where each row is a new line. That's handy for showing units. Labeling each point on the figure can be done as so:
for i=1:length(M)
text(M(i,1),M(i,2),'Label Text')
end
The label text can also be a string variable that you can edit with sprintf and make special strings for each point.