3D bar graph with gradient Z values

22 views (last 30 days)
newbie9 on 24 Aug 2017
Commented: newbie9 on 14 Mar 2018
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.
newbie9 on 14 Mar 2018
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');
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;
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.

Community Treasure Hunt

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

Start Hunting!