フィルターのクリア

Create a stand-alone legend for tabulated data

9 ビュー (過去 30 日間)
oran
oran 2023 年 6 月 7 日
回答済み: oran 2023 年 6 月 7 日
I'm analyizing old datasets for work. As part of this work I am grouping the data into bins as shown in the table. I am then creating PDF reports of the data, and as part of that pdf creation I need a stand alone legend to insert into its own area. Historically I had done a continuous color scale, and was easily able to produce a stand alone colorbar, but the powers who be would like discrete data groupings.
I am struggling with producing figure handles in a loop that then can be used to produce a stand alone legend. All attempts are failing. The general code is this (I've stripped all my attempts out of the the legend producer):
dat_bins=readtable("Bin_Input.xlsx");
%create RGB's for dat_bins
for j=1:length(dat_bins.bin_color)
if matches(string(dat_bins.bin_color(j)),"b")
dat_bins.rgb{j}=[0 0 1];
elseif matches(string(dat_bins.bin_color(j)),"g")
dat_bins.rgb{j}=[0 1 0];
elseif matches(string(dat_bins.bin_color(j)),"y")
dat_bins.rgb{j}=[1 1 0];
elseif matches(string(dat_bins.bin_color(j)),"o")
dat_bins.rgb{j}=[1 165/255 0];
else
dat_bins.rgb{j}=[1 0 0];
end
end
fig_leg=figure('Visible', 'on'); %create stand alone legend
axis off
for k=1:length(dat_bins.bin_start)
fig_leg(k) = plot(nan, nan, 'color', dat_bins.rgb{k});
end
legend(fig_leg, {string(dat_bins.bin_name)'})
Any help would be greatly appreciated.

採用された回答

oran
oran 2023 年 6 月 7 日
Figured it out. Partially borrowed from: here
dat_bins=readtable("Bin_Input.xlsx");
%create RGB's for dat_bins
for j=1:length(dat_bins.bin_color)
if matches(string(dat_bins.bin_color(j)),"b")
dat_bins.rgb{j}=[0 0 1];
elseif matches(string(dat_bins.bin_color(j)),"g")
dat_bins.rgb{j}=[0 1 0];
elseif matches(string(dat_bins.bin_color(j)),"y")
dat_bins.rgb{j}=[1 1 0];
elseif matches(string(dat_bins.bin_color(j)),"o")
dat_bins.rgb{j}=[1 165/255 0];
else
dat_bins.rgb{j}=[1 0 0];
end
end
fig_leg=figure('Visible', 'on'); %create stand alone legend
axis off
for k=1:length(dat_bins.bin_start)
fig_leg=scatter(nan,nan, 'MarkerFaceColor', dat_bins.rgb{k});
hold on
end
legend(dat_bins.bin_name')
% legend window
set(gcf,'Position',[0,0,1024,1024]);
legend_handle = legend('Orientation','vertical');
% This sets the figure to have the same size as the legend
set(gcf,'Position',(get(legend_handle,'Position')...
.*[0, 0, 1, 1].*get(gcf,'Position')));
set(legend_handle,'Position',[0,0,1,1]);
% Put the figure back in the middle screen area
set(gcf, 'Position', get(gcf,'Position') + [500, 400, 0, 0]);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by