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

7 vues (au cours des 30 derniers jours)
FK
FK le 8 Juil 2022
Commenté : FK le 11 Juil 2022
1つのfigure内に多数のラインプロットをする場合、凡例が多くなりすぎてしまいます。
そのため、凡例を知りたいプロット上をマウスで選択し、マウスに最も近いラインプロットの凡例を
表示させる方法が知りたいです。
下記回答集を参考にしていますが、データではなく、凡例にする方法が見つかりません。宜しくお願いします。

Réponse acceptée

交感神経優位なあかべぇ
プロット上をクリックするだけで、凡例を表示させたいのであれば、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 commentaire
FK
FK le 11 Juil 2022
交感神経優位なあかべぇ様
ご回答有難うございました。ButtonDownFcnを使用するのですね。
頂いたサンプルプログラムが大変参考になりました。
この度は有難うございました。

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!