1
votes

I need to make stacked bar plot with my data to look like this: Graph1

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(handles.axes1,x,y,'stacked')

It looks like this: Graph2

So I need to green part be like negative values on the first graph (blue values)

If I do it like this in classic workspace:

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

It looks as I want, but if I do it this way in GUI

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

Only negative values are plotted. Thank you for advice.

1
Can't reproduce your problem. When I use your code in GUI I get the same graph as in classic workspace. What Matlab version do you use? - EBH
My version is R2015b. - Lama Obecna
Hm, when I create just GUI with pushbutton and graph, it's working fine, as you said. Maybe problem is somewhere else. I will describe you the way how I obtain my dataset. I have 2 GUIs. In the first GUI I define parametres and with this parametres I'am calling with pushbutton another function. Input parametres are inputs for this function. Outputs of this fuction I store in struct called "data". This dataset I want to plot in another GUI (as mentioned). So I save struct output to the root by setappdata(0,'data',data) In the plot GUI I call data data = getappdata(0,'data'); - Lama Obecna
Rest of the code is same...just y is y1 = [data.data2' data.data3']; - Lama Obecna
Whole code of plotting GUI is like this: handles.output = hObject; data = getappdata(0,'data'); x = 1:12; y1 = [data.data2' data.data3']; bar(handles.axes1,x,y1,'stacked') hold on bar(handles.axes1,x, -data.data1, 'g') hold off That's the only difference, data are not manually defined, but evaluated and called from another function. - Lama Obecna

1 Answers

0
votes

All you need is specifying the handle you are operating with in hold

clear;clc;close all
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]
x = 1:12;
y1 = [data2' data3'];

ax = axes;
figure % some other figures that interferes gcf()
bar(ax,x,y1,'stacked')
hold(ax,'on')
bar(ax,x, -data1, 'g')
hold(ax,'off')