How to make subplot size bigger?

27 views (last 30 days)
Yejin Hong
Yejin Hong on 12 May 2022
Commented: DGM on 13 May 2022
Hi, I am trying to put 21 surf subplots in one figure but the colors are not showing because they are so small. How can I make the subplot size bigger?
This is my code;
for i=1:21
xi = linspace(1,4,100);
yi = linspace(1,5,100);
zi = xlsread('curve_interpolated.xlsx',i);
% read each excel sheet
zlim([20 100])
colormap jet
caxis([0 100])
ax = gca;
ax.XTick = 1:1:4
ax.YTick = 1:1:5
ax.ZMinorTick = 'on'
subplot(5,5,i)
surf(xi,yi,zi)
end
colorbar
Below is how I want each subplot to be shown (colors visible), but
I'm getting this output as shown below. Also, I want one big colorbar next to all the subplots, but I am getting the colorbar next to the last subplot instead. Is there a way to fix this?
  2 Comments
dpb
dpb on 12 May 2022
Edited: dpb on 12 May 2022
Well, when you try to put 21 axes on one figure, there's not going to be a lot of room by definition. The new(ish) tiledlayout has more options to control spacing than does subplot, Trying it would be the first thing I'd suggest before trying to fiddle with subplot axis sizes through the 'Position' property. You could try increasing the width and height values (3rd, 4th values in position vector) by 10% or so and see how far you can go before you run out of space. Visually, of course, everything fits into the figure so maximizing the figure to the screen size will give you the most space/pixels upon which to draw; the relative sizes will all still be the same, of course.
You can gain some extra room by reducing the font size as small as can and still be read, or since the scales are all the same, simply set the 'Ticklabels' property to [] for all axes.
As for the colorbar, it is associated with an axes object so it will be attached to one subplot of your choice. Only way around that w/ subplot() would be to create another axes that overlays the whole figure with the same range of data and associate the colorbar with it. Of course, when you do that, you'll have to manually shrink the various subplots and relocate them manually in order to have somewhere to draw the colorbar...
There are some global properties associated with the tiled layout object; whether one of those would include the addition of a colorbar or not I've not investitaged, but I'd still suggest looking at it before you spend any amount of time on this path...
DGM
DGM on 13 May 2022
There's this example of adding a global legend to a tiledlayout(), but I don't know if the same convenience is available for colorbars.
The functionality of tiledlayout() has evolved since the version I have, so if there are such conveniences, chances are that I'm in no position to craft an example.

Sign in to comment.

Answers (1)

DGM
DGM on 12 May 2022
Disregarding subplot arrangement, the edges are obscuring the faces due to the mesh resolution.
figure
colormap(parula(256))
h = surf(peaks(1000));
You might use flat shading
figure
colormap(parula(256))
h = surf(peaks(1000));
shading flat
% could also set the EdgeColor property of the surf object to 'none'.
That will remove the black edges from the surface. That will prevent the edges from obscuring the colormapped height information, but the smoothness tends to make it more difficult to see subtle curvature.
Using a shorter colormap may help in that regard.
figure
colormap(parula(16)) % a shorter colormap
h = surf(peaks(1000));
shading flat
Alternatively, you can plot the data with a reduced mesh resolution so that there are fewer edges to deal with. For such tiny plots, there is no need for an extreme excess of resolution. Graphs are representations of data for visualization, not stores of data, so discarding excess data points should be acceptable if it does not impact the visual interpretation of the information.
figure
colormap(parula(256))
h = surf(peaks(50));
I know that's not a complete answer, but I think that it's likely going to be part of any complete answer. I'll let someone who is more familiar with tiledlayout() handle the rest.

Community Treasure Hunt

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

Start Hunting!