Im creating a nested treemap, but the output is giving different colors for the legend and treemap

2 views (last 30 days)
close all;
clear all;
clc;
url = 'https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/17192/versions/4/download/zip/treemap.zip';
savename = 'treemap.zip';
filestr = urlwrite(url, savename);
filenames = unzip(savename)
filenames = 1×13 cell array
Columns 1 through 7 {'treemap/plotRect…'} {'treemap/treemap.m'} {'treemap/html/tre…'} {'treemap/html/tre…'} {'treemap/html/tre…'} {'treemap/html/tre…'} {'treemap/html/tre…'} Columns 8 through 13 {'treemap/html/tre…'} {'treemap/html/tre…'} {'treemap/outline.m'} {'treemap/treemap_…'} {'treemap/Contents…'} {'license.txt'}
addpath( fileparts(filenames{1}) )
%Define a color palette
colorPalette = [0.9290 0.6940 0.1250; % yellow
0 0.4470 0.7410; % blue
0.8500 0.3250 0.0980; % orange
0.4940 0.1840 0.5560; % purple
0.4660 0.6740 0.1880];% green
%Import the data to matlab
data = {'Brighton & Hove','Books',610;'Brighton & Hove','Electronics',855;'Brighton & Hove','Hardware',998;'Brighton & Hove','Software',2020;'Chichester','Home & Garden',396;'Chichester','Health & Beauty',580;'Chichester','Sports ',613;'Chichester','Software',885;'Chichester','Electronics',891;'Chichester','DIY',1449;'Portsmouth','Books',268;'Portsmouth','Home & Garden ',390;'Portsmouth','Electronics',690;'Portsmouth','DIY',834;'Portsmouth','Sports',1008;'Portsmouth','Clothes',1016;'Portsmouth','Toys & Children',1201;'Southampton','DIY',169;'Southampton','Hardware',604;'Southampton','Electronics',757;'Southampton','Sports',1567;'Winchester','Hardware',524;'Winchester','Sports',541;'Winchester','Books',806;'Winchester','Software',991;'Winchester','Toys & Children',1079;'Winchester','Electronics',1661};
categories=unique(data(:,1));
numUniqueCategories=length(categories);
branches={'Brighton & Hove','Chichester','Portsmouth','Southampton','Winchester'};
numInstances = size(data, 1);
disp(['Number of instances: ' num2str(numInstances)]);
Number of instances: 27
array1 = zeros(numInstances,5);
%disp(categories);
disp(numUniqueCategories);
5
%for loop to store the values in array
for i =1:numUniqueCategories
category_items = strcmp(data(:,1),categories{i});
array1(category_items,i)=[data{category_items,3}];
% for j=1:numInstances
% if strcmpi(strtrim(branches{i}), strtrim(data{j, 1}))
% array1(j,i)=cell2mat(data(j,3));
% %disp([num2str(array1(j, i))]);
% end
% end
end
%disp(array1);
values = sum(array1,1);
%create the outertree map
outertree = treemap(values);
plotRectangles(outertree);
title('Sales by Branch');
% % Extract the unique entries in the second column for each main category
legend_labels = categories;
% Initialize legend colors
legend_colors = zeros(numUniqueCategories, 3);
disp(size(legend_colors));
5 3
%for loop to create the inner tree
for j=1:numUniqueCategories
category_items = strcmp(data(:,1), categories{j});
%create inner treemap
innertree = treemap(array1(category_items, j), outertree(3, j), outertree(4, j));
innertree(1,:)=innertree(1,:)+outertree(1,j);
innertree(2,:)=innertree(2,:)+outertree(2,j);
% Get the color for the current category
categoryColor = colorPalette(mod(j-1, size(colorPalette, 1)) + 1, :);
% Get the color for the current category
%categoryColor = categoryColorMap(char(branches(j)));
plotRectangles(innertree, data(category_items,2), repmat(categoryColor, size(innertree, 2), 1));
outline(innertree);
% Store colors for legend
legend_colors(j,:) = categoryColor;
end
% Convert color values to strings in the correct format
legend_colors_norm = legend_colors / 255; % Normalize RGB values
% Convert color values to strings in the correct format
legend_colors_str = cell(numUniqueCategories, 1);
for i = 1:numUniqueCategories
legend_colors_str{i} = sprintf('[%.3f, %.3f, %.3f]', legend_colors(i, :));
end
%plot the legend for the outer rectangle only
legend(legend_labels, 'color',legend_colors_norm,'Location', 'westoutside');
Error using legend
Error setting property 'Color' of class 'Legend':
Invalid RGB triplet. Specify a three-element vector of values between 0 and 1.
legend('boxoff'); % Turn off legend box
%labels = categories(:);
outline(outertree);
%axis([-0.02 1.01 -0.01 1.01])

Answers (1)

Walter Roberson
Walter Roberson on 13 Mar 2024
legend() does not permit passing in an N x 3 array of legend colors.
legend() does not permit setting Color at all.
legend() permits setting TextColor -- but only as a single RGB triple that applies to all of the legends.
You need to record the output of legend(). Then you need to access the PlotChildren property, which will be an array of line() handles. You can set the Color property of the line handles (one RGB triple for each line... unless you know the magic incantation using cell arrays.)

Community Treasure Hunt

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

Start Hunting!