Filter löschen
Filter löschen

Colorbar spacing issue using tiledlayout

28 Ansichten (letzte 30 Tage)
Sebastian
Sebastian am 21 Jun. 2021
Beantwortet: Dave B am 28 Jul. 2021
I'm using Matlab R2021a. I still have some problems using colorbar for multiple plots:
My main intention is to create a tiledlayout, plotting different plots (3 to 4) and to insert one shared colorbar to the right. To illustrate this, I used peaks in the following code:
fig= figure('color', 'w');
tile= tiledlayout(2,2,'TileSpacing','compact','Padding', 'compact');
for a=1:3
nexttile(a);
contourf(peaks);
set(gca, 'FontSize', 14);
title(['Plot No. ', num2str(a)],'FontName','Arial Narrow','FontSize',14);
end
xlabel(tile, 'X-Label [unit]');
ylabel(tile, 'Y-Label [unit]');
cb= colorbar('FontName','Arial Narrow','FontSize',14);
cb.Label.String = 'Label';
cb.Layout.Tile= 'east';
The first problem that arises is that Mablab truncates the associated label. It's presented correctly in the Matlab-figure but when I'm saving the figure as .png or .tiff using print-fuction the label is cut off. Has anyone got an idea how to solve this so that Matlab is considering the space for the label or to "freeze" the figure before saving?
Another problem I'm facing is that i would like to reduce the (constant) hight of the colorbar while keeping it centered. This is especialy useful when plotting more than 2 plots in height so that the colorbar doesn't become overwhelming. My aim would look as follows:
Thank you in advance!
  1 Kommentar
Sergey Kasyanov
Sergey Kasyanov am 21 Jun. 2021
Bearbeitet: Sergey Kasyanov am 21 Jun. 2021
Hello!
It is not difficult to set sizes of any elements on figure by 'Position' and another properties. There are a lot of ways to get handles of graphic objects for operating with them. For example you can get all graphic objects on figure by:
objects = get(gcf, 'Children');
Then you can change their position on figure by:
set(objects(2), 'Position', [0.1, 0.1, 0.5, 0.5]);
I can't use your code for trying on your example (it does not work for me), but it is very simple. Also try property 'Outer Position'.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Dave B
Dave B am 28 Jul. 2021
@Sebastian - For the first bit, that looks frustrating! When I tried the same code (also in R2021a) the colorbar's label was not cut off. I used print as follows:
print(gcf,'-dpng','foo_print.png')
An alternative to try is to use the exportgraphics function. It crops things a little differently and while I normally think it's advantage is that it crops things more tightly, maybe it will do a better job in this case?
exportgraphics(gcf,'foo_exportgraphics.png')
*note you can also pass in the tiled layout (tile in your code) as the first argument to exportgraphics.
A final alternative is to try a different option for Padding, although I don't think you should need to do this. Please do let me know if exportgraphics doesn't crop correctly for you.
For the second part I sadly don't have great suggestions for fine-grained control of the position of the colorbar, but there's a trick for getting a colorbar that's centered and about 1/3 the size of the layout. The strategy is basically to surround the colorbar with a couple of 'placeholder' layouts. That's a little smaller than one you were after but maybe an improvement? I'm hoping this is an area we can provide more options.
placeholder1=tiledlayout('flow','Parent',tile);
placeholder1.Layout.Tile='east';
cb= colorbar('FontName','Arial Narrow','FontSize',14); % Your existing line of code
placeholder2=tiledlayout('flow','Parent',tile);
placeholder2.Layout.Tile='east';

Kategorien

Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by