How change space between subplot and include colobar out axis?

Hello. Does someone know how I can increase the space between subplots? I have a 1x7 figure (seven subplots) and some of the labels of the y-axis are overlapping. Another problem, its because four of them are color pictures with color bar and the subplots with colorbar keep a horizontal axis extension different of that wihout colorbar. Any help will be greatly appreciated!
figure
subplot(7,1,1)
plot(rand(10),rand(10),'-');
subplot(7,1,2)
plot(rand(10),rand(10),'-');
subplot(,1,3)
plot(rand(10),rand(10),'-');
colobar
subplot(7,1,4)
plot(rand(10),rand(10),'-');
colobar
subplot(7,1,5)
plot(rand(10),rand(10),'-');
colobar
subplot(7,1,6)
plot(rand(10),rand(10),'-');
subplot(7,1,7)
plot(rand(10),rand(10),'-');

Answers (1)

I would recommend you take a look at this MATLAB Answers discussion for some ideas about adding vertical spacing between your subplots.
As for the problem with your subplots not being horizontally aligned because some of the axes contain colorbars, check out the following solution. I have created a local function called position_plot that accepts the axes handle for a subplot and then sets the width of the axes to a specific number. For this example, I set the widths of all the plots to be 0.4 normalized units, but you can adjust this however you please.
I hope this helps!
EDIT: Instead of calling the position_plot function after every subplot I simplified it with a for-loop.
figure
s1 = subplot(7,1,1);
plot(rand(10),rand(10),'-');
s2 = subplot(7,1,2);
plot(rand(10),rand(10),'-');
s3 = subplot(7,1,3);
plot(rand(10),rand(10),'-');
colorbar
s4 = subplot(7,1,4);
plot(rand(10),rand(10),'-');
colorbar
s5 = subplot(7,1,5);
plot(rand(10),rand(10),'-');
colorbar
s6 = subplot(7,1,6);
plot(rand(10),rand(10),'-');
s7 = subplot(7,1,7);
plot(rand(10),rand(10),'-');
splots = [s1 s2 s3 s4 s5 s6 s7];
for n = 1:length(splots)
position_plot(splots(n))
end
function position_plot(ax)
set(ax,'Units','normalized');
set(ax,'Position',[ax.Position(1), ax.Position(2), 0.4, ax.Position(4)]); % You can change 0.4 (the width) to meet your needs
end

8 Comments

Hi. It was showing an erro when I tried to use your suggestion to size of the axis with colobar:
function position_plot(ax)
Error: Function definition not supported in this context. Create functions in code file.
Hi. It worked creating a separated position_plot.m file with the function. Thank you very much. I just neet to find now a way to separate a little the vertical distance between them.
I'm glad you got the function to work.
Did you try any of the solutions at this link to add vertical spacing between your subplots? There a lot of different options, depending on your version of MATLAB.
For example, you can try adding a two-line title to each subplot:
title({'';'title of my subplot'})
________________________________________________________________________________
Alternatively, you can add a second line of vertical spacing within your x-axis labels:
xlabel("your xlabel"+newline+" ")
________________________________________________________________________________
Another option is to use the subsubplot function from the Climate Data Toolbox (which you can download on the File Exchange at this link). It has a really great syntax where you can specify the vertical and horizontal padding of your subplots.
subsubplot(7,1,1,'vpad',0.05)
Doing this for each subsubplot would add 0.05 normalized units of vertical padding between the subplots, meaning that it separates the axes by 5% of the figure height.
If you have an array of subsubplots with multiple columns and rows, you can also add padding horizontally by setting the 'hpad',hspace name-value pair.
________________________________________________________________________________
Or, if you just want the y-axis labels to not overlap, you can try setting the font size to something smaller for each subplot:
set(gca,'FontSize',8)
I tried but I did not understand the function to add space between the subplots. Also, I do not know why but some of the subplots are not showing the North and East window lines (see attached figures). I need to increase the vertical axis extension and include more space between the subplots. Here is an example of how I am plotting that data:
s4 = subplot(7,1,4);
total_elements_baz_lines_ev1=numel(back_azm_ev1_error);
colororder(parula(total_elements_baz_lines_ev1));
scatter(time_vector_ev1,baz_lines_ev1,50,back_azm_ev1_error, 'LineWidth',1.5)
Hello!
I am back. I was using normal the five rectangle suplots in a single column as we tried before
s1 = subplot(5,1,1);
.
.
.
s2 = subplot(5,1,2);
.
.
.
s3 = subplot(5,1,3);
.
.
.
s4 = subplot(5,1,4);
.
.
.
s5 = subplot(5,1,5);
.
.
.
pos = get(gcf, 'Position');
set(gcf, 'Position',pos+[0 -1000 0 1000] )
splots = [s1 s2 s3 s4 s5];
for n = 1:length(splots)
position_plot(splots(n))
end
However, my supervisor decide to remove one of them and plot two columns with two rectangles in each column. I tried using the
s1 = subplot(2,2,1);
s2 = subplot(2,2,3);
s3 = subplot(2,2,2);
s4 = subplot(2,2,4);
pos = get(gcf, 'Position');
set(gcf, 'Position',pos+[0 -1000 0 1000] )
splots = [s1 s2 s3 s4];
for n = 1:length(splots)
position_plot(splots(n))
end
but the results were very bad (see figure attached). Does anyone have any information to help me to organize the four rectangles? I will be very grateful.
Instead of subplot try using the tiledlayout method. Here is an example:
xData = linspace(0,10,10);
yData = rand(10,4);
t = tiledlayout(2,2);
for n = 1:4
nexttile
plot(xData,yData(:,n));
end
Try using some variation of tiledlayout with your data to see if that helps.
I was trying before, but I did not get to understand how does work titledlayou. Need the four plots to be included inside that for/end? By some examples checked on Google, the four plots are aways square.
tiledlayout is an improved version of subplot that was released in 2019.
With tiledlayout you don't have to specify the subplot number with each graph. You simply specify the size you want at the beginning and then add plots one at a time with the nexttile function. For example, if you want a figure with 4 subplots divided into 2 rows and two columns:
tiledlayout(2,2)
Then, when you want to make your first subplot, use the nexttile function just before the actual plotting function. For example:
nexttile
plot(1:3,[4 7 5])
To add the next subplot, use the nexttile function again:
nexttile
plot(1:3,[2 6 9])
You can even add subplots that take up more than one position in the subplot space. For example, if you want your next subplot to occupy the entire row:
nexttile([1 2]) % this means: one row, two columns
plot(1:6,[3 7 6 4 6 9])
So, if you want to make 4 subplots that are rectangle-shaped, you can set the tiledlayout grid to a wider area and then use the nexttile commands to specify the width of the plots. Here is an example with some more realistic data:
% Make some data
x = 0:pi/12:10*pi;
noise = rand(length(x),4);
Y = [sin(x)' sin(x)' sin(x)' sin(x)'] + noise;
figure('units','centimeters','position',[0 0 20 10]) % Makes the figure 20cm by 10cm
tiledlayout(2,8)
for n = 1:4
nexttile([1 4])
plot(x,Y(:,n))
% The subplots will even auto-adjust if they have colorbars
% For exaple, if your third subplot needs a colorbar:
if n == 3
colorbar
end
end

Sign in to comment.

Categories

Products

Release

R2020b

Asked:

on 10 Feb 2024

Commented:

on 6 Nov 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!