MATLAB Answers

How to do 3D Histogram Plot with self defined colors according to certain data series?

7 views (last 30 days)
Min Fang
Min Fang on 3 Nov 2019
Answered: Min Fang on 9 Nov 2019
Hi I want to do 3d plot of density but with colored decision rules. Hopefully I am clear enough.
So, first, I have some policy function like following: take X = Capital, Y = log(z), now the contour map is Z1, lets call it "policy" Z1=F1(X,Y)
Now, we have another function which is density function Z2 = f2(X,Y), a demo is following: Suppose LengthofRide is log(z) which is X, Year is Capital which is Y
Finally, I want to plot this 3d histogram with the coloring as in the contour map. Is it possible in matlab?
Thanks, Ethan

  0 Comments

Sign in to comment.

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 6 Nov 2019
I assume that you already have one contour plot with a particular colormap and now you want to apply the same colormap to your 3D histogram. Since you have not provided any data to reproduce the problem, I used some demo data. Refer to the example code below which plots a 3D histogram with the coloring as in the contour map. To color the histogram bars according to the values, you need to set ‘CDataMode’ property of the hist3()function to ‘auto’.
% Demo data for the contour plot
[X,Y,Z] = peaks;
% Plot the contour
fig1 = figure; contour(X,Y,Z,50); colormap spring; title('Contour Plot');
% Demo data for the 3D histogram
X1 = rand(1,406)'; Y1 = rand(1,406)'; A = [X1,Y1];
% Plot the 3D histogram
fig2 = figure; hist3(A,'CDataMode','auto','FaceColor','interp'); title('3D Histogram');
% Get the colormap from the 1st figure
cmap = colormap(fig1);
% Apply the colormap to the 2nd figure
colormap (fig2, cmap);
ctrPlot.png 3DHist.png
Hope this helps!

  0 Comments

Sign in to comment.

Sign in to answer this question.