Help organizing legend for bar graph
1 view (last 30 days)
Show older comments
So I had a very helpful response in a previous question for creating a legend organizing some data and I've tried manipulating it for another graph but I'm having issues making it work when there are a few more bars
So here's the code for the 1st graph
NN = size(patch,1);
t = 1:NN;
figure
hold on
% Shading based on 'env' value
colors = [ ...
0.898 1 0.337 ; ...
0.561 1 0.643 ; ...
0.561, 0.969, 1 ; ...
0.902 0.561 1 ; ...
];
NC = 4;
hreg = cell(1,NC);
emptyhreg = false(1,NC);
for ii = 1:NC
if ~any(env == ii)
emptyhreg(ii) = true;
continue
end
hreg{ii} = xregion(t(env==ii)+0.5*[-1;1],'FaceColor',colors(ii,:),'FaceAlpha',1);
end
% Count choices for patch 1 and patch 2, ignoring NaN values
count_patch1 = sum(patch == 1, 2);
count_patch2 = sum(patch == 2, 2);
hbar = bar(t,[count_patch1, count_patch2],'EdgeColor','none');
hbar(1).FaceColor = 'r';
hbar(2).FaceColor = 'b';
xlim('tight')
xticks(t)
xlabel('timestep')
ylabel('# of individuals')
h = cellfun(@(x)x(1),hreg(~emptyhreg));
s = ["high","low"];
idx = dec2bin(circshift(NC-1:-1:0,2),2)-'0'+1;
str = join("patch "+[1 2]+" "+s(idx),"/");
str = str(~emptyhreg);
h = [h hbar];
str = [str; "went to patch "+[1;2]];
legend(h,str,'Location','NorthOutside')
Now I want to do something similar with a bar graph that has 4 bars. I tried just changing the second portion of the str to include those two extra bars
hbar = bar(countp,'EdgeColor','none');
hbar(1).FaceColor = 'r';
hbar(2).FaceColor = 'b';
hbar(3).FaceColor = 'k';
hbar(4).FaceColor = 'w';
s = ["high","low"];
idx = dec2bin(circshift(NC-1:-1:0,2),2)-'0'+1;
str = join("patch "+[1 2]+" "+s(idx),"/");
str = str(~emptyhreg);
h = [h hbar];
str = [str;"went to patch 1 and found food","went to patch 1 and didn't","went to patch 2 and found food","went to patch 2 and didn't"];
legend(h,str,'Location','NorthOutside')
But I got this error
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in forwardsim_socialinfo2 (line 1019)
str = [str; "went to patch 1 and found food","went to patch 1 and didn't","went to patch 2 and found food","went to patch 2 and didn't"];
How do I add in these extra bar labels?
Hopefully this is enough, but let me know if more context/code is needed.
0 Comments
Accepted Answer
Voss
on 21 Oct 2024
str is a column vector, so use semicolons (instead of commas) to append new elements
str = [str;"went to patch 1 and found food";"went to patch 1 and didn't";"went to patch 2 and found food";"went to patch 2 and didn't"];
% ^ ^ ^
A similar example to illustrate the problem, using a numeric vector:
x = [1; 2; 3] % a column vector
x = [x; 4; 5] % append a 4 and a 5 to x
x = [x; 4, 5] % error
If that doesn't work or there are other problems, please share enough code and variables that we can run it (e.g., countp is not defined in the code we can see).
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!