add variable in the graph legend

82 ビュー (過去 30 日間)
Kamyar Mazarei
Kamyar Mazarei 2021 年 5 月 10 日
編集済み: DGM 2021 年 5 月 11 日
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

採用された回答

DGM
DGM 2021 年 5 月 10 日
編集済み: DGM 2021 年 5 月 10 日
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 件のコメント
Kamyar Mazarei
Kamyar Mazarei 2021 年 5 月 10 日
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 2021 年 5 月 11 日
編集済み: DGM 2021 年 5 月 11 日
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');

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by