Filter löschen
Filter löschen

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

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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!