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 件のコメント
Stephen
2024 年 5 月 10 日
Perfect! Many thanks!!
Voss
2024 年 5 月 10 日
You're welcome!
Stephen
2024 年 5 月 10 日
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);

Stephen
2024 年 5 月 10 日
VERY helpful, very clear, very thorough. This is tremendously appreciated, Voss!
Voss
2024 年 5 月 10 日
You're welcome!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Surface and Mesh Plots についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
