2D colour coded plot with already binned data
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have a [32x32] matrix of data that's already binned that I want to make a 2 dimensional color coded plot of - something like this : 

I also want to be able to define the bin edges/centres. I tried using imagesc but couldn't find a way to specify the bin edges. Is there a way I could maybe add the data to a hhistogram2 object?
採用された回答
Here's an example of how to specify bins/edges using fake, random data.
Using imagesc()
See link for additional input options.
% Create fake data
c = rand(32,32);
% Define x and y edges that span from -4 to 4
x = linspace(-4, 4, size(c,2));
y = linspace(-4, 4, size(c,1));
% Produce plot
imagesc(x,y,c)
grid on
colorbar

●
Using heatmap()
See link for additional input options.
% ...continuing from above
heatmap(x,y,c)
colormap('parula')

●
Using rectangle(), when grid is variable in size (build your own color grid)
If the x and y coordinates of each grid is nonuniform and each grid potentially has a different size, you'll have to build the grid from scratch (if there's a different method I'd love to know).
Using the data shared below in one of the comments left by OP, this example builds the color grid from scratch using rectangle().
% c is a [32x32] matrix
% xedges is a monotonically increasing vector of length 33;
% yedges (same)
c = rand(32,32);
xedges = linspace(-4, 4, size(c,2)+1);
yedges = linspace(-4, 4, size(c,1)+1);
% Define bottom, left corner (x,y) of each rectangle
[x, y] = meshgrid(xedges(1:end-1),yedges(1:end-1));
% Determine width and height of each rectangle
[w, h] = meshgrid(diff(xedges), diff(yedges));
% Normalize c matrix (0:1)
cNorm = (c - min(c(:))) / max(c(:));
% Create color matrix
% * you can use any color map: https://www.mathworks.com/help/matlab/ref/colormap.html#buc3wsn-1-map
% * if you change the color map here, change it below as well.
% * I'm setting precision here to 4 decimal places
prec = 1e4;
cmat = parula(prec);
% Assign color (row index) to each value in c
cIdx = round(cNorm * prec);
% loop through each rectangle and draw it
figure
axh = axes;
hold(axh, 'on')
for i = 1:numel(cIdx)
% Don't draw rectangle if color value is 0
if cIdx(i) == 0
continue
end
% Draw rectangle
rh = rectangle(axh, 'Position', [x(i), y(i), w(i), h(i)], ...
'FaceColor', cmat(cIdx(i), :), 'EdgeColor', 'k');
end
% Plot cosmetics
grid(axh, 'on')
colormap(axh, 'parula')
colorbar(axh)
caxis([min(c(:)), max(c(:))])
%% Sanity check
% Confirm that edges are correct by drawing lines at edges.
% Save and restore axis limits
yl = ylim;
xl = xlim;
plot([xedges;xedges], repmat(ylim', size(xedges)), 'k-') %x bins, vertical lines
plot(repmat(xlim', size(yedges)), [yedges;yedges], 'k-') %y bins, horizontal lines
xlim(xl)
ylim(yl)

5 件のコメント
Ace_of_Shovels
2019 年 2 月 11 日
Sorry, I should have mentioned that my bin sizes are irregular and imagesc keeps converting the intervals into regulary spaced ones.
Could you provide an example? Or better yet, the data?
At the very least, please describe the variables 'x', 'y' and 'c' where x and y are your edges and c is your matrix.
- What are the length of x and y?
- Are x and y monotonically increasing?
- Do they have duplicate values?
- Does 'c' just described the color value of each rectangle?
- Do you expect to have a plot with a variety of rectangles of different sizes and colors?
- Will any of the rectangles overlap?
Ace_of_Shovels
2019 年 2 月 11 日
編集済み: Ace_of_Shovels
2019 年 2 月 11 日
I've attached the files for c and x and y edges.
x and y are both (1, 33) and are monotonically increasing. There aren't any duplicates in the edge values if that's what you mean. 'c' would correspond to the colour value of each rectangle. None of the rectangles would overlap.
I updated my solution. See the 3rd section for code that works with your data and produces the following plot (excluding the sanity check at the end of the code).

Ace_of_Shovels
2019 年 2 月 12 日
Precisely what I wanted. Thank you very much.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
