필터 지우기
필터 지우기

How change space between subplot and include colobar out axis?

조회 수: 16 (최근 30일)
Guilherme Weber Sampaio de Melo
Guilherme Weber Sampaio de Melo 2024년 2월 10일
댓글: Ahmed 2024년 2월 11일
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),'-');

답변 (1개)

Austin M. Weber
Austin M. Weber 2024년 2월 10일
편집: Austin M. Weber 2024년 2월 10일
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
  댓글 수: 4
Austin M. Weber
Austin M. Weber 2024년 2월 10일
편집: Austin M. Weber 2024년 2월 10일
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)
Guilherme Weber Sampaio de Melo
Guilherme Weber Sampaio de Melo 2024년 2월 10일
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)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by