我正在绘制矩阵的特征值,因为我使用从0到10的线性间隔向量来更改矩阵内部的值。我想做的是使用颜色条来跟踪for循环的数量继续。也就是说,我希望当i = 1时颜色条为红色,并且将点绘制为红色,而当我= length(K)时,我希望颜色条为绿色,并绘制点。是绿色的。有没有办法做到这一点?我看过有关colorbar的帮助文档,但无法理解它的正面或反面。
否,colorbar
并不意味着是进度条。您可以通过以下方式实现自己想要的目标使用area
。请参阅代码中的注释以获取解释。
area
% generate some example data, probably you'd do this in the loop.
N = 100;
eig_vals = rand(1,N) + 2*sin((1:N)*pi/20);
% make colormap from red to green.
red = [1, 0, 0];
green = [0, 1, 0];
cmap = interp1([1, N], [red; green], 1:N);
cmap_nan = NaN(N, 3); % some colormap with NaNs as placeholder.
% Initialize the figure. I chose to use two subplots, one for the data,
% one for the progressbar. The data subplot spans 4 horizontal subplot
% 'positions', and the progressbar is plotted in the 5th subplot.
figure(1); clf;
ax(1) = subplot(1,5,1:4);
% initialize a scatter object.
s = scatter(1:N, zeros(N, 1), [], cmap_nan, 'filled');
% and set the axis limits to the range you expect
ax(1).XLim = [0 N];
ax(1).YLim = [min(eig_vals), max(eig_vals)];
% second subplot for the progressbar. Initially, I make N area plots, and
% set the FaceColors to white.
ax(2) = subplot(1,5,5);
a = area([1 2]+N, ones(2,N)/N, 'FaceColor', 'w', 'LineStyle', 'none');
% remove ticks and set YLim.
ax(2).XTick = [];
ax(2).YTick = [];
ax(2).YLim = [0 1];
% loop over data, or calculate data in the loop.
for k = 1:N
% code to calculate stuff?
% ...
% add the new data point in the scatterplot
s.YData(k) = eig_vals(k);
% set the color of the datapoint
s.CData(k, :) = cmap(k, :);
% and set the color of the appropriate face in the progressbar
a(k).FaceColor = cmap(k,:);
% update the figure:
drawnow
end
% That's it.