Search code examples
matlabmatlab-figure

Manipulating bar3 x-Axis values results into hollow barplot


I am setting up a bar3 plot and manipulated the X-Axis values since there is not a better way to do so. I went thorugh the code that Ander Biguri provided in his answer to this thread: How to set x and y values when using bar3 in Matlab?.

It turns out that the X-Axis values are fine but the bars that are not located at the borders are archetype shaped. Probably it has to do with the data manipulation.

Here is the corrisponding plot:

bar3 Plot with hollow bars

The data i used for this example:

klasse_sig_a=[70 82 94 106 118 130 142 154 166 178 190];
klasse_sig_m=[-120 -102 -84 -66 -48 -30 -12 6 24 42 60];
RFMatrix=
[2  0   0   0   0   0   0   0   0   0;
0   0   0   0   0   0   0   0   0   0;
0   0   0   0   0   0   0   0   0   0;
0   0   0   0   0   0   0   0   0   0;
0   0   0   0   0   0   0   0   0   0;
0   0   0   0   0   0   0   1   0   0;
0   0   0   0   2   0   0   0   0   2;
0   0   0   0   1   0   0   0   0   0;
0   0   0   0   0   0   0   0   0   0;
0   0   0   0   2   0   0   0   0   0;]

My code:

b=bar3(klasse_sig_m(2:end),RFMatrix,1);
xlabel('\sigma_a [MPa]')
ylabel('\sigma_m [MPa]')
zlabel('N [-]')
axis tight

for k = 1:length(b)
    zdata = b(k).ZData;
    b(k).CData = zdata;
    b(k).FaceColor = 'interp';
end

Xdat=get(b,'XData');
diff=klasse_sig_a(2)-klasse_sig_a(1);
ONEMAT=ones(size(Xdat{1},1),size(Xdat{1},2)/2);

for ii=1:length(Xdat)
    MAT=(Xdat{ii}-0.5);
    if ii==1
        MAT=MAT+[ONEMAT*min(klasse_sig_a) ONEMAT*(min(klasse_sig_a)+diff)-ii];
        MAT_VOR=MAT(:,3:4);
    else
        MAT(:,1:2)=MAT_VOR;
        MAT(:,3:4)=MAT(:,3:4)+ONEMAT*(min(klasse_sig_a)+ii*diff)-ii;
        MAT_VOR=MAT(:,3:4);
    end
    Xdat{ii}=MAT;
    set(b(ii),'XData',Xdat{ii});
end
set(gca,'XTick', klasse_sig_a(1:2:end))
set(gca,'YTick', klasse_sig_m(1:2:end))

I noticed that the non manipulated data always has a difference of 1 between left and right side of the matrix for each xdata{ii}

   ...       ...       ...       ...
   NaN       NaN       NaN       NaN
   NaN    0.5000    1.5000       NaN
0.5000    0.5000    1.5000    1.5000
0.5000    0.5000    1.5000    1.5000
   NaN    0.5000    1.5000       NaN
   NaN    0.5000    1.5000       NaN
   NaN       NaN       NaN       NaN

When setting my own data the difference becomes much bigger and the bar plots become hollow

   ...   ...   ...  ...
   NaN    70    82   NaN
    70    70    82    82
    70    70    82    82
   NaN    70    82   NaN
   NaN    70    82   NaN
   NaN   NaN   NaN   NaN

How can I make the bars appear solid again? I guess the data manipulation is erroneous. Thanks for any help! Regards


Solution

  • Note that the answer to the related question is specifically designed to deal with the case when your x values are sequential integers (i.e. the bin width is 1). Your case is more general, with a bin width of 12. This requires slightly different logic. I was able to get your desired results with the following code:

    b = bar3(klasse_sig_m(2:end), RFMatrix,1);
    xlabel('\sigma_a [MPa]');
    ylabel('\sigma_m [MPa]');
    zlabel('N [-]');
    axis tight;
    
    for k = 1:length(b)
      xData = b(k).XData;
      zData = b(k).ZData;
      set(b(k), 'XData', (xData-k).*diff(klasse_sig_a(k:(k+1)))+klasse_sig_a(k), ...
                'CData', zData, 'FaceColor', 'interp');
    end
    
    set(gca, 'XTick', klasse_sig_a(1:2:end), 'YTick', klasse_sig_m(1:2:end));
    

    And the plot:

    enter image description here