24

I have two sets of data, (Ax, Ay; Bx, By). I'd like to plot both of these data sets on a scatter plot with different colors, but I can't seem to get it to work, because it seems scatter() does not work like plot(). Is it possible to do this?

I've tried...

scatter(Ax, Ay, 'g', Bx, By, 'b')

And

scatter(Ax, Ay, 'g')
scatter(Bx, By, 'b')

The first way returns an error. The latter only plots the Bx/By data.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark
  • 315
  • 3
  • 4
  • 6
  • See also this question: http://stackoverflow.com/questions/386712/is-there-any-way-to-silence-the-hold-function-in-matlab/386765#386765 – Jonas Mar 20 '10 at 02:51

3 Answers3

39

Try using hold on with the second example.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Qtax
  • 33,241
  • 9
  • 83
  • 121
  • That did the trick! Many thanks for the speedy and concise response. Much appreciated! – Mark Mar 20 '10 at 02:49
  • This may cause some headache if you are trying to combine it with adding legends. See [this question](http://stackoverflow.com/q/33059911/478116). – 3VYZkz7t Oct 04 '16 at 13:03
  • This is fine, but you are not able to plot all best fitting lines at once. – Windy Day Apr 23 '18 at 03:03
6

plot (ax,ay,'g.') generates a scatter plot with green dots

if you want bigger circles, you can use

plot (ax,ay,'g.', 'MarkerSize', XX) %XX = 20 or whatever

To make open circles

plot (ax, ay, 'go')

As you know, plot can be chained, so you can do it one go with

plot (ax, ay, 'go', bx, by, 'bo')

The difference between plot and scatter is that scatter lets you specify the marker size, but you're not asking to do that here.

Marc
  • 5,315
  • 5
  • 30
  • 36
4

Another option is to use gscatter. The parameters are different, but it is sometimes more useful than scatter(...); hold on; scatter(...);

Michael Kopinsky
  • 884
  • 1
  • 7
  • 14