Exclude NaN values when plotting using bar3

13 次查看(过去 30 天)
I want to create a 3d bar plot which only shows bars in locations with real values (not NaNs). Unfortunately, when I try to do this, the plot includes a bar of height zero in each location where there's a NaN. I want no bar at all - just a blank space. Can anybody help?
clear; close all; clc
S = [37,46];
loads = [nan, 1.62, 1.85, nan;
0.88, nan, 0.85, 0.62];
figure()
bar3(S,loads)
xticks([1,2,3,4])
xticklabels({'14','15','17.5','21'})
xlabel('H, mm')
ylabel('S, mm')
zlabel('Pull-off Force, N')
Output:
  1 个评论
Rik
Rik 2020-4-29
You may need to plot each bar separately. The bar3 function returns a 1x4 surface object when I run your code, so you can't simply delete some patch objects, as I had hoped.

请先登录,再进行评论。

采纳的回答

Ameer Hamza
Ameer Hamza 2020-4-29
编辑:Ameer Hamza 2020-4-30
Edit: This code is a more general and does not require to set the edge color to white
clear; close all; clc
S = [37,46];
loads = [nan, 1.62, 1.85, nan;
0.88, nan, 0.85, 0.62];
fig = figure();
ax = axes();
hold(ax);
grid(ax);
view(3);
b = gobjects(size(loads));
for i=1:size(loads,2)
for j=1:size(loads,1)
if ~isnan(loads(j,i))
b(j,i) = bar3(S(j), loads(j,i), 5);
b(j,i).CData(:) = i;
b(j,i).XData = rescale(b(j,i).XData, 0.6, 1.4) + i - 1;
end
end
end
ax.CLim = [1 size(loads, 2)];
ax.YDir = 'reverse';
ax.XTick = 1:size(loads,2);
xticklabels({'14','15','17.5','21'})
ax.YTick = S;
axis(ax, 'tight');
ax.YLim = ax.YLim + [-2 2];
xlabel('H, mm')
ylabel('S, mm')
zlabel('Pull-off Force, N')
Try this. The downside of this method is that it cannot remove the edges of the individual bar, so It set the color of all the edges to white so that the edges on the ground seem invisible.
clear; close all; clc
S = [37,46];
loads = [nan, 1.62, 1.85, nan;
0.88, nan, 0.85, 0.62];
fig = figure();
b = bar3(S,loads);
xticks([1,2,3,4])
xticklabels({'14','15','17.5','21'})
xlabel('H, mm')
ylabel('S, mm')
zlabel('Pull-off Force, N')
[b.EdgeColor] = deal('w');
[r,c] = find(isnan(loads));
for i=1:numel(r)
r_ = r(i);
c_ = c(i);
b(c_).CData(6*(r_-1)+1:6*r_, :) = nan;
end
  4 个评论
Ameer Hamza
Ameer Hamza 2020-4-30
I am glad to be of help.
No, the edge color cannot be set individually. Each surface object has a single EdgeColor property.
Ameer Hamza
Ameer Hamza 2020-4-30
I have updated my answer to show a more general way, which does not require setting the edge color to white. Each bar is a seperate surface object.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by