Export colors of hist3 boxes in an array

1 回表示 (過去 30 日間)
MichailM
MichailM 2019 年 11 月 18 日
編集済み: Adam Danz 2019 年 11 月 19 日
Having the simple script below:
close all;
clear all;
clc;
x = rand(10,2);
hist3(x,'CdataMode','auto')
view(2)
which generates a figure like the one below,
untitled.png
Is there any way I can possibly export the RGB colors in a NxNx3 array (here N=10)?

採用された回答

Adam Danz
Adam Danz 2019 年 11 月 18 日
編集済み: Adam Danz 2019 年 11 月 19 日
After creating the bivariate histrogram with hist3(), call the function again with the same inputs but this time, store the first output which is the binvariate bin count N = hist3(___). This will not produce a plot which is why it must be called separately from the plot.
Assuming the bivariate histrogram was produced with default CDataMapping property, you can scale the bin counts to the axes colormap range to compute the RGB values for each bin.
% Produce the bivariate hist
x = rand(10,2);
hist3(x,'CdataMode','auto') % produces plot
count = hist3(x); % grabs bin count; does not produce plot
view(2)
ax = gca(); % Get axis handle
cm = ax.Colormap; % Get axis colormap
%Scale the bin count to colormap scale
count = round((count-min(count(:)))./max(count(:)).*(size(cm,1)-1) + 1);
% produce 3D array with 3 'pages' for RGB values
% This has the same orientation as 'count'
rgbArray = permute(reshape(cm(count,:).', [3,size(count)]),[2,3,1]);
Investigate the count matrix to explore the mapping between the count bins and your histrogram bins. You'll find that the first column of count represents the bottom row of bins in the plot. Therefore rgbArray(i,j,:) contains the color for the i-th column and j-th row from the bottom. You may want to do some transposing or flipping to orient the array to your liking.
hist3() produces a surface object. Check out more surface properties here:
  2 件のコメント
MichailM
MichailM 2019 年 11 月 19 日
Thanks for the very useful reply. The "cm(count,:)" in the very last line of your script was returning an error for the cases where at least one of the elements of the "count" was not an integer. Below is another implementation based on your suggestions.
close all;
clear all;
clc;
x = rand(10,2);
hist3(x,'Nbins',[size(x,1) size(x,1)],'CDataMode','auto','FaceColor','interp')
count = hist3(x,'Nbins',[size(x,1) size(x,1)],'CDataMode','auto','FaceColor','interp');
view(2)
ax = gca();
cm = ax.Colormap;
count = (count-min(count(:)))./max(count(:)).*(size(cm,1)-1) + 1;
A = reshape(cm(round(rot90(count)),:),size(count,1),size(count,2),3);
Adam Danz
Adam Danz 2019 年 11 月 19 日
編集済み: Adam Danz 2019 年 11 月 19 日
ahh.... a simple fix is to round the count variable. I updated my answer to reflect that change.
% old line
count = (count-min(count(:)))./max(count(:)).*(size(cm,1)-1) + 1;
% New line
count = round((count-min(count(:)))./max(count(:)).*(size(cm,1)-1) + 1);
% ^^^^^^ ^

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by