How to plot this in matlab?
1 view (last 30 days)
Show older comments
I have following code
xr=randi([1 150],1,20)
z=numel(xr);
N=10; %Window length
gAll=zeros(1,z-N+1);
for n=0:z-N;
x=xr(1+n:N+n)
d=max(x);
m=numel(x);
y=zeros(d,1);
p=zeros(d,d);
for k=1:m-1
y(x(k))=y(x(k))+1;
p(x(k),x(k+1))=p(x(k),x(k+1))+1;
end
p=bsxfun(@rdivide,p,y);
p(isnan(p)) = 0;
j=prod(p(p~=0));
[~,~,idx] = unique(x);
q=prod(hist(idx,1:max(idx))/numel(x));
s=log(j);
l=log(q);
g=s+l
gAll(n+1)=g;
end
plot(gAll)
I want a plot such a that if gAll is greater than -22 then it should be in red color and if gAll less than -22 then it should in blue color, how to do it.
0 Comments
Answers (1)
dpb
on 18 May 2016
Edited: dpb
on 18 May 2016
ix=gAll>yLim; % yLim would be -22 for the above wished-for case...
x=[1:length(gAll)]; % x vector to plot to satisfy plot syntax
plot(x(ix),gAll(ix),'r',x(~ix),gAll(~ix),'r')
This will leave 'holes' between nonadjacent sections; you'll have to decide how you want to transition between. 'color' is a line property so there must be a line for each color.
ADDENDUM
Your 1) is precisely what I alluded to with 'holes'; 2) means the plot was drawn knowing those intersection points. It's relatively simple to find the locations--
First find the locations of the crossover; since length(diff(x)) is one less than length(x), the following finds the point prior to each crossing then interpolates from that point to the next to find the intersection point in the x-direction--
iCross=find(abs(diff(sign(y-yLim)))==2);
xCrs=arrayfun(@(ix) interp1(y(ix:ix+1),ix:ix+1,yLim),iCross);
Then augment the x, gAll vectors() with these points sorted on x and switch the index for the *plot to include >= instead of just > and conversely for the lower direction since need to include the boundary point in each set (or, duplicate the xCrs array and add/subtract a minute delta from each so the inequality is satisfied but the delta is so small as to be inconsequential on the plot).
I've not looked; perhaps somebody has previously submitted this enhancement as a File Exchange submittal...
(*) The augmented gAll vector values are, of course, yLim
I've only a few spare minutes at a time so getting enough to do more than the outline is hard but--try this demo
x=1:20;x=x.'; y=rand(size(x)); % arbitrary dataset
yLim=0.6; % set the threshold value
% find intersection index w/ threshold and resulting x
iCrs=find([abs(diff(sign(y-yLim)))]==2);
xCrs=arrayfun(@(ix) interp1(y(ix:ix+1),ix:ix+1,yLim),iCrs);
% Augment original x with crossing points and reorder
[xAug,iAug]=sort([x;xCrs]);
yAug=[y; ylim*ones(size(xCrs))];
yAug=yAug(iAug);
% segregate augmented vector on threshold and plot two sections
ix=yAug>=ylim;
yPlus=yAug;yPlus(~ix)=nan;
plot(xAug,yPlus,'r')
ix=yAug<=ylim;
yMinus=yAug;yMinus(~ix)=nan;
hold on
plot(xAug,yMinus,'b')
I've used temporary arrays throughout to hopefully clarify the logic of "who's who in the zoo" and so you can see the intermediaries to see what's happening. It basically implements what I outlined before with the exception I had forgotten to mention needing to "blank out" the intervening sections to avoid the line across the threshold level. If you're going to draw in that line as well, then you can skip that step and allow the threshold line to obscure the other two and thus eliminate the need for the yPlus|yMinus and the setting of the opposite section to NaN.
That is, if the end result is going to have the threshold value line then
ix=yAug>=ylim;plot(xAug(ix),yAug(ix),'r')
hold on
ix=yAug<=ylim;plot(xAug(ix),yAug(ix),'b')
hL=line([x(1) x(end)],yLim*[1 1]);set(hL,'color','k')
will suffice. As always, "salt to suit"...
2 Comments
See Also
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!