Question

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:

http://www.epa.gov/ncer/progress/images/R827933C033_02_003.gif

Was it helpful?

Solution

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')

graph

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

OTHER TIPS

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

bar2.gif

bar_ex2.gif

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top