Stacked bar plot with negative value

Hello, I need to make stacked bar plot with my data to look like this:
My data set:
data1 = [0 0 3.16 25.08 46.87 57.97 39.25 28.81 10.63 0.06 0 0]
data2 = [74.00 152.68 319.99 514.05 635.73 647.61 645.32 569.51 398.48 226.13 84.88 52.08]
data3 = [628.07 497.66 426.97 285.56 220.67 184.04 212.71 239.93 318.25 451.61 545.02 626.39]
If I make it like this:
x = 1:12;
y = [data2' data1' data3'];
bar(x,y,'stacked')
It looks like this:
So I need to green part be like negative values.
y = [data2' -data1' data3'];
and graph then look like this:
How to get green part to negative side of y axe?
Thank you for advice.

 Accepted Answer

Try this:
x = 1:12;
y1 = [data2' data3'];
bar(x,y1,'stacked')
hold on
bar(x, -data1, 'g')
hold off

6 Comments

It's working fine in classic workspace, but If I want to apply it in GUI graph, then it looks like this:
Used code:
x = 1:12;
y = [data2' data3'];
bar(handles.axes1,x,y,'stacked')
hold on
bar(handles.axes1,x,-data1','g')
hold off
I would plot the figure first, since that works, then refer to the figure handle in the GUI call, rather than plotting them in the GUI call.
I do not routinely do GUI programming. I do not have enough experience with GUI programming to give you more help with the GUI part.
To create a figure handle, call the figure function before plotting to it:
x = 1:12;
y1 = [data2' data3'];
F1 = figure(1) % Define ‘figure’ & Create Handle
bar(x,y1,'stacked')
hold on
bar(x, -data1, 'g')
hold off
EDIT Added figure call and figure handle.
I dont get it, how can I plot figure into GUI axes?
I don’t know what you are doing.
You will have to experiment.
Problem solved.
x = 1:12;
y1 = [data2' data3'];
bar(handles.axes1,x,y1,'stacked');
hold(handles.axes1);
bar(handles.axes1,x, -data1, 'g');
hold off;
Thank you!
I learned something about GUIs!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!