1

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

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
jdoubleu
  • 73
  • 5
  • 1
    "_... manipulated the X-Axis values since there is not a better way to do so_" **what is "so"**? Note that Ander's answer from 2015 is meant for an older version of MATLAB, before the new graphics system known as "HG2" was introduced. There should be a different way to do it with recent MATLAB version. Please explain what you were trying to achieve that gave you these hollow bars! – Dev-iL Jul 30 '18 at 14:33
  • Thanks for your answer. I was trying to set my own X-Axis values, see `klasse_sig_a`. Since bar3 plots only (Y,Z) X axis can only be set by manipulation. I haven't seen a better method so far, not even in my matlab version. I'm using 2016a. – jdoubleu Jul 30 '18 at 14:44
  • yup, that code is old. Probably there is a better way now. – Ander Biguri Jul 30 '18 at 14:55
  • Ugh. 3D bar plots. Combining the worst of both worlds... :p If you share the goal of your data visualization I might be able to suggest a better alternative. – Cris Luengo Jul 30 '18 at 15:11

1 Answers1

2

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

gnovice
  • 125,304
  • 15
  • 256
  • 359