Schnigges
Schnigges

Reputation: 1326

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); enter image description here

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

Upvotes: 1

Views: 8138

Answers (1)

Schnigges
Schnigges

Reputation: 1326

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);

Upvotes: 1

Related Questions