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!