简体   繁体   中英

Plot 3D histogram using bar3

I'm currently trying to plot the output of hist3 using bar3. This is a simple example:

vec_x = [1 2 4 5 7 8 9 3 8 7 2]';
vec_y = [1 3 9 5 7 8 1 3 2 9 2]';
vec_bin_edges = 0:9;
hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
mat_joint = hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
figure
bar3(mat_joint, 1);
axis tight

In order to demonstrate my issue, I made two pics of both figures:

This one is the output of hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});

This one is the output of bar3(mat_joint, 1); 在此处输入图片说明

As you can see, the bar3 function does not really "bin" the data values as hist3 does, so the bars are shifted slightly in their positions. My question is now, whether it's possible to make the bar3 plot look exactly like the hist3 plot. My motivation to do so is, that I need to modify the mat_joint matrix and plot it again, which is not possible using hist3 .

EDIT: The different colors are not important, it's just about the bin positions

ok, I figured it out:

set(gca, 'xtick', [1.5:1:10.5]);
set(gca, 'ytick', [1.5:1:10.5]);
vec_bin_labels = 1:10;
vec_string_bin_labels = reshape(cellstr(num2str(vec_bin_labels(:))), size(vec_bin_labels));
set(gca, 'xticklabel', vec_string_bin_labels);
set(gca, 'yticklabel', vec_string_bin_labels);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM