tags:

views:

67

answers:

2

In MATLAB, is it possible to create a single graph of two related data sources with the first source plotted along the bottom of the x-axis and the 2nd source plotted down from the top of the x-axis? I can't find anywhere in the MATLAB documentation where this is done.

The final graph I need is in a form like this:

A: 

Check out the documentation on the bar function. You can use it to create graphs like the following:

alt text

alt text

Jacob
Why the -1? It's not a full solution, but it should get the OP on the right track.
Doresoom
+5  A: 

I tried to reproduce your graph as close as possible. Here's what I ended up with:

t = linspace(datenum('01-19-2002'), datenum('06-27-2002'), 12);
x1 = randi(40, [12 1]);
x2 = randi(40, [12 1]);
z = 100-x1-x2;

hAxR = axes();
hAxL = axes();

h = bar(t, [x1 z x2], 'stacked');

set(h(1),'facecolor','y')
set(h(2),'facecolor',[.8 .8 .8])
set(h(3),'facecolor','r')
legend(h, {'s1' 's2' 's3'}, ...
   'orientation','horizontal', 'location','northoutside')

set(hAxL, 'xtick',t, 'xlim',[datenum('01-01-2002') datenum('07-15-2002')])
datetick(hAxL, 'x',2,'keepticks','keeplimits')
xticklabel_rotate

ylabel(hAxL, 'label1')
ylabel(hAxR, 'label2')

set(hAxR, 'position',get(hAxL,'position'), 'color','none', 'xtick',[], ...
    'ydir','reverse', 'yaxislocation','right', 'ylim',get(hAxL,'ylim'))
set(hAxL, 'YGrid','on')

alt text

I am using XTICKLABEL_ROTATE to rotate the labels on the x-axis

Amro
Gah! Beat me by 5 minutes! Note that with http://www.mathworks.com/matlabcentral/fileexchange/18826-hatch-fill-patterns-plus-plus, you can get cross-hatched fills for bars. Also, you may want to replace `label1` and `label2` with the real thing. +1 for otherwise perfect solution.
Jonas
@Jonas: thanks, I was just looking up the submission on FEX for hatched patterns as colors
Amro
@Amro: I just noticed that you only plotted one bar series. Given the title of the question, I thought the OP wanted one stacked bar series with two colors, and one bar series with crosshatch pattern. Thus, you'd need to run `hBL=bar(hAxL,data1);hBR=bar(hAxR,data2)`.
Jonas
@Jonas: the thing is if you look at the legend in the original plot, you will see three separate data series.. so in the sample graph above, I simply generated the data so that the middle one fills up in-between to make the sum 100. I guess the OP should give a description of how the data actually looks like (this was simply an attempt to visually replicate the plot [with the exception of the labels, I'm too lazy to type all that!])
Amro
@Amro: Fantastic. FYI. My example graph was an image I found using Google, It's not my data. I just wanted to find something close to what I needed becuase my problem was hard to explain otherwise.I didn't expect anyone to replicate the example so thoroughly. If only I could up-vote this answer twice. :)
AaronThomson