フィルターのクリア

グラフの凡例を図面か​らはみ出さないように​したいのですが

13 ビュー (過去 30 日間)
早恵香
早恵香 2024 年 4 月 22 日
回答済み: Akira Agata 2024 年 4 月 22 日
作成したグラフで凡例が多すぎてfigureの枠に収まり切りません。
枠を大きくすることで対策しましたが、そうすると文字が小さめになってしまいます。
文字を大きくするとまたはみ出します。
グラフの図面の比率は変えずに凡例を収める方法はないのでしょうか。
%% 図面の枠を生成
f1 = figure;
ax1 = gca;
tfontsizeorg1 = ax1.FontSize;
tfontscale1 = 1.1 * 1.5;
leafcount = 50;
%% 図面の大きさを調整
if leafcount > 15
f1_scale3 = 28 * leafcount * 4 / 3;
f1_scale4 = 28 * leafcount * 2 / 3;
else
f1_scale3 = 560;
f1_scale4 = 280;
end
if f1_scale3 > 1920
f1_scale3 = 1920;
f1_scale4 = f2_scale3 / 2;
end
legcol = fix((leafcount - 1) / 15) + 1;
f1.Position(3) = f1_scale3;
f1.Position(4) = f1_scale4;
tfontsize1 = tfontscale1 * tfontsizeorg1 / 560 * f1_scale3;
ZXAxis1 = max(tfontsize1 * 9 / 11 /2, 9);
Zlegend1 = ZXAxis1 * 9 / 10;
pd = makedist('Normal');
valid_rows = 300;
r = random(pd,[valid_rows,1]);
% 範囲内の整数の乱数行列を生成
Zleaf = randi([1, leafcount], valid_rows, 1);
Xl = [r ones(sum(valid_rows), 1)];
orgplot = gscatter(Xl(:,1),Xl(:,2),Zleaf); % 読み込んだデータをグラフ化して確認
OrgCol = vertcat(orgplot.Color);
[OrgCsize, ~, iOrgdx] = unique(OrgCol, 'rows');
usedata_name="sample";
legend('Location','eastoutside')
title(usedata_name,'FontSize',tfontsize1); % グラフのタイトル(データ名)
ax1.XAxis.FontSize = ZXAxis1;
ax1.Legend.FontSize = Zlegend1;
ax1.YTick = [];

回答 (1 件)

Akira Agata
Akira Agata 2024 年 4 月 22 日
legend の NumColumnsプロパティを設定するのが良いかと思います。
ご参考までに、下記にその一例を示します。
figure
gscatter(rand(100, 1), ones(100, 1), randi(30, 100, 1))
legend(Location = "eastoutside", NumColumns = 2)

カテゴリ

Help Center および File Exchange2 次元および 3 次元プロット についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!