add variable in the graph legend

69 views (last 30 days)
Kamyar Mazarei
Kamyar Mazarei on 10 May 2021
Edited: DGM on 11 May 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

Accepted Answer

DGM
DGM on 10 May 2021
Edited: DGM on 10 May 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 Comments
Kamyar Mazarei
Kamyar Mazarei on 10 May 2021
yes
but what if i wanted to put something that isnt on the graph
like in the last examples there are 3 theta but they all have graphs
what if i wanted to put a 4th value that wasnt ploted
i just want to add like CR=50 (CR changes every time and is not a constant)
i tried creating another matrix that is [ ] and make CR for that while dosnt effect my graph but didnt work
DGM
DGM on 11 May 2021
Edited: DGM on 11 May 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');

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!