Effacer les filtres
Effacer les filtres

Need help putting multiple values in Legends

62 vues (au cours des 30 derniers jours)
Ruskin Patel
Ruskin Patel le 5 Oct 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?

Réponse acceptée

Mohammad Haghighat
Mohammad Haghighat le 5 Oct 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');

Plus de réponses (2)

Giovanni Mottola
Giovanni Mottola le 5 Oct 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 commentaire
Ruskin Patel
Ruskin Patel le 5 Oct 2016
Modifié(e) : Ruskin Patel le 5 Oct 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.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 5 Oct 2016
Modifié(e) : Walter Roberson le 5 Oct 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 commentaire
Walter Roberson
Walter Roberson le 6 Oct 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(:));

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by