Need help putting multiple values in Legends
75 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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');
0 个评论
更多回答(2 个)
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:
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
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 Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!