Filter löschen
Filter löschen

Need help putting multiple values in Legends

62 Ansichten (letzte 30 Tage)
Ruskin Patel
Ruskin Patel am 5 Okt. 2016
Kommentiert: Walter Roberson am 6 Okt. 2016
I am fitting straight line into the data I have. I have 6 sets of data. My code fits straight line in for each data set. I have created a loop to put all datasets with their fits on top of each other, into a single plot
for N0=1:6
f = polyfit(T,Nl,1);
Nfit = polyval(f,T);
figure(1)
plot(T,Nl)
hold on;
plot(T,Nfit,'--')
hold on;
grid on;
end
Here the data set is (T,Nl) for different values of N0=[1:6]. Now I need to create a legend which can display each line along with its fit. Can anybody help me?

Akzeptierte Antwort

Mohammad Haghighat
Mohammad Haghighat am 5 Okt. 2016
First of all, you only need one "hold on". You don't need to repeat it after each plot.
You can add the legend after your loop by calling:
legend('description1','description2','description3','description4','description5','description6');

Weitere Antworten (2)

Giovanni Mottola
Giovanni Mottola am 5 Okt. 2016
Here I assumed, as it seems from your question, that T is a 1xm vector and that Nl is a 6xm matrix.
First I'd define an empty vector of plot handles (one for each data set):
vec=zeros(1, 6);
Then create once the first figure, add grid and tell MATLAB to keep adding new plots on top of old ones:
figure(1) % you put this in the for loop, but there's no need to make these calls more than once
hold on;
grid on;
Now I modify your code as follows:
for N0=1:6
f = polyfit(T,Nl(N0, :),1); % in your original code, Nl is not indexed, so I don't really understand how are you fitting different data sets
Nfit = polyval(f,T);
plot(T,Nl(N0, :)) % plot N0-th dataset
res=findobj(gca,'Type','line'); % returns handles of all line objects on current axes
h(N0)=plot(T,Nfit,'--', 'Color', res(1).Color); % this way the plot I add (fitted values) has the same color of the original data
vec(N0)=h(N0); % add current fit line handle to vector of handles
end
The results should now be something like the following figure:
Then, to add labels (but only to the fit lines) use the legend command:
legend(vec, {'line_1', 'line_2', 'line_3', 'line_4', 'line_5', 'line_6'}, 'Location', 'BestOutside')
Now the resulting figure is:
  1 Kommentar
Ruskin Patel
Ruskin Patel am 5 Okt. 2016
Bearbeitet: Ruskin Patel am 5 Okt. 2016
I need to distinguish between the data plotted and line fitted in the legend. I am plotting the data with solid line and the fit with dotted line. The legend should show line1 for both data and fit.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 5 Okt. 2016
Bearbeitet: Walter Roberson am 5 Okt. 2016
ph = gobjects(1, 6);
legs = cell(1, 6);
figure(1);
for N0=1:6
f = polyfit(T,Nl,1);
Nfit = polyval(f,T);
ph(N0) = plot(T,Nl);
legs{N0} = sprintf('set #%d', N0);
hold on;
plot(T,Nfit,'--')
grid on;
end
legend(ph, legs);
But I wonder if you are not trying for something closer to
ph = gobjects(1, 6);
legs = cell(1, 6);
figure(1);
for N0=1:6
Nl = N{N0};
f = polyfit(T,Nl,1);
Nfit = polyval(f,T);
ph(N0) = plot(T,Nl);
legs{N0} = sprintf('set #%d', N0);
hold on;
plot(T,Nfit,'--')
grid on;
end
legend(ph, legs);
which would require that the 6 datasets be in N{1}, N{2}, ... N{6} .
If you have a cell array of dataset names then you can assign those in place of the way I assigned legs{N0}, such as
legs{N0} = filenames{N0};
Note: if you are using R2014a or earlier, replace
ph = gobjects(1, 6);
with
ph = zeros(1, 6);
  1 Kommentar
Walter Roberson
Walter Roberson am 6 Okt. 2016
N = 6;
If you want both lines to be legend'd, then:
ph = gobjects(2, N);
legs = cell(2, N);
figure(1);
for N0=1:N
f = polyfit(T,Nl,1);
Nfit = polyval(f,T);
ph(1, N0) = plot(T,Nl);
legs{1, N0} = sprintf('line%d', N0);
hold on;
ph(2, N0) = plot(T,Nfit,'--');
legs{2, N0} = legs{1, N0}; %if they are to be the same
grid on;
end
legend(ph, legs(:));

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by