Creation of timeline with multiple occurrences of various events?

조회 수: 11 (최근 30일)
Aundrea Dolan
Aundrea Dolan 2016년 1월 21일
댓글: Guillaume 2016년 1월 25일
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.

채택된 답변

Guillaume
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
Aundrea Dolan
Aundrea Dolan 2016년 1월 22일
Guillaume,
Thank you so much for the timely response. Your help is very much appreciated, and I am very excited to finally have a timeline that works for what I need. Just a couple quick questions- I tried to mess around with some list values to fully understand the function unique and I'm not sure I quite comprehend it completely. According to help in matlab, [C,IA,IC] = unique(A,'stable') returns the values of C in the same order that they appear in A. So- to me, I take this as the placement of the values within the matrix list will change where they appear on the graph. But after playing around with this, it has not. Would you be willing to clarify this function?
Also- my second question is in regard to changing the x-axis values from years to months. Would the best way to do this be the following: » x=datenum(1975:1999, 1, 1); » y=round(100*rand(size(x))); » plot(x,y) » datetick('x','mmmyy')?
If so, would I format the ranges in the list matrix to be in the ('mmyy') format?
Thanks again very much for your help!
Guillaume
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 CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by