Matlab colorbar指标哪个动态变化

时间:2016-09-26 18:13:56

标签: matlab dynamic matlab-figure colorbar colormap

运行下一个代码我在颜色条中得到一个黑色条,在每个循环中都会发生变化。

如果我更改限制,从200更改为2000,并运行y= x.^2 +10*i +1000,第二个版本,则有时会显示栏,其他则不会。有谁知道为什么?以及如何使其发挥作用?

是否可以使用动态颜色条?即如果我们绘制声音输出,则以dB为单位显示声音级别

编辑:

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
谢谢。

新编辑:

基于EBH的优秀答案,一个补充问题:

我正在尝试在左边添加第二个colobar,但我不能同时完成它们的工作:

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

那么,我可以让它发挥作用吗?问题是caxis?,是否可以减小两个颜色条的宽度?

3 个答案:

答案 0 :(得分:1)

以下是关于如何在评论中实施@Suever建议的想法:

x = 1:10;
cb_width = 0.04;
c = sum(jet(4000),2);
c = c(1:2000);
h = imagesc(c);
h.Parent.Position(1) = 1-cb_width-0.07;
h.Parent.Position(3) = cb_width;
h.Parent.YAxisLocation = 'right';
h.Parent.XAxis.Visible = 'off';
axis xy
box off
colormap jet
ax = axes;
ax.Position(3) =  ax.Position(3) - cb_width;
grid on;
hold on;
w = -5:5; % <-- this is very important!
for k = 1:10
    h.CData = c;
    y = x.^2 +10*k +1000;
    plot(ax,x,y,'-r');
    h.CData(w+y(end),1) = nan;
    drawnow;
    disp(y(end))
    pause(1)
end

此代码在图中创建2个轴,一个用于plot,另一个用于“colorbar”,实际上是图像。

dyn_colorbar

多个颜色栏

如果你想添加另一个颜色条,可以轻松完成,这里是3的例子:

x = 1:10;
cb_width = 0.08; % for all the colorbars together
c = sum(jet(4000),2);
% we define the colorbars area as [c nan c nan c]:
c = [c(1:2000) nan(2000,1) c(1:2000) nan(2000,1) c(1:2000)];
h = imagesc(c);
h.Parent.Position(1) = 1-cb_width-0.07;
h.Parent.Position(3) = cb_width;
h.Parent.YAxisLocation = 'right';
h.Parent.XAxis.Visible = 'off';
axis xy
box off
f = gcf;
back = f.Color;
cmap = [back; jet(2000); 0 0 0];
colormap(cmap)
% now we start to draw the data:
ax = axes;
ax.Position(3) =  ax.Position(3) - cb_width;
grid on;
hold on;
w = -5:5;
for k = 1:10
    h.CData = c;
    y = x.^2 +10*k +1000;
    z = x.^2 +15*k +500;
    t = x.^2 +30*k +700;
    plot(ax,x,y,'-r',x,z,'-.b',x,t,'--m');
    h.CData(w+y(end),1) = 2.1; % <-- left bar
    h.CData(w+z(end),3) = 2.1; % <-- middle bar
    h.CData(w+t(end),5) = 2.1; % <-- right bar
    drawnow;
    disp(y(end))
    pause(1)
end

给出:

dyn_colorbar3

左右色条:

在左侧添加另一个颜色条有点棘手:

x = 1:10;
cb_width = 0.88; % for all the colorbars together
c = sum(jet(4000),2);
% we define the colorbars area as [c nan c nan c]:
c = [c(1:2000) nan(2000,50) c(1:2000)];
h = imagesc(c);
h.Parent.Position(1) = h.Parent.Position(1)-0.07;
h.Parent.Position(3) = cb_width;
axis xy
box off
h.Parent.XAxis.Visible = 'off';
yyaxis left
h.Parent.YAxis(1).Color = [0 0 0];
yyaxis right
h.Parent.YAxis(2).Color = [0 0 0];
h.Parent.YAxis(2).Limits = h.Parent.YAxis(1).Limits;
% make the nan transparent
cmap = [1 1 1; jet(2000); 0 0 0];
colormap(cmap)
% now we start to draw the data:
ax = axes;
% ax.Position(3) =  ax.Position(3) - cb_width;
grid on;
hold on;
w = -5:5;
for k = 1:10
    h.CData = c;
    y = x.^2 +10*k +1000;
    z = x.^2 +15*k +500;
    plot(ax,x,y,'-r',x,z,'-.b',x,t,'--m');
    h.CData(w+y(end),1) = 2.1; % <-- left bar
    h.CData(w+z(end),52) = 2.1; % <-- right bar
    drawnow;
    disp(y(end))
    pause(0.2)
end

left_and_right

答案 1 :(得分:1)

你有时候在彩条上看不到黑色的原因是它非常薄,所有你需要做的就是增加一些宽度,如下面w所示:

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

结果:

dyn_colorbar

答案 2 :(得分:1)

在您上次发表评论时,您希望在一个数字上有2个不同的colormaps。这不是那么简单,因为MATLAB每个图只支持一个色图,而且它是另一个问题的主题。但是,既然你已经在这里问了我,我会发布另一个答案。

所以它是这样的:

x = 1:10;
w = -15:15;
cmap = [jet(2000); jet(2000)];

% left colorbar
lcb = subplot(1,3,1);
caxis([1 2000])
h1 = colorbar('west','Position',[0.1 0.11 0.05 0.815]);
h1.Limits = [1 1000];
h1.TickLabels = num2str(linspace(200,2000,numel(h1.Ticks)).');
ylabel(h1,'y')
axis off

% right colorbar
rcb = subplot(1,3,3);
caxis([1 150])
h2 = colorbar('east','Position',[0.85 0.11 0.05 0.815]);
h2.Limits = [76 150];
h2.TickLabels = num2str(linspace(10,150,numel(h2.Ticks)).');
ylabel(h2, 'y2')
axis off

% main axes
ax = axes;
ax.Position = [0.2 0.11 0.62 0.815];
grid on
hold on

scale = floor(2000/150);

for k = 1:10
    y = x.^2 +10*k +1000;
    y2= x.^2 +5*k;

    cmap = [jet(2000); jet(2000)];
    cmap(w+y(end),:) = 0; 
    disp(y(end))

    cmap(scale+2000+w+y2(end-5)*scale, :) = 0;
    cmap(scale+2000+w+y2(end)*scale, :) = 0;
    disp([y2(end-5) y2(end)])

    colormap(cmap)
    plot(ax,x,y,'-r',x,y2,'-b');
    pause(0.1)
end

结果是:

dyn_cb2_alt