plotly/plotly_matlab

docs - plot - legend capture warning

danton267 opened this issue · 1 comments

load accidents
x = hwydata(:,14); %Population of states
y = hwydata(:,4); %Accidents per state

X = [ones(length(x),1) x];
b = X\y;

yCalc2 = X*b;
plot(x,yCalc2,'--');
legend('Data','Slope','Slope & Intercept','Location','best');

fig2plotly(gcf);

yields following error

Warning: Ignoring extra legend entries. 
> In legend>process_inputs (line 587)
In legend>make_legend (line 315)
In legend (line 259)
In temp (line 10) 

That error you report is caused by MATLAB but not by matlab_plotly. This is because you are not using the MATLAB legend function correctly. You are passing the parameters wrong when you invoke legend.

Since you have a single plot line, you can only pass one label in legend, which would be 'Data' or 'Slope' or 'Slope & Intersection'. But you can't go through three because the chart doesn't have three traces.

The correct use would be the following

load accidents
x = hwydata(:,14) %Population of states
y = hwydata(:,4) %Accidents per state

X = [ones(length(x),1) x];
b = X\y;

yCalc2 = X*b
plot(x,yCalc2,'--');
legend({'Data'},'Location','best');

fig2plotly(gcf);

result will be

Screen Shot 2021-10-23 at 12 32 43 PM

Data+Slope

If you want to plot both the data and the slope, you need to do the following

load accidents
x = hwydata(:,14) %Population of states
y = hwydata(:,4) %Accidents per state

X = [ones(length(x),1) x];
b = X\y;

yCalc2 = X*b
plot(x,y,'o');
hold on
plot(x,yCalc2,'--');
legend({'Data', 'Slope'},'Location','best');

fig2plotly(gcf);

Screen Shot 2021-10-23 at 12 39 58 PM