How to create legend for two error bar plots

8 次查看(过去 30 天)
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
dpb 2018-7-11
Please attach a mat or text file with the variables. Help us help you...

请先登录,再进行评论。

采纳的回答

dpb
dpb 2018-7-12
编辑:dpb 2018-7-12
h and ht are column vectors of handles and you've concatenated them into a 2x2 array instead of a vector...it's trying to put those together inside legend that's failing.
Use ; instead of , --
legend([h;ht],'time vs. inverse_square_radius (Data)','Linearized Fit (Data)','time vs. inverse_square_radius (Theory)','Linearized Fit (Theory)');
You'll probably cover up most of the plot area with the length of the text, but that's another problem... :)
  2 个评论
dpb
dpb 2018-7-12
编辑:dpb 2018-7-12
legend documents subset handles input must be a vector but seems rude for it to fail unceremoniously; it couldn't cost anything for it to use (:) on the argument inside to ensure is a vector or, failing that, inform of the what the error is during input check...
I didn't catch it just reading the code; set breakpoint in editor to see what you ended up with (hence the request for sample or data to run yours; almost certainly if had tried to emulate I'd have missed the crucial mistake and not found the problem).
ADDENDUM
I did submit enhancement SRQ to either add informative error or, it turns out that one can modify the failing line by the (:) and it will then accept the array of handles. I made the patch and recompiled the p-code and with that the existing code runs to completion with the expected result. No big deal but even an informative error would be better than the crash inside TMW-supplied function with no guidance as to why.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by