- Create a new figure and axes for the colorbar.
- Plot a colorbar using the colorbar function with a suitable range of values.
- Set the CLim property of each subplot to the same range of values used in the colorbar.
- Adjust the position of the subplots and colorbar axes to make them fit nicely in the figure.
How can use a common colorbar (cbarrow) for multiple number of figures?
41 views (last 30 days)
Show older comments
I have four different plot. Now I want to use a single/common colour bar instead of four different colorbar for this four figure with 'cbarrow' command. A example of my code are also attached with this file.
subplot(2,2,1);
imagesc(long,lati,max_tmax_k2_15'-max_tmax_k_15','alphadata',~isnan(max_tmax_k2_15'));
set(gca,'Ydir','normal');
xlabel('Longitude');
ylabel('Latitude');
%%TITLE({'2015-2040'})
xlim([85.5 98])
ylim([20.5 30])
caxis([-2 2])
grid on
colorbar
colormap(flipud(colormap))
hold on
plot(X,Y)
xlim([85.5 98])
ylim([20.5 30])
hold off
subplot(2,2,2);
imagesc(long,lati,max_tmax_k3_15'-max_tmax_k_15','alphadata',~isnan(max_tmax_k3_15'));
set(gca,'Ydir','normal');
xlabel('Longitude');
ylabel('Latitude');
%TITLE({'2041-2070'})
xlim([85.5 98])
ylim([20.5 30])
caxis([-2 2])
% % %%caxis([18 51])
grid on
colorbar
colormap(flipud(colormap))
hold on
plot(X,Y)
xlim([85.5 98])
ylim([20.5 30])
hold off
subplot(2,2,3)
imagesc(long,lati,max_tmax_k4_15'-max_tmax_k_15','alphadata',~isnan(max_tmax_k4_15'));
set(gca,'Ydir','normal');
xlabel('Longitude');
ylabel('Latitude');
%TITLE({'2071-2100'})
xlim([85.5 98])
ylim([20.5 30])
caxis([-2 2])
% % %%caxis([18 51])
grid on
colorbar
colormap(flipud(colormap))
hold on
plot(X,Y)
xlim([85.5 98])
ylim([20.5 30])
hold off
subplot(2,2,4)
imagesc(long,lati,max_tmax_k6_15'-max_tmax_k_15','alphadata',~isnan(max_tmax_k6_15'));
set(gca,'Ydir','normal');
xlabel('Longitude');
ylabel('Latitude');
%TITLE({'2015-2040'})
% colormap(parula)
xlim([85.5 98])
ylim([20.5 30])
caxis([-2 2])
grid on
colorbar
colormap(flipud(colormap))
hold on
plot(X,Y)
xlim([85.5 98])
ylim([20.5 30])
hold off
Ja = customcolormap_preset('pink-white-green');
colormap(custommap(12,Ja))
colormap(flipud(colormap))
0 Comments
Answers (1)
Shubham
on 6 Mar 2023
Hi Ashesh,
To create a single colorbar for the four subplots, you can follow these steps:
Here's an example code snippet that shows how to do this:
% Create a new figure for the colorbar
fig = figure;
cbax = axes('Position', [0.85 0.15 0.05 0.7]);
% Plot a colorbar with a suitable range of values
caxis([-2 2]);
colorbar(cbax, 'Location', 'westoutside', 'TickDirection', 'out');
% Set the CLim property of each subplot to the same range of values
for i = 1:4
subplot(2,2,i);
caxis([-2 2]);
end
% Adjust the position of the subplots and colorbar axes
set(gcf, 'Position', [100 100 800 600]);
set(gca, 'Position', [0.1 0.15 0.7 0.7]);
set(cbax, 'Position', [0.85 0.15 0.05 0.7]);
You can insert this code snippet after your existing code to create the colorbar for the four subplots. Note that you may need to adjust the position and size of the subplots and colorbar axes to make them fit nicely in your figure.
0 Comments
See Also
Categories
Find more on Title in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!