using bar3(z) to plot in unusual data visualization
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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)
