필터 지우기
필터 지우기

Need help putting multiple values in Legends

조회 수: 62 (최근 30일)
Ruskin Patel
Ruskin Patel 2016년 10월 5일
댓글: Walter Roberson 2016년 10월 6일
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?

채택된 답변

Mohammad Haghighat
Mohammad Haghighat 2016년 10월 5일
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');

추가 답변 (2개)

Giovanni Mottola
Giovanni Mottola 2016년 10월 5일
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
Ruskin Patel
Ruskin Patel 2016년 10월 5일
편집: Ruskin Patel 2016년 10월 5일
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.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2016년 10월 5일
편집: Walter Roberson 2016년 10월 5일
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
Walter Roberson
Walter Roberson 2016년 10월 6일
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(:));

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by