views:

81

answers:

1

At the end of some data processing in MATLAB, I want to create a plot which shows colored timeline bars for a series of data. I have a number of processes which each go through similar steps, and start and stop and different at different times. Ideally it'd end up looking something like this (forgive the ASCII art):

   |   ###***$$$$$$$$$$         Process 1
   |        ###***$$$$$$$       Process 2
   |           ###$$$$$         Process 3
   |             *******$$$$$$  Process 4
   +------------------------------------------
                    Time

Where # * and $ are standing in for solid, adjacent blocks of differing colors (one color per step the processes go through; note some are optional).

The labels could be elsewhere, but next to each line is good.

I have hacked together a solution using rectangle and text, but it seems like this might be an existing type of plot within MATLAB that I just haven't found yet. Do you know of one?

+5  A: 

Use barh. Set the first column as your initial process time

data_with_init_time = [ 
       1, 10, 5, 3 ;
       3, 10, 3, 9 ;
       7, 10, 4, 8 ;
       12,10, 2, 2 ];

h = barh(data_with_init_time, 'stack');
set(h(1), 'facecolor', 'none', 'EdgeColor', 'none'); % disable the color of the first column (init time)
set(gca, 'YTickLabel', {'proc 1', 'proc 2', 'proc 3', 'proc 4'} ); % change the y axis tick to your name of the process
axis ij; % Put the first row at top
YYC
+1: Very nice, although I might make one small modification. The background color of the axes is usually white by default, but just in case it isn't you can do this instead: `axesColor = get(gca,'Color'); set(h(1),'FaceColor',axesColor,'EdgeColor',axesColor);`
gnovice
@gnovice: you can simply set the colors as `'none'`
Amro
@Amro: Ah yes. I totally forgot that would work on bar graphs. That's a better alternative.
gnovice
Thanks for the tips guys. Your comments are integrated to the answer. And I just found that 'axis ij' may present the data in a preferred order.
YYC
@YYC: if you want to match the OP's ASCII art, you could also add `set(gca,'YAxisLocation','right')`
Amro
Also just occurred to me, you can use `set(h(1), 'Visible','off')` instead of 'none' colors, especially if you want the first series to match the first color in the current colormap according to `ColorOrder` property (it would skip the first one otherwise)
Amro