How to create legend for two error bar plots
이전 댓글 표시
The only problem with my code is, that the legend line gives error messages and doesn't show up in the graph. I want a legend with 4 elements (The data of graph 1, the fit of graph 1, the data of graph 2, the fit of graph 2). I don't want the error bars to show in the legend. My code:
function [fitresult, gof, goft] = Linearized_plot_both(inverse_square_radius,time,er,inverse_square_radiust,timet,ert)
%%Fit: 'Linearized Fit'.
[xData, yData] = prepareCurveData( inverse_square_radius, time );
[xDatat, yDatat] = prepareCurveData( inverse_square_radiust, timet );
% Set up fittype and options.
ft = fittype( 'poly1' );
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft );
[fitresultt, goft] = fit( xDatat, yDatat, ft );
% Plot fit with data.
figure( 'Name', 'Comparing Linearized Fit' );
x= linspace(0,11e5);
y=(fitresult.p1).*x + (fitresult.p2);
h=plot(xData,yData,'.k',x,y,'b');
h(1).MarkerSize = 12;
h(2).LineWidth = 1;
hold on;
axis([0 10.5e5 0 41.5]);
grid on;
hEB=errorbar(xData,yData,er,'.k', 'MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',12);
xt= linspace(0,11e5);
yt=(fitresultt.p1).*x + (fitresultt.p2);
ht=plot(xDatat,yDatat,'.k',xt,yt,'r');
ht(1).MarkerSize = 12;
ht(2).LineWidth = 1;
hold on;
hEBt=errorbar(xDatat,yDatat,ert,'.k', 'MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',12);
legend([h,ht],'time vs. inverse_square_radius (Data)','Linearized Fit (Data)','time vs. inverse_square_radius (Theory)','Linearized Fit (Theory)');
% Label axes
xlabel inverse_square_radius
ylabel time
The produced graph:

Way I want my legend to look (just for both graphs):

%
댓글 수: 5
dpb
2018년 7월 11일
I don't see anything obvious but can't run your code nor duplicate the intended effect without a fair effort. How about posting a simple test case with included data that illustrates the issue; perhaps even in doing that you'll uncover the problem...
Nicholas Pfaff
2018년 7월 11일
편집: dpb
2018년 7월 11일
Nicholas Pfaff
2018년 7월 11일
dpb
2018년 7월 11일
Please attach a mat or text file with the variables. Help us help you...
Nicholas Pfaff
2018년 7월 12일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Legend에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!