I am trying to create 2 side by side scatter plots but I cannot figure out how to get my data to scatter, I tried the scatter command but keep getting an error message. data1 is for the left graph and data 2 for the right. How can I get my data to scatter on both graphs like so:
%data
data1 = [9.3 480; 9.7 501; 9.7 540; 9.7 552; 9.9 547; 10.2 622; 10.5 655; 11 701; 10.6 712; 10.6 708];
data2 =[169.8;99;129;159.1;108.8;118.7;78.4;27.8;57.9;17.6];
%make 2 figures
figure;
% add first plot in 2 x 1 grid
subplot(1,2,1);
ylabel('Engineering Doctorates');
xlabel ('Cheese Consumption');
box 'on'
axis square;
axis([9 11 500 700])
% add second plot in 2 x 1 grid
subplot(1,2,2);
ylabel('Kentucky Marriage Rate');
xlabel('Fishing Boat Drownings');
box 'on'
axis square;
axis([0 20 7 10]);
%white background
set(gcf,'color','w');

 Réponse acceptée

Sandro Lecci
Sandro Lecci le 18 Mai 2018
Dear mini ayachi,
you need to add a line after you declare each subplot to plot the data into the scatter plot.
subplot(1,2,1)
scatter(data1(:,1), data1(:,2), '+', ''markerfacecolor', 'b')
and
subplot(1,2,2)
scatter(data2(:,1), data2(:,2), 'x', ''markerfacecolor', 'b')
Best, Sandro

3 commentaires

Hi Sandro,
Thanks for your help.So it looks like that fixed one of the graphs but not the other. I also am not sure why the blue is not changing to black despite that I changed the color code.
%data
data1 = [9.3 480; 9.7 501; 9.7 540; 9.7 552; 9.9 547; 10.2 622; 10.5 655; 11 701; 10.6 712; 10.6 708];
data2 =[169.8;99;129;159.1;108.8;118.7;78.4;27.8;57.9;17.6];
%make 2 figures
figure;
% add first plot in 2 x 1 grid
subplot(1,2,1);
scatter(data1(:,1), data1(:,2), '+', 'MarkerFaceColor', '[0 0 0]');
ylabel('Engineering Doctorates');
xlabel ('Cheese Consumption');
box 'on'
axis square;
axis([7 12 450 800]);
set(gca,'Ticklength',[0 0])
% add second plot in 2 x 1 grid
subplot(1,2,2);
scatter(data2(:,1), data2(:,1), 'x', 'MarkerFaceColor', '[0 0 0]');
ylabel('Kentucky Marriage Rate');
xlabel('Fishing Boat Drownings');
box 'on'
axis square;
axis([0 20 7 10]);
%ticks
set(gca,'Ticklength',[0 0])
%white background
set(gcf,'color','w');
Sandro Lecci
Sandro Lecci le 18 Mai 2018
Modifié(e) : Sandro Lecci le 18 Mai 2018
Hi, in the second scatter plot command you are plotting data2(:,1) against himself.
Also, in the code you provide data2 is a 10x1 vector, I guess some data are missing. Concerning the color, remove the ' ' from before and after the brackets.
scatter(data1(:,1), data1(:,2), '+', 'MarkerFaceColor', [0 0 0]);
Best, Sandro
Minka Califf
Minka Califf le 18 Mai 2018
Works great! Thanks so much!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by