How do you update the legend of Figure 2 and Figure 3 instead of Figure 1?

3 views (last 30 days)
Need:
Figure 1 should have red circle data points called "Iris Data (red)" with correct legend.
Figure 2 should have blue circle data points called "Synthetic Iris Data (blue)" with correct legend.
Figure 3 should be a combination of both graphs with correct legend.
(Iris.xls data set is attached)
Here is my erroneous code that displays the wrong legends:
%% The column vector, |species|, consists of iris flowers of three
% different species, setosa, versicolor, virginica. The double matrix
% |features| consists of four types of measurements on the flowers, the length
% and width of sepals and petals in centimeters, respectively.
features = xlsread("iris.xls", 2); %sepal length, sepal width, petal length, petal width
labels = xlsread("iris.xls", 3); % flower type classes
species = cell(size(labels));
species(labels==1)={'Setosa'};
species(labels==2)={'Versicolor'};
species(labels==3)={'Virginica'};
% The names of features are stored in an array.
feature_names = ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"];
%% Features
% Use petal length (third column in |features| ) and petal width (fourth column
% in |features| ) measurements. Save these as variables PL and PW,
% respectively.
% 4 features & 3 classes
SL = features(:,1);
SW = features(:,2);
PL = features(:,3);
PW = features(:,4);
%% Recall Original Iris Data
% Recall Iris Setosa
Class1 = features(1:50,:);
SLClass1 = features(1:50,1);
SWClass1 = features(1:50,2);
PLClass1 = features(1:50,3);
PWClass1 = features(1:50,4);
% Recall Iris Versicolor
Class2 = features(51:100,:);
SLClass2 = features(51:100,1);
SWClass2 = features(51:100,2);
PLClass2 = features(51:100,3);
PWClass2 = features(51:100,4);
% Recall Iris Virginica
Class3 = features(101:150,:);
SLClass3= features(101:150,1);
SWClass3= features(101:150,2);
PLClass3= features(101:150,3);
PWClass3= features(101:150,4);
%% Synthetic Data
%% Class 1 - SL vs. PW
% Plot Original Class 1 - Iris SL vs. PW.
figure
plotIris = scatter(SLClass1,PWClass1,'r')
hold on;
set(plotIris,{'DisplayName'},{'Iris Data (red)'})
legend show;
title('Original Iris Data - Class 1 - Sepal Length vs. Petal Width');
xlabel('Sepal Length');
ylabel('Petal Width');
hold off;
%% Generate Random Data
% Create Random Data with 100 Additional Observations for Each Class
% Class 1 Randoms. Generate 100 extra observations. Random Data must be 100 x 4 and normally distributed.
Class1_r = randn(100,4);
%Class1_r = randi([0 1],100,4); % Randi did not create a truly random data.
SLClass1_r = Class1_r(:,1);
SWClass1_r = Class1_r(:,2);
PLClass1_r = Class1_r(:,3);
PWClass1_r = Class1_r(:,4);
a = min(SLClass1_r(:));
b = max(SLClass1_r(:));
ra = 0.0886;
rb = 0.4684;
SLClass1_Normalized = (((ra-rb) * (SLClass1_r - a)) / (b - a)) + rb;
a = min(PWClass1_r(:));
b = max(PWClass1_r(:));
ra = -0.9200;
rb = -0.5200;
PWClass1_Normalized = (((ra-rb) * (PWClass1_r - a)) / (b - a)) + rb;
% Plot Synthetic Data - Class 1 - SL vs. PW
figure(2)
Class1_Synthetic = scatter(SLClass1_Normalized,PWClass1_Normalized,'b')
hold on;
set(plotIris,{'DisplayName'},{'Synthetic Iris Data (blue)'})
legend show
title('Synthetic Iris Data - Class 1 - Sepal Length vs. Petal Width');
xlabel('Sepal Length');
ylabel('Petal Width');
% Plot Combined Graphs - Class 1 - SL vs. PW
figure;
plotIris = scatter(SLClass1,PWClass1,'r')
set(plotIris,{'DisplayName'},{'Iris Data (red)'})
hold on;
legend show
title('Class 1 - SL vs. PW - Original & Synthetic Data');
xlabel('Sepal Length');
ylabel('Petal Width');
Class1_Synthetic = scatter(SLClass1_Normalized,PWClass1_Normalized,'b')
set(plotIris,{'DisplayName'},{'Synthetic Iris Data (blue)'})
legend show
hold off;
Figure1 Legend Error:Figure1_updated.png
Figure2 Legend Error:
Figure2.png
Figure3 Legend Error:
Figure3.png

Accepted Answer

