use marker fill colors according to data value in MATLAB

独自空忆成欢 提交于 2019-12-04 12:57:31
bla

Here is a very simple, not so efficient but very easy to read, way to do this:

% create fake data
x=linspace(-10,10,100);
y=sin(x);
c=randi(numel(x),1,numel(x));
cmap=colormap(jet(numel(x)));

% plot the lines
plot(x,y,'--'); hold on

% plot the squares, one at a time according to color vector c
% I added some randome noise to y to get the image nicer... 
for n=1:numel(x)
    plot(x(n),y(n)+0.3*(rand-0.5),'s','MarkerFaceColor',cmap(c(n),:));hold on
end
hold off

On a completely different not, this question reminded me of an answer I gave a while ago on a similar subject (see here)...

Use the scatter function. It can take up to two extra parameters, the size of each point and the color.

To expand a bit:

You will need an X vector. Since you appear to be plotting against point number, you'll need to define one. For the color gradient, I typically go from blue to red, but you will need to play around with it to see what you like.

A = rand(100,3);
[M,N] = size(A);
X = 1:M;
S = 35; % size of symbols in pixels

% normalize vector to go from zero to 1
normValue = (A(:,3)-min(A(:,3)))./(max(A(:,3))-min(A(:,3)))

% this will do blue to red. play around with it to get the color scheme you want
C = [normValue zeros(size(normValue)) 1-normValue];

figure
scatter(X,A(:,1),S,C,'Marker','s')
line(X,A(:,2),'Color','r','LineWidth',2)

(This is a minimum working example, obviously replace the random data with your own) Note that scatter won't draw lines between the points, so you will have to either do without them or plot the data twice, first with a plot function and then with a scatter on top.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!