MATLAB: Graph colour using patch

patch

I want to be able to have a different colour with the points above 0 in the y-axis. Can any one please help! The code is as shown below. Thanks
% Pos = 2
GqrotIS2 = [-0.8479 -0.7562];
% Pos = 3
GqrotIS3 = [-0.7562 -0.05];
% Pos = 4
GqrotIS4 = [-0.8808 -0.0831];
% Pos = 5
GqrotIS5 = [-0.7863 -0.0581];
% Pos = 6
GqrotIS6 = [-21.2861 0.0931];
% Pos = 7
GqrotIS7 = [-22.7203 0.086];
% Pos = 8
GqrotIS8 = [-21.9607 0.0896];
% Pos = 9
GqrotIS9 = [-23.44 0.0823];
% Pos = 10
GqrotIS10 = [-0.8234 -0.0699];
% Pos = 11
GqrotIS11 = [-0.7268 -0.0431];
% Pos = 12
GqrotIS12 = [-0.8556 -0.0787];
% Pos = 13
GqrotIS13 = [-0.7559 -0.0509];
% Pos = 14
GqrotIS14 = [-20.733 0.0934];
% Pos = 15
GqrotIS15 = [-22.1352 0.0864];
% Pos = 16
GqrotIS16 = [-21.3903 0.09];
% Pos = 17
GqrotIS17 = [-22.8366 0.0827];
v = [ -0.8479 -0.7562 -0.8808 -0.7863 -21.2861 -22.7203 -21.9607 -23.44 -0.8234 -0.7268 -0.8556 -0.7559 -20.733 -22.1352 -21.3903 -22.8366; ...%bottom of each element
-0.0742 -0.05 -0.0831 -0.0581 0.0931 0.086 0.0896 0.0823 -0.0699 -0.0431 -0.0787 -0.0509 0.0934 0.0864 0.09 0.0827]; %top of each element
[K,N] = size(v);
assert(K == 2);
assert(N >= 1);
assert(all(diff(v) >= 0))
sq_x = [0; 1; 1; 0; 0];
sq_y = [0; 0; 1; 1; 0];
%%
fig = figure;
ylabel('GQROT Gain')
xlabel ('Position (Flight Conditions)')
patch(sq_x*0.5 + (1:N-1)-0.25, sq_y .* diff(v(:,1:N-1)) + v(1,1:N-1), 'g')
patch(sq_x*0.5 + (13:N)-0.25, sq_y .* diff(v(:,13:N)) + v(1,13:N), 'r')
patch(sq_x*0.5 + (5:N-8)-0.25, sq_y .* diff(v(:,5:N-8)) + v(1,5:N-8), 'r')
%patch(sq_x*0.5 + N-0.25, sq_y .* diff(v(:,N)) + v(1,N), 'g')
set(gca, 'XTick', 1:N)
% legend('0 \in denominator','0 not \in denominator')

Best Answer

  • I am not certain what you want.
    Try this for the plot:
    v2gt0 = v(2,:) > 0;
    nnzv2gt0 = nnz(v2gt0);
    figure
    plot([1;1]*(1:size(v,2)), v, 'g', 'LineWidth',10)
    hold on
    Q1 = zeros(1,nnzv2gt0);
    Q2 = v(2,v2gt0);
    plot([1;1]*(1:size(v,2)), [zeros(size(v2gt0)); v(2,:).*v2gt0], 'r','LineWidth',10)
    hold off
    grid
    xlim([0 17])
    ylabel('GQROT Gain')
    xlabel ('Position (Flight Conditions)')
    ylim([-1 1]) % ‘Zoom’ For Detail (Delete)
    This is a second figure to compare with the first. Add it to the end, since the rest of your code is unchanged.
    .