recipes : Plotting : Line and Scatter Plots : How do I make a scatter plot?

Problem

How do I make a scatter plot and modify the properties of the points?

SolutionYou can use the plot or the scatter commands. First I will show examples of obtaining a few different styles using plot, before moving on to scatter.

%Make some random data to plot X=0:0.01:1; Y=5*X+randn(size(X)); plot(X,Y,'ok') %Plot the data using open black circles plot(X,Y,'ok','MarkerFaceColor','r') %Black circles filled in red plot(X,Y,'.b') %Blue points plot(X,Y,'or','MarkerSize',10,'MarkerFaceColor','r') %Larger red circles

Note that by default, the plot command joins up points with a line (try running plot(X,Y)). Similar plots can be made with the scatter command, but this also allows for some extra tricks:

%Make some random data to plot X=0:0.01:1; Y=5*X+randn(size(X)); %Plot the data using open blue circles. %The third argument defines the symbol size scatter(X,Y,60) %Same as above, but with filled points that are %coloured sequentially using the jet color map scatter(X,Y,60,jet(length(X)),'filled')

Discussion

As you can see, there are various ways you can modify a scatter plot. If you run help plot and help scatter you will see the other options available at the command line when you create the plot. However, there are plenty of modifications you can apply to plots after they have been made.

%Make a plot with some random data and keep the plot handle (H) H=plot(rand(1,100),'r*'); %You can see all the stuff you can modify by executing; get(H) DisplayName: '' Annotation: [1x1 hg.Annotation] Color: [1 0 0] LineStyle: 'none' LineWidth: 0.5000 Marker: '*' % etc %You can modify these as follows: set(H,'LineWidth',2) %If you aren't sure what values a particular parameter will take, %you can call set without defining a parameter: >> set(H,'LineStyle') [ {-} | -- | : | -. | none ]