using bar3(z) to plot in unusual data visualization
显示 更早的评论
Can someone help me write the script to plot my data as 3 separate bar charts, each along an X, Y, and Z axes, such as:

(this was created w/ help of Photoshop)
采纳的回答
red_data = [ 5 5 20 35 15];
blue_data = [30 25 20 15 5];
green_data = [30 25 35 5 5];
Nr = numel(red_data);
Nb = numel(blue_data);
Ng = numel(green_data);
data = NaN(Nb+1,Nr+Ng+1);
data(1,1:Nr) = red_data;
data(2:end,Nr+1) = blue_data;
data(1,Nr+2:end) = green_data;
h = bar3(data);
for ii = [1:Nr Nr+2:Nr+Ng+1]
h(ii).ZData(6:end,:) = NaN;
end
h(Nr+1).ZData(1:5,:) = NaN;
set(h(1:Nr),'FaceColor','r')
set(h(Nr+1),'FaceColor','b')
set(h(Nr+2:end),'FaceColor','g')

7 个评论
Perfect! Many thanks!!
You're welcome!
Update/followup...
Voss, could I trouble you to describe how I could add one more set of data such as:

Much appreciated!
Here is the code (explanation/description to follow momentarily):
red_data = [ 5 5 20 35 15];
yellow_data = [10 10 35 55 20];
green_data = [30 25 35 5 5];
blue_data = [30 25 20 15 5];
Nr = numel(red_data);
Ny = numel(yellow_data);
Ng = numel(green_data);
Nb = numel(blue_data);
data = NaN(Ny+Nb+1,Nr+Ng+1);
data(Ny+1,1:Nr) = red_data;
data(1:Ny,Nr+1) = yellow_data;
data(Ny+1,Nr+2:end) = green_data;
data(Ny+2:end,Nr+1) = blue_data;
h = bar3(data);
h(Nr+1).ZData(6*Ny+(1:5),:) = NaN;
for ii = [1:Nr Nr+2:Nr+Ng+1]
h(ii).ZData([1:6*Ny 6*Ny+7:end],:) = NaN;
end
set(h(1:Nr),'FaceColor','r')
set(h(Nr+2:end),'FaceColor','g')
h(Nr+1).CData = repmat(permute(repelem([1 1 0; 0 0 0; 0 0 1],6*[Ny 1 Nb],1),[1 3 2]),1,4);

red_data = [ 5 5 20 35 15];
yellow_data = [10 10 35 55 20];
green_data = [30 25 35 5 5];
blue_data = [30 25 20 15 5];
First, put the vectors into a matrix of NaNs like this:
Nr = numel(red_data);
Ny = numel(yellow_data);
Ng = numel(green_data);
Nb = numel(blue_data);
data = NaN(Ny+Nb+1,Nr+Ng+1);
data(Ny+1,1:Nr) = red_data;
data(1:Ny,Nr+1) = yellow_data;
data(Ny+1,Nr+2:end) = green_data;
data(Ny+2:end,Nr+1) = blue_data;
disp(data)
NaN NaN NaN NaN NaN 10 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 10 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 35 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 55 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 20 NaN NaN NaN NaN NaN
5 5 20 35 15 NaN 30 25 35 5 5
NaN NaN NaN NaN NaN 30 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 25 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 20 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 15 NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN 5 NaN NaN NaN NaN NaN
When you create a 3d bar graph from that matrix, it looks like this by default:
figure
h = bar3(data);
xlabel('x')
ylabel('y')

So the task is to re-color the objects created by bar3.
bar3 returns a vector of surface objects, one object per x value, and I'll set the properties of those surface objects in order to re-color.
figure
h = bar3(data)
h =
1x11 Surface array:
Surface Surface Surface Surface Surface Surface Surface Surface Surface Surface Surface
The first thing to do is to eliminate the colored squares from the regions where I don't want any bars. I'll do that by essentially setting their heights to NaN so that they are not rendered. The ZData property of a surface object describes the surface's vertices' locations in the Z-direction, so that's what I'll set to NaN.
For example, the ZData of the middle surface at x = 6 contains the data in the yellow_data and blue_data vectors (as well as a zero-height bar in between the yellow and the blue) but each element is repeated in a 2x2 square and surrounded by zeros which are surrounded by NaNs (so each group of 6 rows in ZData describes a single bar's height):
size(h(Nr+1).ZData) % h(Nr+1) is the middle surface (at x = Nr+1 = 6 in this example)
ans = 1x2
66 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
disp(h(Nr+1).ZData)
NaN 0 0 NaN
0 10 10 0
0 10 10 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 10 10 0
0 10 10 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 35 35 0
0 35 35 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 55 55 0
0 55 55 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 20 20 0
0 20 20 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 0 0 0
0 0 0 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 30 30 0
0 30 30 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 25 25 0
0 25 25 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 20 20 0
0 20 20 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 15 15 0
0 15 15 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
NaN 0 0 NaN
0 5 5 0
0 5 5 0
NaN 0 0 NaN
NaN 0 0 NaN
NaN NaN NaN NaN
I'll use the fact that each bar takes up 6 rows of ZData to set the appropriate elements of ZData to NaN in order to make the bars I don't want to show up effectively disappear.
This sets the bar between the yellow and blue sections to NaN:
h(Nr+1).ZData(6*Ny+(1:5),:) = NaN;
And this sets the rest of the bars outside the red/green/yellow/blue sections to NaN:
for ii = [1:Nr Nr+2:Nr+Ng+1]
h(ii).ZData([1:6*Ny 6*Ny+7:end],:) = NaN;
end

So now the plot looks like that.
(Now, repeating that code for a new figure ...)
figure
h = bar3(data);
h(Nr+1).ZData(6*Ny+(1:5),:) = NaN;
for ii = [1:Nr Nr+2:Nr+Ng+1]
h(ii).ZData([1:6*Ny 6*Ny+7:end],:) = NaN;
end
All that remains is to change the colors of the remaining surfaces.
The first Nr surfaces (for x = 1 to x = Nr) need to be all red, and the last Ng surfaces (for x = Nr+2 to x = Nr+Ng+2) need to be all green. That's easy because each surface is all one color:
set(h(1:Nr),'FaceColor','r')
set(h(Nr+2:end),'FaceColor','g')
But it's tricker for the surface at x = Nr+1 because it needs to contain both yellow and blue bars (this wasn't an issue when there was no yellow section). For surfaces whose faces need to be different colors you can use the CData property instead of FaceColor. CData in this case is going to be a m-by-n-by-3 array of RGB values. It's again 6 rows per bar.
Rather setting that surface's CData all in one line of code, I break it up into two parts for illustration, where first I construct a temporary m-by-3 matrix of RGB colors containing yellow [1 1 0], black [0 0 0] (for the bar in between yellow and blue - doesn't matter what it is since its ZData is NaNs), and blue [0 0 1], each repeated the appropriate number of times:
C = repelem([1 1 0; 0 0 0; 0 0 1],6*[Ny 1 Nb],1)
C = 66x3
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
and then manipulate it to be m-by-4-by-3. I know it has to be 4 since that's how many columns ZData has (because each surface face has four vertices).
h(Nr+1).CData = repmat(permute(C,[1 3 2]),1,4);

VERY helpful, very clear, very thorough. This is tremendously appreciated, Voss!
You're welcome!
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
另请参阅
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
