Plot overlaps in matlab
I have a strange problem that the plot overlaps in the graph, but not in the image in the same axis.
I am sure I didn't leave hold on somewhere or else it will overlap in the image itself as well.
EDIT : I want to get rid of the blue overlapped lines, I only want one blue line to be present in that pic.
Here is a sample :
(NOTE : The black image is a RGB image but I didn't plot that atm, so it开发者_JS百科 IS meant to be a transition from black to white on the graph. )
alt text http://img541.imageshack.us/img541/3212/parabolaaaaa.png
Some part of the code :
for K=1:23
hold on
I = fig.img.(['p' num2str(K)]);
bw=(I);
imshow(bw)
ss = bwlabel(bw);
s = regionprops(ss,'centroid');
centroids{K} = cat(1,s.Centroid);
hold(imgca,'on')
plot(imgca,centroids{K}(:,1), centroids{K}(:,2), 'r*'); hold on;
x=centroids{K}(:,1);
y=centroids{K}(:,2);
points=plot(x,y,'go',x,y,'rx');
hold on
axis on
axis fill
ccentroids = cat(1,centroids{:});
C1=ccentroids(:,1);
C2=ccentroids(:,2);
set(points,'XData',C1,'YData',C2);
.
.
.
p= polyfit(x2,y2,2)
parabola_x = linspace(-250,640,500);
parabola_polyval = polyval(p,parabola_x);
plot(parabola_x,parabola_polyval,'b-');
.
.
.
end
Any ideas?
The reason you have multiple blue lines is because you plot one for every pass through your loop with the line:
plot(parabola_x,parabola_polyval,'b-');
In fact, you are plotting everything (the images, points, and lines) over and over again in your loop without clearing the old ones.
You should instead initialize plot objects outside of your for loop and use the SET command to update them within the loop, instead of just replotting them. I gave one example of this in this answer to a question you previously asked where I discuss using the handles to plot objects to modify them. For the sample code you give here, you could do something like this:
hImage = imshow(bw(fig.img.p1)); %# Initialize the image
hold on; %# Add to the existing plot
hStar = plot(nan,nan,'r*'); %# Initialize the red star
hPoints = plot(nan,nan,'go',... %# Initialize the other points
nan,nan,'rx');
hLine = plot(nan,nan,'b-'); %# Initialize the blue line
for K = 1:23
I = fig.img.(['p' num2str(K)]);
bw = (I);
set(hImage,'CData',bw); %# Update the image
ss = bwlabel(bw);
s = regionprops(ss,'centroid');
centroids{K} = cat(1,s.Centroid);
set(hStar,'XData',centroids{K}(:,1),... %# Update the red star
'YData',centroids{K}(:,2));
ccentroids = cat(1,centroids{:});
C1 = ccentroids(:,1);
C2 = ccentroids(:,2);
set(hPoints,'XData',C1,'YData',C2); %# Update the other points
...
p = polyfit(x2,y2,2);
parabola_x = linspace(-250,640,500);
parabola_polyval = polyval(p,parabola_x);
set(hLine,'XData',parabola_x,... %# Update the blue line
'YData',parabola_polyval);
...
end
精彩评论