MATLAB: Creation of timeline with multiple occurrences of various events

timeline

I am working to create a timeline containing numerous events that occur multiple times over the course of about 10 years (hurricanes, beach nourishment projects, flooding, etc.). I have tried various forms of horizontal stacked bar graphs and have come to find the attached script created by Iari-Gabriel Marino. The setup seems to be the closest that I can find to what I need to graph. The issue I am having is figuring out how to code an event occurring multiple times- as his script depicts a one-time lifeline of historical figures- and how to do so on monthly intervals, rather than yearly.
Does anyone know the best way to accomplish this? I have tried creating multiple scripts, adjusting Iari-Gabriel Marinos' as well as creating my own, and can not seem to figure out how to do so.
Thank you.

Best Answer

This is a variation on that timeline code that copes with duplicate events:
function timeline2(list)
%make sure the list is ordered in reverse chronological order (to help identify the last label of a row)
[~, order] = sortrows(vertcat(list{:, 2}), [-1 2]);
list = list(order, :);
%identify unique label and generate vertical positioning
[labels, idx, ylabels] = unique(list(:, 1), 'stable');
ypatches = max(ylabels) + 1 - [ylabels, ylabels, ylabels-1, ylabels-1]';
ylabels = max(ylabels) + 1 - ylabels(idx);
%generate horizonal positioning
xpatches = [vertcat(list{:, 2}), fliplr(vertcat(list{:, 2}))]';
xlabels = xpatches(2, idx);
%plot
figure;
colour = parula(size(list, 1));
patch(xpatches, ypatches, reshape(colour, 1, [], 3));
text(xlabels+5, ylabels+0.5, labels, 'fontsize', 10);
xlabel('Time (years)');
grid on
end
An example:
timeline2({'A', [-10 20]
'B', [0 50]
'C', [-100 100]
'A', [90 100]
'D', [30 50]
'D', [-20 20]}
The only difference from the original code is in the generation of the y position of the patches, whereas the original code just use 1:numberoflines I use the 3rd return value of unique, and in the labeling where I need to be a bit more clever to only have one label per row. Again unique comes to the rescue.