How to Create Custom Legend for barh Stacked Plot

4 views (last 30 days)
I am creating a 3-row stacked barh plot that shows the historical data of a project. Each row is generated as a single barh plot and represents a different phase in a project. I have concurrent matricies that define the days and state (green, yellow, red, or white) for the given dates. The phases are plotted in relative days to the epoch (start of the project). Each phase may have several different values, likely repeating themselves throughout the history. Also, the historical data may not be complete (first entry not the initial project date) Here's some potential data:
Planning: day 0-20: green day 21-40: red day 41-80: yellow day 81-now: white
There is similar data for the Funding and Execution of each project. Any of these phases can start in any state
This is not the normal, nor probably the intended use of bar/barh. That being said, the goal is to show a historical timeline of the information for each phase.
I am able to generate the stacked plots, but I am having trouble generating the appropriate legend. This is the shortened version of the code I'm using (let me know if you need more detail).
% generate plots, generate size of plot in first call
p_plan = barh(1:3, plan_dates zeros(size(plan_dates,1),1) zeros(size(plan_dates,1),1)]', 'stack');
p_fund = barh(2:3, [fund_dates zeros(size(fund_dates,1),1)]', 'stack');
p_exec = barh(3:4, [exec_dates zeros(size(exec_dates,1),1)]', 'stack');
% loop over each plan/fund/exec value and change color appropriately
% this is for the planning phase only, similar code for fund/exec
for i = 1:size(plan_dates,1)
if (strcmpi(plan_val{i}, 'w'))
set(p_plan(i), 'facecolor', 'none');
else
set(p_plan(i), 'facecolor', lower(plan_val{i}));
end
end
If I try to auto-generate a legend I will get the entries as they were put into the corresponding "plan_val" (or fund/exec) vectors. If the first 4 entries happen to be RGBW then I don't have a problem, but that is not a robust method to solve the problem. It also is a broad assumption that will not normally be correct.
How can I plot put this into a legend appropriately? Is there anyway to group it appropriately? Then use groups for the legend entry?
Thanks in advance, Peter
  1 Comment
Kelly Kearney
Kelly Kearney on 19 Aug 2013
It would help if you could create small example versions of the data needed to run this code ( plan_dates, fund_dates, exec_dates, etc).

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!