0

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

  • Rather than changing the `XData` you will need to create a *new* plot with each different value of x if you want to see the old ones. – Suever Jun 21 '16 at 21:40
  • @Suever we want the points to be showed in the same figure (without opening a new figure) shouldn't this be done using hold on?? – user3332603 Jun 21 '16 at 21:56
  • `hold on` works *if you have separate plot objects you want to show*. If you modify the `XData` position you are changing an existing plot so `hold on` has no effect there. You will want a `hold on` between the two calls to `plot` so that they are both shown though. – Suever Jun 21 '16 at 21:57

1 Answers1

1

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);
Suever
  • 64,497
  • 14
  • 82
  • 101