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

1 Ansicht (letzte 30 Tage)
Need:
Figure 1 should have red circle data points called "Iris Data (red)".
Figure 2 should have blue circle data points called "Synthetic Iris Data (blue)".
Figure 3 should be a combination of both graphs.
Code Below:
% 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);
%% PROBLEM 3
%% 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;
Outputs:
Figure1:
Figure2:
Figure3:
Figure3.png
  2 Kommentare
Aviel Moos
Aviel Moos am 23 Jul. 2019
can you uplode the full code?
Kristin Contreras
Kristin Contreras am 23 Jul. 2019
Bearbeitet: Kristin Contreras am 23 Jul. 2019
Figure 1 should have red circle data points called "Iris Data (red)".
Figure 2 should have blue circle data points called "Synthetic Iris Data (blue)".
Figure 3 should be a combination of both graphs.
% 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);
%% PROBLEM 3
%% 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;

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

infinity
infinity am 23 Jul. 2019
Bearbeitet: infinity am 23 Jul. 2019
Hello,
In the code of figure 2, you should change a bit like this
figure
Class1_Synthetic = scatter(SLClass1_Normalized,PWClass1_Normalized,'b')
hold on;
set(Class1_Synthetic,{'DisplayName'},{'Synthetic Iris Data (blue)'})
  2 Kommentare
Kristin Contreras
Kristin Contreras am 23 Jul. 2019
Hi Trung, I believe I had the "hold on" in my code as well.
infinity
infinity am 23 Jul. 2019
Bearbeitet: infinity am 23 Jul. 2019
Hello,
in my edited code, I did not change anything except
set(Class1_Synthetic,...
and
set(plotIris,...
Since, plotIris is handle of figure 1, and Class1_Synthetic is handle of figure 2. In figure 2, you keep handle of figure 1, therefore, you just again update in figure 1.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Aviel Moos
Aviel Moos am 23 Jul. 2019
In general if you want to apply something to a specifice figure you can write this before:
figure(2); % That will bring figure number 2 infront.
Just change to a specifice figure number.
  2 Kommentare
Kristin Contreras
Kristin Contreras am 23 Jul. 2019
Aviel, I have posted my code up above. Figure(2) did not allow the legend to be updated for Figure2.
Aviel Moos
Aviel Moos am 24 Jul. 2019
Try to change what Trung Showed you

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Number games finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by