マウスで選択した位置に近い凡例を表示させる方法
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
1つのfigure内に多数のラインプロットをする場合、凡例が多くなりすぎてしまいます。
そのため、凡例を知りたいプロット上をマウスで選択し、マウスに最も近いラインプロットの凡例を
表示させる方法が知りたいです。
下記回答集を参考にしていますが、データではなく、凡例にする方法が見つかりません。宜しくお願いします。
0 comentarios
Respuesta aceptada
交感神経優位なあかべぇ
el 9 de Jul. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!