Search code examples
matlabfill

horizontal standard deviation fill plot in matlab


I have an example file. The first column is my y axis (representing depth). 2nd column are the mean values. 3rd column is the mean + standard deviation and 4th column is mean - standard deviation.

50      8,52021458848310    12,8530947497846    4,18733442718158
100     19,2273240305912    23,7734814467110    14,6811666144714
150     24,2615944638934    30,2678190187990    18,2553699089878
200     30,6833427597963    39,4337701225571    21,9329153970354
250     34,9509485935449    47,8249116812250    22,0769855058648
300     32,4629780635280    40,1731214462594    24,7528346807966
350     33,7584752483269    43,2424890071851    24,2744614894686
400     39,6944893679358    52,5666743873288    26,8223043485428
450     46,5051455836423    63,5570927760677    29,4531983912169
500     49,0458418910265    70,7507807967089    27,3409029853441
550     52,0737915678693    70,5030313767484    33,6445517589901
600     57,5870126919132    81,1282679272898    34,0457574565365
650     58,7946350532416    85,0428844330643    32,5463856734189
700     59,3263566691245    92,6606340696131    25,9920792686359
750     69,0930407230073    105,935771600040    32,2503098459742
800     71,4416953118855    113,211405934863    29,6719846889079
850     70,3617175991868    111,663890244861    29,0595449535130
900     64,7764846391086    97,4995723191651    32,0533969590521
950     70,7142864622626    102,429751242886    38,9988216816390
1000    88,0269426788223    123,259291760572    52,7945935970724
1250    126,529915074894    193,319779063845    59,7400510859419
1500    173,395473019573    242,127131744655    104,663814294491
1750    225,087445395238    300,319434885651    149,855455904824
2000    269,218588809667    295,582291796995    242,854885822339
figure();
plot(example(:,2),example(:,1),'b-x','linewidth',2);
set(gca,'ydir','rev','xlim',[0 400],'ylim',[0 2000]);
grid on; hold on;
fill( [example(:,3) fliplr(example(:,4))], [example(:,1) fliplr(example(:,1))], 'k'); alpha(.25);

My fill area on the plot is not coming out as how it should. And I am not really sure what the problem is either. What I get is the image on the left, but what it really should be is the image on the right, except filled. enter image description here


Solution

  • Correct alignment of vectors, i.e. align all vectors to one large vector. Else you have two sets of vectors, which are regarded seperately

    [example(:,x) example(:,y)] --> [example(:,x); example(:,y)]
    

    and flipud instead of fliplr

    fliplr(example(:,x)) // --> does nothing
    flipud(example(:,x)) // --> for a column vector the correct approach
    

    a working code example

    %%
    example = [50      8.52021458848310    12.8530947497846    4.18733442718158
    100     19.2273240305912    23.7734814467110    14.6811666144714
    150     24.2615944638934    30.2678190187990    18.2553699089878
    200     30.6833427597963    39.4337701225571    21.9329153970354
    250     34.9509485935449    47.8249116812250    22.0769855058648
    300     32.4629780635280    40.1731214462594    24.7528346807966
    350     33.7584752483269    43.2424890071851    24.2744614894686
    400     39.6944893679358    52.5666743873288    26.8223043485428
    450     46.5051455836423    63.5570927760677    29.4531983912169
    500     49.0458418910265    70.7507807967089    27.3409029853441
    550     52.0737915678693    70.5030313767484    33.6445517589901
    600     57.5870126919132    81.1282679272898    34.0457574565365
    650     58.7946350532416    85.0428844330643    32.5463856734189
    700     59.3263566691245    92.6606340696131    25.9920792686359
    750     69.0930407230073    105.935771600040    32.2503098459742
    800     71.4416953118855    113.211405934863    29.6719846889079
    850     70.3617175991868    111.663890244861    29.0595449535130
    900     64.7764846391086    97.4995723191651    32.0533969590521
    950     70.7142864622626    102.429751242886    38.9988216816390
    1000    88.0269426788223    123.259291760572    52.7945935970724
    1250    126.529915074894    193.319779063845    59.7400510859419
    1500    173.395473019573    242.127131744655    104.663814294491
    1750    225.087445395238    300.319434885651    149.855455904824
    2000    269.218588809667    295.582291796995    242.854885822339];
    figure();
    plot(example(:,2),example(:,1),'b-x','linewidth',2);
    set(gca,'ydir','rev','xlim',[0 400],'ylim',[0 2000]);
    grid on; hold on;
    plot(example(:,4), example(:,1))
    plot(example(:,3), example(:,1))
    fill( [example(:,3); flipud(example(:,4))], [example(:,1); flipud(example(:,1))], 'k'); alpha(.25);