Need help putting multiple values in Legends

79 visualizaciones (últimos 30 días)
Ruskin Patel
Ruskin Patel el 5 de Oct. de 2016
Comentada: Walter Roberson el 6 de Oct. de 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?

Respuesta aceptada

Mohammad Haghighat
Mohammad Haghighat el 5 de Oct. de 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');

Más respuestas (2)

Giovanni Mottola
Giovanni Mottola el 5 de Oct. de 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 comentario
Ruskin Patel
Ruskin Patel el 5 de Oct. de 2016
Editada: Ruskin Patel el 5 de Oct. de 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.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 5 de Oct. de 2016
Editada: Walter Roberson el 5 de Oct. de 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 comentario
Walter Roberson
Walter Roberson el 6 de Oct. de 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(:));

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by