动态变化的Matlab colorbar指示器
Matlab colorbar indicator which dynamical change
运行 下一个代码我在颜色条中得到一个黑条,它在每个循环中都会发生变化。
如果我将限制从 200 更改为 2000,并且将 运行 更改为 y= x.^2 +10*i +1000
,第 2 版,则该条有时会出现,而其他则不会。有谁知道为什么?以及如何让它发挥作用?
是否可以有动态颜色条?即如果我们绘制一个声音输出,以 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的精彩回答,补充一个问题:
我正在尝试在左侧添加第二个颜色条,但我无法同时使用它们:
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 的问题吗?是否可以减小两个 colorbar 的宽度?
这里是关于如何在评论中实施@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",它实际上是一个图像。
多个颜色条
如果你想添加另一个颜色条,这很容易完成,这里是 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
给出:
左右颜色条:
在左侧添加另一个颜色条有点棘手:
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
您有时在颜色条上看不到黑色的原因是它太细了,您所要做的就是为其添加一些宽度,如下所示 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
结果:
在你最后的评论中,你想在一个数字上有 2 个不同的 colormaps
。这不是那么简单,因为 MATLAB 只支持每个图一个颜色图,这是另一个问题的主题。但是,既然你已经在这里问过了,我就 post 另一个答案。
所以它是这样的:
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
结果是:
运行 下一个代码我在颜色条中得到一个黑条,它在每个循环中都会发生变化。
如果我将限制从 200 更改为 2000,并且将 运行 更改为 y= x.^2 +10*i +1000
,第 2 版,则该条有时会出现,而其他则不会。有谁知道为什么?以及如何让它发挥作用?
是否可以有动态颜色条?即如果我们绘制一个声音输出,以 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的精彩回答,补充一个问题:
我正在尝试在左侧添加第二个颜色条,但我无法同时使用它们:
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 的问题吗?是否可以减小两个 colorbar 的宽度?
这里是关于如何在评论中实施@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",它实际上是一个图像。
多个颜色条
如果你想添加另一个颜色条,这很容易完成,这里是 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
给出:
左右颜色条:
在左侧添加另一个颜色条有点棘手:
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
您有时在颜色条上看不到黑色的原因是它太细了,您所要做的就是为其添加一些宽度,如下所示 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
结果:
在你最后的评论中,你想在一个数字上有 2 个不同的 colormaps
。这不是那么简单,因为 MATLAB 只支持每个图一个颜色图,这是另一个问题的主题。但是,既然你已经在这里问过了,我就 post 另一个答案。
所以它是这样的:
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
结果是: