3D bar graph with gradient Z values

I'm trying to make a 3D bar graph from a 3-column matix (let's call the columns x-y-z). For any x-y combo, there are multiple z values (sort of like a stacked 3D bar graph). I want to make a plot where the Z values are a gradient, instead of discrete stacks.
Here's an example from different software... I'm having trouble replicating it. I'd like to be able to replicate it and preferably have the vertical colors be a gradient.
Thanks for any help, and apologies if this solution is out there and I have simply missed it.

Answers (1)

Michael Abboud
Michael Abboud on 28 Aug 2017
Edited: Michael Abboud on 28 Aug 2017
Hi Alex,
If I've understood your question correctly, I believe the following link should provide the solution. Essentially you can accomplish this in 3 steps:
1. Use the 'bar3' function to create a 3-D bar graph
Then for each 'surface' element of the returned object (AKA for each row of bars):
2. Set the 'CData' property equal to the current 'ZData'
3. Set the 'FaceColor' property to 'interp'
From here you can adjust the colormap as for whatever scheme you'd like.
Reference:

3 Comments

Hi Michael, Thanks so much for taking the time to reply. I spent some time looking at that example before I posted--I think I understand part of my trouble now, as I had an error in my question/post regarding my data format.
The data are in five columns: an X-Y grid (11 by 8), and in the example shown in my OP there is a "Z1" column that represents the "bottom of the yellow" for example and a "Z2" column that represents the "top of the yellow." And the "bottom of the orange" is equal to the "top of the yellow," etc.
When I plot in bar3 (even before putting in colors), the view is not what I expected. I think this is perhaps because I have duplicate X-Y pairs (with different Z values). I'm having a hard time understanding the data format for bar3(x,y) in the help.
The actual data used to make the example shown in the OP are attached, and here is a bit of code I am working with:
fid001 = fopen('deagg_data_all.txt', 'r');
header = textscan(fid001, '%s %s %s %s %s', 1 , 'HeaderLines', 0);
data = textscan(fid001, '%f %f %f %f %f', 'Delimiter', '\t');
fclose(fid001);
x = data{:,1};
y = data{:,2};
z = data{:,4};
data2 = [x y z];
% below only plots two dimensions
b = bar3(x,z,y);
% below throws an error; my matrix dimensions are wrong
%for k = 1:length(b)
% zdata = z;
% b(k).CData = z;
% b(k).FaceColor = 'interp';
%end
Thanks for any help, Alex
Hi Alex, thanks for providing more information.
It looks like the primary issue is transforming your data from [x,y,z] format, to more of an 'image' format, where the (x,y) location in your matrix contains a value 'z'. Consider:
data3 = zeros(max(x),max(y));
for i = 1:length(x)
xx = x(i); yy = y(i); zz = z(i);
if zz > data3(xx,yy)
data3(xx,yy) = zz;
end
end
Note that I'm only taking the largest Z since there are many duplicate points. Also I've found in some cases, using an ">> axis tight" greatly helps the appearance after plotting.
Using the provided data and copying the example from my answer, it should look something like this before any further modifications. Hope this helps!
Hello Michael (and other Matlab experts),
I'm still having some trouble with my 3D bar charts, but it's getting closer! Your previous reply with the loop worked like a charm, so many thanks!
This code (attached data) was used to generate the jpeg.
fid001 = fopen('T001_RP2_Deagg_Combined_mod.txt', 'r');
header = textscan(fid001, '%s %s %s %s %s %s', 1 , 'HeaderLines', 0);
data = textscan(fid001, '%f %f %f %f %f %f', 'Delimiter', '\t');
fclose(fid001);
data = cell2mat(data);
dist = data(:,1);
mag = data(:,5);
color_code = data(:,6);
percent_top = data(:,4);
percent_bot = data(:,3);
data2 = zeros(max(dist),max(mag));
for i = 1:length(dist)
xx = dist(i); yy = mag(i); zz = percent_top(i);
if zz > data2(xx,yy)
data2(xx,yy) = zz;
end
end
bar3(data2)
I would like to set the colors to be gradiated based on the corresponding "color_code" value.
Alternatively, I would be okay with a stacked plot. However, bar3(data2,'stacked') collapses down everything from the "mag" axis into one slot. In the attached dataset, the bottoms and tops of the stacks are actually calculated (that's the "percent_bot" and "percent_top" variables).
Thanks for any help and clever ideas!

Sign in to comment.

Asked:

on 24 Aug 2017

Commented:

on 14 Mar 2018

Community Treasure Hunt

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

Start Hunting!