add variable in the graph legend
38 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Kamyar Mazarei
el 10 de Mayo de 2021
hi
i have 2 matrixes A and CC which are named 'Org' and 'Rec'
and variable CR and PRD
i want to plot A and CC on each other
and show 'Org' , 'Rec' , 'show CR value' , 'show PRD value'
all i could find on internet was this and only can show CR in title
i want CR and PRD in legends IF possible, or at least show both in title
figure(2), plot(A); hold on; plot(CC); legend('Org','Rec'); title(sprintf('CR = %f', CR))
thank you
0 comentarios
Respuesta aceptada
DGM
el 10 de Mayo de 2021
Editada: DGM
el 10 de Mayo de 2021
I'm not sure what you're asking for, so I'll try a couple possibilities. If you want to put variable names in the legend text, then you can do that just like you would with the title. Consider the generalized example:
numberofplots=3;
% preallocate vectors for handles and legend strings
h=zeros(numberofplots,1);
legendstrings=cell(numberofplots,1);
for n=1:numberofplots
x=rand(10,1); % garbage example data
y=rand(10,1);
h(n)=plot(x,y); hold on; % store this plot handle
% store this legend string
% legend string can contain special characters and calculated values
legendstrings{n}=sprintf('\\theta_{min} = %4.2f',min(abs(atan2d(y,x))));
end
legend(h,legendstrings,'location','northwest');
On the other hand, if you just want to use the legend box for numbers that are related to both of the data series instead of each data series independently, then using an annotation would be less confusing.
annstr = sprintf('Today is %s',today('datetime'));
annotation('textbox',[0.2 0.1 0.1 0.1],'string',annstr,'fitboxtotext','on');
2 comentarios
DGM
el 11 de Mayo de 2021
Editada: DGM
el 11 de Mayo de 2021
The purpose of the legend is to identify the objects in the figure. That's why it doesn't make sense to put parameters that affect all plots in the legend. You could do it, but it's confusing.
h(n+1) = plot(NaN,NaN,'color','none');
legendstrings{n+1} = sprintf('\\Sigma_{22} = %4.2f',pi^2);
legend(h,legendstrings,'location','northwest');
And if you want the text centered so that readers aren't confused by the suggestion that an invisible plot exists somewhere, then I don't know of a way to do it with a legend, because that's not what legends are meant to do. You can probably do some sort of kludge like this:
% add more padding room to the legend
h(n+1:n+2) = plot(NaN,NaN,'color','none');
legendstrings(n+1:n+2) = {'',''};
legend(h,legendstrings,'location','northwest');
% overlay an annotation on top of the legend
% you'll have to mess with the position and font
annstr = sprintf('\\Sigma_{22} = %4.2f',pi^2);
annotation('textbox',[0.185 0.705 0.1 0.1],'string',annstr, ...
'fitboxtotext','on','fontsize',9,'edgecolor','none');
Más respuestas (0)
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!