How to change XTick Labels in a heatmap

조회 수: 190 (최근 30일)
Stefanie Aebi
Stefanie Aebi 2018년 1월 25일
댓글: md mamunur rashid 2018년 8월 10일
Hello I am trying to Change the X ticklabels of a heatmap plot. Apparently, the normal procedure over "xticklabels" is not supported for heatmaps. So I tried out a Workaround, which still has a bug. This is the example:
figure;
heatmap(my_matrix, 'Colormap', colorMap, 'ColorbarVisible', 'on', 'XLabel', 'Time', 'YLabel', 'September')
ax = gca;
axp = struct(ax);
axp.Axes.XTickLabel = num2cell(0:23)
This works in the first go, however if I then change the size of my figure (i.e. I expand to full screen), the X ticklabels return back to their original values (which is 1:24). I have also tried to adapt axp.XAxis.TickLabels, which has the same result. Could you give me a hint on what to do?

채택된 답변

Sean de Wolski
Sean de Wolski 2018년 1월 25일
편집: Sean de Wolski 2018년 1월 25일
The tick labels are deceptively called *data.
figure;
my_matrix = rand(3);
heatmap(my_matrix, 'Colormap', parula(3), 'ColorbarVisible', 'on', 'XLabel', 'Time', 'YLabel', 'September')
ax = gca;
ax.XData = ["Hello" "World" "Thursday"]
Or simply
heatmap(etc..., 'XData', ["Hello" "World" "Thursday"])
  댓글 수: 2
Stefanie Aebi
Stefanie Aebi 2018년 1월 25일
Thanks a lot, that perfectly solves the problem.
md mamunur rashid
md mamunur rashid 2018년 8월 10일
thanks for your answer

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Brendan Hamm
Brendan Hamm 2018년 1월 25일
A heatmap stores the labels in the XDisplayLabels property. It is not a matlab.graphics.axis.Axes, but rather a matlab.graphics.chart.HeatMap which contains a hidden Axes. There is a reason you get the warning when you pass ax to struct and this is because you are seeing undocumented properties which you are not really meant to interact with. If you see this, you should probably be looking for a documented way of doing this, such as looking at the properties using:
properties(ax)
Here we see the documented property XDisplayLabels, and changing this gives the desires result.
ax.XDisplayLabels = num2cell(0:23);
So the moral of the story is that undocumented stuff is nice when you have no other options ... but this should be the last resort!

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by