How to set colour bar scale as a power of 10?

2 views (last 30 days)
Adnan Barkat
Adnan Barkat on 31 Mar 2022
Answered: Voss on 31 Mar 2022
Hi everyone,
May somehelp me to adjust the colour bar scale with only major ticks (at 1e-9, 1e-8, 1e-7, 1e-6)
Here is my script (data also attached)
clear all
clc
X = load('PDS_case.csv'); % input data
C_sort = sortrows(X,5);
Tri = C_sort(1:86,:);
data1 = sortrows(Tri,7);
PDS=(data1(:,7))';
data2=data1(:,8:66);
data4=data2';
Y=data4(:,1:86);
X=Y';
UU=[1e-8, 1.5e-8, 2e-8, 2.5e-8, 3e-8, 3.5e-8, 4e-8, 4.5e-8, 5e-8, 5.5e-7
6e-8, 6.5e-8, 7e-8, 7.5e-8, 8e-8, 8.5e-8, 9e-8, 9.5e-8, 10e-8, 10.5e-7]; % these values linked with each color bar, should be make a color bar
r{1}=[X(1:18,:)];
r{2}=[X(19:39,:)];
r{3}=[X(40:54,:)];
r{4}=[X(55:60,:)];
r{5}=[X(61:62,:)];
r{6}=[X(63:65,:)];
r{7}=[X(66,:)];
r{8}=[X(67:68,:)];
r{9}=[X(69:72,:)];
r{10}=[X(73:74,:)];
r{11}=[X(75:76,:)];
r{12}=[X(77:78,:)];
r{13}=[X(79,:)];
r{14}=[X(80,:)];
r{15}=[X(81,:)];
r{16}=[X(82:85,:)];
r{17}=[X(86,:)];
figure
cm = colormap(jet(numel(r)));
cm = colormap(jet);
cm = colormap(jet(numel(r)));
cc = colorbar('vert');
set(cc,'Ticks',((1:numel(UU))/numel(UU))');
set(cc,'TickLabels',num2str(UU'));
ylabel(cc,'Peak dynamic strain')
hold on
for k = 1:numel(r) % ... Then, Remove The 'NaN' Elements ...
ra = r{k};
ra = ra(~isnan(ra));
[f ,x ] = ecdf(ra);
[x ,ia ,ic ] = unique(x);
f = f(ia );
xx = linspace(min(x ),max(x ),100);
ff = interp1(x ,f ,xx );
ffs = smoothdata(ff , 'loess',20);
dx = mean(diff(xx ));
dfdxs = gradient(ffs )./dx ;
plot(xx , dfdxs , '-', 'linewidth', 1, 'Color',cm(k,:))
ylim([0, 8])
xlabel('R')
ylabel('Probability density')
caption = sprintf('Case 1:Events with R<0.5');
sgtitle(caption);
end
hold off
grid
Here are my results
Here is what i required.

Accepted Answer

Voss
Voss on 31 Mar 2022
This will make a log-scale colorbar with limits and ticks based on the vector UU (it doesn't go down to 1e-9 because UU doesn't go down to 1e-9, but you can adjust UU as required):
clear all
clc
X = load('PDS_case.csv'); % input data
C_sort = sortrows(X,5);
Tri = C_sort(1:86,:);
data1 = sortrows(Tri,7);
PDS=(data1(:,7))';
data2=data1(:,8:66);
data4=data2';
Y=data4(:,1:86);
X=Y';
% UU=[1e-8, 1.5e-8, 2e-8, 2.5e-8, 3e-8, 3.5e-8, 4e-8, 4.5e-8, 5e-8, 5.5e-7
% 6e-8, 6.5e-8, 7e-8, 7.5e-8, 8e-8, 8.5e-8, 9e-8, 9.5e-8, 10e-8, 10.5e-7]; % these values linked with each color bar, should be make a color bar
UU = (1:0.5:10.5)*1e-8;
r{1}=[X(1:18,:)];
r{2}=[X(19:39,:)];
r{3}=[X(40:54,:)];
r{4}=[X(55:60,:)];
r{5}=[X(61:62,:)];
r{6}=[X(63:65,:)];
r{7}=[X(66,:)];
r{8}=[X(67:68,:)];
r{9}=[X(69:72,:)];
r{10}=[X(73:74,:)];
r{11}=[X(75:76,:)];
r{12}=[X(77:78,:)];
r{13}=[X(79,:)];
r{14}=[X(80,:)];
r{15}=[X(81,:)];
r{16}=[X(82:85,:)];
r{17}=[X(86,:)];
figure
cm = colormap(jet(numel(r)));
cm = colormap(jet);
cm = colormap(jet(numel(r)));
cc = colorbar('vert');
% set(cc,'Ticks',((1:numel(UU))/numel(UU))');
% set(cc,'TickLabels',num2str(UU'));
% set(cc, ...
% 'Ticks',log10([1e-9 1e-8 1e-7]), ...
% 'TickLabels',{'10^{-9}' '10^{-8}' '10^{-7}'});
% clim(log10([1e-9 2e-7]));
ticks = ceil(log10(UU(1))):floor(log10(UU(end)));
set(cc, ...
'Ticks',ticks, ...
'TickLabels',sprintfc('10^{%d}',ticks));
clim(log10([min(UU) max(UU)]));
ylabel(cc,'Peak dynamic strain')
hold on
for k = 1:numel(r) % ... Then, Remove The 'NaN' Elements ...
ra = r{k};
ra = ra(~isnan(ra));
[f ,x ] = ecdf(ra);
[x ,ia ,ic ] = unique(x);
f = f(ia );
xx = linspace(min(x ),max(x ),100);
ff = interp1(x ,f ,xx );
ffs = smoothdata(ff , 'loess',20);
dx = mean(diff(xx ));
dfdxs = gradient(ffs )./dx ;
plot(xx , dfdxs , '-', 'linewidth', 1, 'Color',cm(k,:))
ylim([0, 8])
xlabel('R')
ylabel('Probability density')
caption = sprintf('Case 1:Events with R<0.5');
sgtitle(caption);
end
hold off
grid

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!