Creation of timeline with multiple occurrences of various events?
11 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Guillaume
2016-1-21
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.
2 个评论
Guillaume
2016-1-25
You've missed the fact that the first step of my edited function is to sort the list in reverse order. I then use unique to keep the first occurence in the reverse sorted list. So, in the end unique, returns the last occurence of any event and in a reverse chronological order, regardless of the order of the input.
With regards to the x-axis. Probably, the easiest is to pass intervals of datenum to the function. Another option is to multiply the year by 12 and add to the month.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!