How to get a colorbar to show only a specified range of values?

90 次查看(过去 30 天)
I'm plotting a data set with a wide range of values (10 to 100000), but I only want to plot the values 10 to 300
Is there a way to have both the plotted data and colorbar range to 10-300, and rescale the colors to just this range? This is what I currently have.
contourf(X_ert, Z_ert, log(ert_interp), ncont, 'edgecolor', 'none');
colormap('jet');
cb = colorbar('FontSize', 18, 'YTick', log([10 50 100 200 300]), 'YTickLabel', [10 50 100 200 300]);

回答(2 个)

Sulaymon Eshkabilov
Use caxis() command. E.g.:
% Create a data set:
D = rand(200, 200) * 1e5 + 10;
% Visualize the data set:
imagesc(D)
% Assign the colorbar scale/range to: 10.. to .. 300
caxis([10 300])
% Add colorbar:
colorbar
xlabel('X')
ylabel('Y')
title('Data Plot')
fprintf('MAX value of the data: DMax = %f \n', max(D(:)))
MAX value of the data: DMax = 100008.594421
fprintf('MIN value of the data: DMin = %f \n', min(D(:)))
MIN value of the data: DMin = 10.131180

Voss
Voss 2024-1-8
Use clim(). Example:
% made up variable values:
X_ert = 1:550;
Z_ert = 2080:2180;
ert_interp = exp(cosd(X_ert(ones(numel(Z_ert),1),:))*10);
min_val = min(ert_interp(:));
max_val = max(ert_interp(:));
ert_interp = (ert_interp-min_val)./(max_val-min_val)*(100000-10)+10;
ncont = 64;
figure
subplot(2,1,1)
contourf(X_ert, Z_ert, log(ert_interp), ncont, 'edgecolor', 'none');
colormap('jet');
cb = colorbar('FontSize', 18, 'YTick', log([10 50 100 200 300]), 'YTickLabel', [10 50 100 200 300]);
title('Without clim()')
subplot(2,1,2)
contourf(X_ert, Z_ert, log(ert_interp), ncont, 'edgecolor', 'none');
colormap('jet');
cb = colorbar('FontSize', 18, 'YTick', log([10 50 100 200 300]), 'YTickLabel', [10 50 100 200 300]);
clim(log([10 300]))
title('With clim()')

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by