フィルターのクリア

Heatmap Chart - Depict Squares rather than Rectangles

41 ビュー (過去 30 日間)
Dario Walter
Dario Walter 2019 年 9 月 23 日
コメント済み: mkarikom 2020 年 4 月 21 日
Hey guys,
I have a heatmap object h and would like to depict squares rather than rectangles (see appendix). The heatmap properties (https://de.mathworks.com/help/matlab/ref/matlab.graphics.chart.heatmapchart-properties.html) do not provide any help.
Do you have an idea?
Thank you very much!
  2 件のコメント
Walter Roberson
Walter Roberson 2019 年 9 月 23 日
I wonder if it would help to use
axis equal
Dario Walter
Dario Walter 2019 年 9 月 23 日
Hey Walter,
thanks for your quick reply. Unfortunately, axis equal does not work for heatmap objects :/.

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

採用された回答

Adam Danz
Adam Danz 2019 年 9 月 23 日
編集済み: Adam Danz 2019 年 9 月 23 日
It drives me nutz that heatmap properties are special cases. To set the aspect ratio of the heatmap, you need to adjust the axis or figure size based on the number or rows & columns. Below are two functional demos. The first adjusts the axis size and recenters it. The second adjusts the figure size instead. I recommend the first method so the heatmap title and other graphics won't potentially expand beyond the figure boundaries.
Method 1: Adjust axis size and re-center
% Create heatmap using built-in Matlab data
load patients
tbl = table(LastName,Age,Gender,SelfAssessedHealthStatus,...
Smoker,Weight,Location);
h = heatmap(tbl,'Smoker','SelfAssessedHealthStatus');
% Temporarily change axis units
originalUnits = h.Units; % save original units (probaly normalized)
h.Units = 'centimeters'; % any unit that will result in squares
% Get number of rows & columns
sz = size(h.ColorData);
% Change axis size & position;
originalPos = h.Position;
% make axes square (not the table cells, just the axes)
h.Position(3:4) = min(h.Position(3:4))*[1,1];
if sz(1)>sz(2)
% make the axis size more narrow and re-center
h.Position(3) = h.Position(3)*(sz(2)/sz(1));
h.Position(1) = (originalPos(1)+originalPos(3)/2)-(h.Position(3)/2);
else
% make the axis size shorter and re-center
h.Position(4) = h.Position(4)*(sz(1)/sz(2));
h.Position(2) = (originalPos(2)+originalPos(4)/2)-(h.Position(4)/2);
end
% Return axis to original units
h.Units = originalUnits;
Method 2: Adjust figure size
% Create heatmap using built-in Matlab data
load patients
tbl = table(LastName,Age,Gender,SelfAssessedHealthStatus,...
Smoker,Weight,Location);
h = heatmap(tbl,'SelfAssessedHealthStatus','Smoker');
% ^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^ switched from method 1
% Temporarily change figure units
fh = gcf(); %if you don't already have the figure handle
originalUnits.fig = fh.Units; % save original units (probaly normalized)
originalUnits.ax = h.Units;
fh.Units = 'centimeters'; % any unit that will result in squares
h.Units = 'Normalize';
% Get number of rows & columns
sz = size(h.ColorData);
% make axes square (not the table cells, just the axes)
h.Position(3:4) = min(h.Position(3:4))*[1,1];
fh.InnerPosition(3:4) = min(fh.InnerPosition(3:4))*[1,1];
% Change figure position;
if sz(1)>sz(2)
% make the figure size more narrow
fh.InnerPosition(3) = fh.InnerPosition(3)*(sz(2)/sz(1));
else
% make the figure size shorter
fh.InnerPosition(4) = fh.InnerPosition(4)*(sz(1)/sz(2));
end
% return original figure units
fh.Units = originalUnits.fig;
h.Units = originalUnits.ax;
  3 件のコメント
Adam Danz
Adam Danz 2019 年 9 月 23 日
Ah, so the number of rows are the same as the number of columns in your heatmap, then (unlike in the demo). Glad I could help!
mkarikom
mkarikom 2020 年 4 月 21 日
This is very helpful, spent all morning trying to figure out why subfigures, tiled objects, etc wouldn't respond as expected.

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

その他の回答 (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