필터 지우기
필터 지우기

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

조회 수: 103 (최근 30일)
Charlie
Charlie 2024년 1월 8일
답변: Voss 2024년 1월 8일
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
Sulaymon Eshkabilov 2024년 1월 8일
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