Kristin Contreras
Kristin Contreras on 25 Jul 2019
Edited: Kristin Contreras on 25 Jul 2019
When using the set function for Figure 2 and Figure 3,I had accidentally kept referring to the same plot name for Figure 1. Here is my corrected code.
%% The column vector, |species|, consists of iris flowers of three
% different species, setosa, versicolor, virginica. The double matrix
% |features| consists of four types of measurements on the flowers, the length
% and width of sepals and petals in centimeters, respectively.
features = xlsread("iris.xls", 2); %sepal length, sepal width, petal length, petal width
labels = xlsread("iris.xls", 3); % flower type classes
species = cell(size(labels));
species(labels==1)={'Setosa'};
species(labels==2)={'Versicolor'};
species(labels==3)={'Virginica'};
% The names of features are stored in an array.
feature_names = ["Sepal Length", "Sepal Width", "Petal Length", "Petal Width"];
%% Features
% Use petal length (third column in |features| ) and petal width (fourth column
% in |features| ) measurements. Save these as variables PL and PW,
% respectively.
% 4 features & 3 classes
SL = features(:,1);
SW = features(:,2);
PL = features(:,3);
PW = features(:,4);
%% Recall Original Iris Data
% Recall Iris Setosa
Class1 = features(1:50,:);
SLClass1 = features(1:50,1);
SWClass1 = features(1:50,2);
PLClass1 = features(1:50,3);
PWClass1 = features(1:50,4);
% Recall Iris Versicolor
Class2 = features(51:100,:);
SLClass2 = features(51:100,1);
SWClass2 = features(51:100,2);
PLClass2 = features(51:100,3);
PWClass2 = features(51:100,4);
% Recall Iris Virginica
Class3 = features(101:150,:);
SLClass3= features(101:150,1);
SWClass3= features(101:150,2);
PLClass3= features(101:150,3);
PWClass3= features(101:150,4);
%% Synthetic Data
%% Class 1 - SL vs. PW
% Plot Original Class 1 - Iris SL vs. PW.
figure
plotIris1 = scatter(SLClass1,PWClass1,'r')
hold on;
set(plotIris1,{'DisplayName'},{'Iris Data (red)'})
legend show;
title('Original Iris Data - Class 1 - Sepal Length vs. Petal Width');
xlabel('Sepal Length');
ylabel('Petal Width');
hold off;
%% Generate Random Data
% Create Random Data with 100 Additional Observations for Each Class
% Class 1 Randoms. Generate 100 extra observations. Random Data must be 100 x 4 and normally distributed.
Class1_r = randn(100,4);
%Class1_r = randi([0 1],100,4); % Randi did not create a truly random data.
SLClass1_r = Class1_r(:,1);
SWClass1_r = Class1_r(:,2);
PLClass1_r = Class1_r(:,3);
PWClass1_r = Class1_r(:,4);
a = min(SLClass1_r(:));
b = max(SLClass1_r(:));
ra = 0.0886;
rb = 0.4684;
SLClass1_Normalized = (((ra-rb) * (SLClass1_r - a)) / (b - a)) + rb;
a = min(PWClass1_r(:));
b = max(PWClass1_r(:));
ra = -0.9200;
rb = -0.5200;
PWClass1_Normalized = (((ra-rb) * (PWClass1_r - a)) / (b - a)) + rb;
% Plot Synthetic Data - Class 1 - SL vs. PW
figure(2)
Class1_Syntheticslpw = scatter(SLClass1_Normalized,PWClass1_Normalized,'b')
hold on;
set(Class1_Syntheticslpw,{'DisplayName'},{'Synthetic Iris Data (blue)'})
legend show
title('Synthetic Iris Data - Class 1 - Sepal Length vs. Petal Width');
xlabel('Sepal Length');
ylabel('Petal Width');
% Plot Combined Graphs - Class 1 - SL vs. PW
figure;
plotIris1 = scatter(SLClass1,PWClass1,'r')
set(plotIris1,{'DisplayName'},{'Iris Data (red)'})
hold on;
legend show
title('Class 1 - SL vs. PW - Original & Synthetic Data');
xlabel('Sepal Length');
ylabel('Petal Width');
Class1_Synthetic_slpw = scatter(SLClass1_Normalized,PWClass1_Normalized,'b')
set(Class1_Synthetic_slpw,{'DisplayName'},{'Synthetic Iris Data (blue)'})
legend show
hold off;

More Answers (2)

Mario Chiappelli
Mario Chiappelli on 24 Jul 2019
I have always used the legend function in the following way:
legend('nameOfGraphOne','nameOfGraphTwo',etc);
I see you are implementing a legend in the same line as the plot. I believe if you eliminate that and insert a legend using the legend function, it should work.
  1 Comment
Kristin Contreras
Kristin Contreras on 25 Jul 2019
Great tip, Mario! Thank you!
Should I use the set function in order for this to work?

Sign in to comment.


Mario Chiappelli
Mario Chiappelli on 25 Jul 2019
No you don't need to. Here is some example code that I used for a plot for a class project.
figure(1);
hold on
plot(data_time,data_hour,"r-");
plot(data_time2,data_hour2,'r--');
xlabel('time(s)');
ylabel('light intensity in DANA 303');
hold off
ylim([0 10000]);
title('light intensity vs time (s) in DANA 303');
legend('Marios Sensor','Devins Sensor')

Categories

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!