move x axis labels on a heatmap to the top

76 ビュー (過去 30 日間)
HW
HW 2018 年 1 月 24 日
コメント済み: Walter Roberson 2023 年 11 月 9 日
Is it possible to move the x axis label on a heatmap (the heatmap function introduced in version R2017a) to the top of the heatmap? I have tried the following:
ax = gca;
ax.XAxisLocation = 'top';
but in response the error message is "Unrecognized property 'XAxisLocation' for class 'matlab.graphics.chart.HeatmapChart'."

採用された回答

Walter Roberson
Walter Roberson 2018 年 1 月 24 日
You can cheat.
axp = struct(ax); %you will get a warning
axp.Axes.XAxisLocation = 'top';
The Axes property is not normally visible, but you can get to it with struct()
  3 件のコメント
Luna
Luna 2019 年 11 月 8 日
You made my day today! +1 :)
Victor Arshavskiy
Victor Arshavskiy 2020 年 5 月 26 日
O.M.G., this is really great!!!

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

その他の回答 (1 件)

Benjamin Kraus
Benjamin Kraus 2023 年 11 月 8 日
If you want to do this using documented approaches, you can use a tiledlayout to position an axes below the heatmap, and the manipulate the tick labels on that axes instead.
t = tiledlayout(1,1);
ax = axes(t);
% Create the heatmap. Specifying the OuterPosition tells the heatmap not to
% replace/delete the axes, but the position of the heatmap will be driven
% by the tiledlayout, so the actual value of OuterPosition doesn't matter.
x = ["Jan","Feb","Mar","Apr","May","Jun"];
y = ["Jul","Aug","Sep","Oct","Nov","Dec"];
h = heatmap(t, x, y, magic(6), 'OuterPosition', [0 0 1 1]);
% Create a categorical vector with the same elements as the heatmap's tick
% labels:
cx = categorical(x, x);
cy = categorical(y, y);
% Plot categorical data into the axes.
plot(ax, cx, cy)
% Change the YDir of the axes to match the heatmap.
ax.YDir = 'reverse';
% Move the XAxisLocation
ax.XAxisLocation = 'top';
% Turn off the y-tick labels on the axes.
yticklabels(ax,[]);
% Optionally, turn off the x-tick labels on the heatmap.
h.XDisplayLabels = repmat("", size(h.XDisplayLabels));
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 11 月 9 日
This trick should go in one of the blogs !

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

カテゴリ

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