マウスで選択した位置に近い凡例を表示させる方法
7 views (last 30 days)
Show older comments
1つのfigure内に多数のラインプロットをする場合、凡例が多くなりすぎてしまいます。
そのため、凡例を知りたいプロット上をマウスで選択し、マウスに最も近いラインプロットの凡例を
表示させる方法が知りたいです。
下記回答集を参考にしていますが、データではなく、凡例にする方法が見つかりません。宜しくお願いします。
0 Comments
Accepted Answer
交感神経優位なあかべぇ
on 9 Jul 2022
プロット上をクリックするだけで、凡例を表示させたいのであれば、plotのプロパティであるButtonDownFcnを使用して、凡例表示の処理を実行させるのが容易だと思います。
ButtonDownFcnを利用した凡例表示の例を下記に記載しました。(.mファイルに貼り付けて.mファイル上から実行すれば動くはずです。)
% グラフ表示
x = 0:0.1:2*pi;
y1 = sin(x);
y2 = cos(x);
hp = plot(x,y1,x,y2,'ButtonDownFcn',@(hplot, event) DispLegend(hplot));
hp(1).DisplayName = 'sin';
hp(2).DisplayName = 'cos';
legend();
function DispLegend(hplot)
% 凡例のテキストを削除
htext = findobj(hplot.Parent, 'Type', 'text');
delete(htext);
% マウスの現在値の取得
Cp = hplot.Parent.CurrentPoint;
Xp = Cp(2,1);
Yp = Cp(2,2);
% 凡例テキストの挿入
text(Xp,Yp,['\leftarrow', hplot.DisplayName],'Clipping','off');
end
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!