マウスで選択した位置​に近い凡例を表示させる方法

1つのfigure内に多数のラインプロットをする場合、凡例が多くなりすぎてしまいます。
そのため、凡例を知りたいプロット上をマウスで選択し、マウスに最も近いラインプロットの凡例を
表示させる方法が知りたいです。
下記回答集を参考にしていますが、データではなく、凡例にする方法が見つかりません。宜しくお願いします。

 Respuesta aceptada

交感神経優位なあかべぇ
交感神経優位なあかべぇ el 9 de Jul. de 2022

1 voto

プロット上をクリックするだけで、凡例を表示させたいのであれば、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

1 comentario

FK
FK el 11 de Jul. de 2022
交感神経優位なあかべぇ様
ご回答有難うございました。ButtonDownFcnを使用するのですね。
頂いたサンプルプログラムが大変参考になりました。
この度は有難うございました。

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2022a

Etiquetas

Preguntada:

FK
el 8 de Jul. de 2022

Comentada:

FK
el 11 de Jul. de 2022

Community Treasure Hunt

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

Start Hunting!