user3332603
user3332603

Reputation: 49

view one point in different positions

We want to show a point "called type1" in different positions (2,8,..), we used this code:

x = linspace(0,30,1000);
axis([0,20,-0.4,1.5]);
ax = gca;
h = hgtransform('Parent',ax);
type1=plot(x(1)-1,y(1),'s','Parent',h,'MarkerFaceColor','red','MarkerSize',20);
type2=plot(x(1)-1,y(1),'s','Parent',h,'MarkerFaceColor','green','MarkerSize',40);
type1.XData= 2;
hold on
type2.XData= 6;
hold on
type1.XData= 8;

But only the last position is showed

How to keep each showed point viewed in the figure??

Thanks inadvance

Upvotes: 0

Views: 63

Answers (1)

Suever
Suever

Reputation: 65430

The purpose of hold on is to allow you to have multiple plot objects on the same axes. So you will want a hold on statement between your two plot calls to ensure that they are both shown.

type1 = plot(x(1)-1,y(1),'s','Parent',h,'MarkerFaceColor','red','MarkerSize',20);
hold on
type2 = plot(x(1)-1,y(1),'s','Parent',h,'MarkerFaceColor','green','MarkerSize',40);

Now, when you change the XData property of one of these plots, that is modifying an existing plot object, and the old XData value is not going to be visible (hold on doesn't have anything to do with the contents of the plots, just the plot objects themselves).

If you want to plot multiple x values, you could create additional plot objects (one for each x position).

plot(2, y(1))
plot(6, y(1))
plot(8, y(1))

A better way is just to plot all the points up front in your initial plot commands.

plot(x, y, 's', 'Parent', h, 'MarkerFaceColor', 'r', 'MarkerSize', 20);
hold on
plot(x, y, 's', 'Parent', h, 'MarkerFaceColor', 'g', 'MarkerSize', 40);

Upvotes: 1

Related Questions