Data Set with multiple markers, how do I get the legend to display this?

3 次查看(过去 30 天)
I have a data set I've been playing with that has low energy observations and high energy observations. I was wanting to display the low energy data with the '*', and the high energy data with the '^'.
Here's the code I've got:
function [fitresult, gof] = createFit1(xdata1, ydata1)
HeliumData_Voyager
%CREATEFIT1(XDATA1,YDATA1)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : xdata1
% Y Output: ydata1
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 11-Aug-2015 22:20:58
%%Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( xdata1, ydata1 );
% Set up fittype and options.
ft = fittype( '1/(a*(x)^b+c*(x)^d+e*(x)^f)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Levenberg-Marquardt';
opts.Display = 'Off';
opts.Robust = 'Bisquare';
opts.StartPoint = [0.5 0.165607687252584 0.777847540631553 0.913375856139019 0.63235924622541 0.0975404049994095];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h1 = plot(fitresult, xData(1:14), yData(1:14),'*');
hold on
h2 = plot(fitresult, xData(15:27), yData(15:27), '^');
hold off
hold on
h3 = plot(fitresult);
set(gca,'XScale','log')
set(gca,'YScale','log')
axis([.001,100,.00001,20])
legend( h1, 'Voyager Data', 'Line of Best Fit', 'Location', 'SouthWest' );
% Label axes
title('Low and High Energy Cosmic Ray Spectra for Helium')
xlabel('Energy (GeV)')
ylabel('Flux particle m^{-2}s^{-1}sr^{-1}MeV')
grid on
You probably don't need the data set for this question, but for good measure:
xdata1 = [0.0021859258,0.0035637526,0.0049688127,0.007943902,0.012700333,...
0.020705573,0.032462176,0.04615445,0.059510704,0.098936856,0.19612777,...
0.2899725,0.381267,0.5013045,12.45768,12.954449,15.1476755,14.378171,...
17.712223,19.658836,20.982672,22.395653,30.620895,24.856985,27.95077,...
33.1117,35.805107];
ydata1 = [2.17084,1.5341274,1.6444274,2.4091163,2.025231,2.673546,2.7679887,...
2.5823257,2.3269181,1.8893886,1.3352263,0.8803091,0.5803841,0.42464474,...
4.8200923E-4,3.8242337E-4,2.194412E-4,2.9647112E-4,1.8235153E-4,...
1.3813264E-4,1.0463649E-4,8.496173E-5,4.1460968E-5,6.144804E-5,...
4.875252E-5,3.2894895E-5,2.5501544E-5];
And I'll attach the graph, where you can see I've already got the stars and triangles. However, I can't get the legend to show both types of markers. I want the stars to say Voyager Data, and the triangles to say Pamela Data.
I've tried the following:
legend( [h1,h2], 'Voyager Data', 'Pamela Data', 'Line of Best Fit', 'Location', 'SouthWest' );
However, that didn't work. How can I accomplish this?

采纳的回答

Jon
Jon 2015-8-12
Why are you including 'fitresult' in your plotting commands? See if this code does what you want:
function [fitresult, gof] = createFit1(xdata1, ydata1)
% HeliumData_Voyager
%CREATEFIT1(XDATA1,YDATA1)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : xdata1
% Y Output: ydata1
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 11-Aug-2015 22:20:58
%%Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( xdata1, ydata1 );
% Set up fittype and options.
ft = fittype( '1/(a*(x)^b+c*(x)^d+e*(x)^f)', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Levenberg-Marquardt';
opts.Display = 'Off';
opts.Robust = 'Bisquare';
opts.StartPoint = [0.5 0.165607687252584 0.777847540631553 0.913375856139019 0.63235924622541 0.0975404049994095];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' )
hold on
plot( xData(1:14), yData(1:14), '*');
plot( xData(15:27), yData(15:27), '^');
plot(fitresult);
set(gca,'XScale','log')
set(gca,'YScale','log')
axis([.001,100,.00001,20])
legend('Voyager Data', 'Pamela Data', 'Line of Best Feet', 'Location', 'SouthWest' );
% Label axes
title('Low and High Energy Cosmic Ray Spectra for Helium')
xlabel('Energy (GeV)')
ylabel('Flux particle m^{-2}s^{-1}sr^{-1}MeV')
grid on
  2 个评论
CAM
CAM 2015-8-12
That did it! Thanks!
To answer your first, this is simply a lot of iterations of code, so some things I haven't "cleaned" out just yet since I've been working on this code for some time now.
Thanks for the help!
Jon
Jon 2015-8-12
It was their inclusion that prevented legend from functioning the way you expected. Basically, extra axes were being plotted in each 'plot' command.

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by