I am using Matlab 2019a and I want to bar-plot positive and negative vectors, positive ones above and negative ones below the x-axis. The following code works fine except for...
a) the colors and legends. I want to have the same colors and corresponding legend entries for each the revenue and the opex vector.
b) I want to have those vectors that are mostly negative (in this case opex) closer to the x-axis when they are positive than those that are mostly positive (in this case revenue). I.e. always revenue above opex in the positive part of the plot.
I'd like to write it in an efficient way so that I can generalize it for more vectors. Thank you!
clc
clear
close all
revenue = ones(100,1);
opex = -1*ones(100,1);
opex(10:15,1) = 3;
revenueNeg = revenue;
revenueNeg(revenueNeg>0) = 0;
revenuePos = revenue;
revenuePos(revenuePos<0) = 0;
opexNeg = opex;
opexNeg(opexNeg>0) = 0;
opexPos = opex;
opexPos(opexPos<0) = 0;
yDataNeg = [revenueNeg, opexNeg];
yDataPos = [revenuePos, opexPos];
hold on;
bar(yDataNeg,'stack')
bar(yDataPos,'stack')
legend('Revenue','Opex');
hold off;