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

조회 수: 23 (최근 30일)
早恵香
早恵香 2024년 4월 22일
답변: michael 2025년 12월 4일 4:45
作成したグラフで凡例が多すぎて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 = [];

답변 (2개)

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)

michael
michael 2025년 12월 4일 4:45
Placing the legend outside the frame or splitting it into multiple columns is the most common way to keep the chart proportions while still displaying the full legend slope run
If there are too many, consider creating a separate legend table instead of stuffing it into the legend.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!