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

21 ビュー (過去 30 日間)
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

採用された回答

Subhadeep Koley
Subhadeep Koley 2019 年 11 月 6 日
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!

その他の回答 (1 件)

Min Fang
Min Fang 2019 年 11 月 9 日
Thanks! that's very helpful!

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by