how to add one colorbar for the whole figure with tiled layout?
223 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jing Ci Neo
am 22 Dez. 2019
Kommentiert: Adam Danz
am 21 Jan. 2022
The matlab example shows that we can add colorbar to individual tiles see attached, but can we add one colorbar for the whole figure? I also have problems with adding a legend and positioning it.
Example code:
tiledlayout(2,1)
% Top plot
nexttile
surf(peaks)
colorbar
% Bottom plot
nexttile
mesh(peaks)
colorbar
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 22 Dez. 2019
Bearbeitet: Adam Danz
am 8 Apr. 2021
To use one colorbar that represents the color values for each subplot, each subplot must use the same colormap and the same colorscale. The colorbar can be assigned to just one of the subplots or it can be a global colorbar assigned to the tiledlayout object.
Here's a demo.
tlo = tiledlayout(2,1)
% Top plot
h(1) = nexttile(tlo); % Get axis handle
surf(h(1), peaks)
% Bottom plot
h(2) = nexttile(tlo); % Get 2nd axis handle
surf(h(2), peaks * 2.5)
% Set colormap and color limits for all subplots
set(h, 'Colormap', jet, 'CLim', [-20 20])
% assign color bar to one tile
cbh = colorbar(h(end));
% To position the colorbar as a global colorbar representing
% all tiles,
cbh.Layout.Tile = 'east';
If the subplots were produced using subplot(), you can reposition the colorbar so that it is not associated with just one subplot. This example places the subplot the right of the figure, centered vertically.
% Reposition to figure's left edge, centered vertically
cbh.Position(1) = .95-cbh.Position(3);
cbh.Position(2) = 0.5-cbh.Position(4)/2;
% decrease horizontal extent of subplots to 92% of their current width
set(h, {'Position'}, mat2cell(vertcat(h.Position) .* [1 1 .92, 1], ones(size(h(:))),4))
6 Kommentare
Adam Danz
am 21 Jan. 2022
@Maximilian Grobbelaar Line color is not controlled by colormap. Instead, assign color directly using this approach:
numberOfLines = 4;
colors = jet(numberOfLines);
nexttile()
plot(___, 'Color', colors(1,:))
nexttile()
plot(___, 'Color', colors(2,:))
nexttile()
plot(___, 'Color', colors(3,:))
nexttile()
plot(___, 'Color', colors(4,:))
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Orange finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!