Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Running the next code I get a black bar in the colorbar that change in every loop.

If I change the limits, from 200 to 2000, and run for y= x.^2 +10*i +1000, 2nd version, then the bar sometimes appears , others not. Is anyone knows why? and how can make it work?

Is it possible to have a dynamic colorbar? i.e. if we plot a sound output, to show as colorbar the sound level in dB

EDITED:

x = 1:10;
figure;
for i = 1:10
y= x.^2 +10*i;
% y= x.^2 +10*i +1000; % 2nd version
plot(x,y,'-r'); hold on;
pause(1)
caxis([0 200]); 
% caxis([0 2000]); % 2nd version
cmap = jet(200);
% cmap = jet(2000);% 2nd version
cmap(y(end), :) = 0;
set(gcf, 'Colormap', cmap);
colorbar;
disp(y(end))
grid on;
end

thank you.

NEW EDIT:

based on the excellent answer by EBH, one supplementary question:

I am trying to add a second colobar, at left, but I cant make work both of them:

x = 1:10;
w1 = -15:15;
w2 = -1:1;
figure;
for i = 1:10

% %{
y= x.^2 +10*i +1000; %  
plot(x,y,'-r'); hold on;
pause(1)
caxis([0 2000]); %  
cmap1 = jet(2000);% 
cmap1(w1+y(end), :) = 0;
set(gcf, 'Colormap', cmap1);
h1=colorbar('eastoutside');
ylabel(h1, 'y')
disp(y(end))
%}


% %{
y2= x.^2 +5*i; %  
plot(x,y2,'-b'); hold on;
pause(1)
caxis([0 150]); 
cmap2 = jet(150);
cmap2(w2+y2(end-5), :) = 0; hold on;
cmap2(w2+y2(end), :) = 0; hold on;
set(gcf, 'Colormap', cmap2);
h2=colorbar('westoutside');
ylabel(h2, 'y2')
disp(y2(end-5))
disp(y2(end))

%}

grid on;
end

SO, can I make it work? is the problem the caxis?, and is it possible to decrease the width of both colorbar ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

The reason you sometimes don't see black on the colorbar is it's very thin, all you have to do is add some width to it, like this w below:

x = 1:10;
w = -5:5;
for k = 1:10
    y= x.^2 +10*k +1000;
    plot(x,y,'-r');
    hold on;
    pause(1)
    caxis([0 2000]);
    cmap = jet(2000);
    cmap(w+y(end), :) = 0;
    set(gcf, 'Colormap', cmap);
    colorbar;
    disp(y(end))
    grid on;
end

and the result:

dyn_colorbar


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...