legend内のpl​otの長さを長くして​,さらに文章がbox​からはみ出ないように​する.

6 views (last 30 days)
Kato Yusuke
Kato Yusuke on 21 Dec 2023
Commented: Kato Yusuke on 25 Dec 2023
太い線でplotしてlegendを表示すると,legend内に表示される線が短く,破線などが見分けにくい問題があります.
それを解決するため以下のようにコードを変更しましたが,
x = 1:0.1:10;
y = sin(x);
plot(x, y, '--', 'Linewidth',10);
[hh,icons,plots,txt] = legend('the graph of sin(x)','Location','eastoutside');
icons(1).Position(1) = 0.72;
icons(2).XData = [0 0.7];
今度はlegendのtextが右にはみ出てしまいます.
解決すべく,hh.Positionを変更したりもしたのですが,するとhh.Locationがnoneになってしまい,今度はlegendが図に重なってしまいます.
legend内のplotの長さを長くして,さらに文章がboxからはみ出ないようにするにはどうすればよいのでしょうか?
  2 Comments
Atsushi Ueno
Atsushi Ueno on 21 Dec 2023
質問に答えていませんが、目的から考えて点線の間隔が短くなれば事足りるのではないでしょうか?
p = plot(1:0.1:10, sin(1:0.1:10), ':', 'Linewidth',10);
Kato Yusuke
Kato Yusuke on 22 Dec 2023
ありがとうございます.私の言葉足らずで申し訳ありませんでしたが,
やりたいこととしては,':', '--', '-.'のそれぞれで指定した複数のプロットを,legendで区別したいというモチベーションでした.

Sign in to comment.

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 21 Dec 2023
Edited: Atsushi Ueno on 24 Dec 2023
  • gca 関数でグラフの軸オブジェクトを取得し、グラフの幅を変更します
  • legend 関数の出力である Legend オブジェクト を取得し、legend の幅を変更します
  • figure 関数の出力である Figure オブジェクト を取得し、figure の幅を変更します
f = figure;
x = 1:0.1:10;
y = sin(x);
plot(x, y, '--', 'Linewidth',10);
[hh,icons,plots,txt] = legend('the graph of sin(x)','Location','eastoutside');
icons(1).Position(1) = 0.72;
icons(2).XData = [0 0.7];
f.Position(3) = 1500; % figureの幅
ax = gca;
ax.Position(3) = 0.3; % グラフ軸の幅
hh.Position(3) = 0.4; % legendの幅
  1 Comment
Kato Yusuke
Kato Yusuke on 25 Dec 2023
ご親切にありがとうございました!
教えていただいた通り,f.Positionとax.Positionを調整することで,legendをeastoutsideにしたままで望みの図を作ることができました!

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!