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

조회 수: 7 (최근 30일)
Kato Yusuke
Kato Yusuke 2023년 12월 21일
댓글: Kato Yusuke 2023년 12월 25일
太い線で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
Atsushi Ueno
Atsushi Ueno 2023년 12월 21일
質問に答えていませんが、目的から考えて点線の間隔が短くなれば事足りるのではないでしょうか?
p = plot(1:0.1:10, sin(1:0.1:10), ':', 'Linewidth',10);
Kato Yusuke
Kato Yusuke 2023년 12월 22일
ありがとうございます.私の言葉足らずで申し訳ありませんでしたが,
やりたいこととしては,':', '--', '-.'のそれぞれで指定した複数のプロットを,legendで区別したいというモチベーションでした.

댓글을 달려면 로그인하십시오.

채택된 답변

Atsushi Ueno
Atsushi Ueno 2023년 12월 21일
편집: Atsushi Ueno 2023년 12월 24일
  • 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
Kato Yusuke
Kato Yusuke 2023년 12월 25일
ご親切にありがとうございました!
教えていただいた通り,f.Positionとax.Positionを調整することで,legendをeastoutsideにしたままで望みの図を作ることができました!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!