how to change the XTick of heatmap?

조회 수: 15 (최근 30일)
Rt Ro
Rt Ro 2019년 12월 3일
댓글: Ayobami Olanrewaju 2021년 2월 9일
when I run this code I have 192 months on the x-axis.
I want to show each year (Jan1971,Jan 1972,...Dec 1986) on the x axis. if I do:
set(h,'XTick',datetime('Jan-1971'):calmonths(12):datetime('Dec-1986'));
I have this error:
Error using matlab.graphics.chart.HeatmapChart/set
The name 'XTick' is not an accessible property for an instance of class 'matlab.graphics.chart.HeatmapChart'.
could you please help me to solve the problem.
tbl = readtable('heatmap.test.xlsx');
t1 = datetime(1971,1,1);t2 = datetime(1986,12,1);
t = t1:calmonths(1):t2;t= t(:);
xlabels = string(datestr(t,12));
ylabels = {'A' 'B' 'C' 'D'};
cdata = tbl{:,2:5};
h = heatmap(xlabels,ylabels,cdata');

채택된 답변

Adam Danz
Adam Danz 2019년 12월 3일
편집: Adam Danz 2019년 12월 3일
I'm not sure what rule you were following to chose the x-ticks in your question (Jan1971,Jan 1972,...Dec 1986) but here's how to show every January. You can adapt the rule as needed.
t is your datetime vector
h is the handle to your heatmap object.
% Show every January
idx = month(t)==1; % index of datetime values to show as x tick
h.XDisplayLabels(~idx) = {''}; % replace rejected tick labels with empties
  댓글 수: 6
Rt Ro
Rt Ro 2020년 2월 13일
Thank you for your help.
it works perfectly :).
Ayobami Olanrewaju
Ayobami Olanrewaju 2021년 2월 9일
Thank you, the XDisplayLabels and YDisplayLabels works like magic.

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

추가 답변 (2개)

Rt Ro
Rt Ro 2020년 2월 12일
Thank you for your reply.
I did it but it still shows the x-axis values as monthly.
could you please help me?

Juan Fernández Blasco
Juan Fernández Blasco 2020년 4월 20일
Any of you did face the problem of rotating the x axis values in a heatmap?
I am trying the following:
h2 = heatmap(tsT,stress_attributes,stress_matrix(:,1:272),'TickAngle',45);
Basically, 'TickAngle',45 because I saw it in a post, but I got this error:
The name 'TickAngle' is not an accessible property for an instance of class 'matlab.graphics.chart.HeatmapChart'.
I also tried without succeeding the following:
h2 = heatmap(tsT,stress_attributes,stress_matrix(:,1:272),[],'TickAngle',45);
  댓글 수: 1
Adam Danz
Adam Danz 2020년 4월 20일
편집: Adam Danz 2020년 4월 21일
Heatmaps are notoriously difficult to customize due to inaccessible graphics handles. A workaround is to remove the tick labels completely, overlay an invisible axes the same size as the heatmap, and add the customizable tick labels to the new axes.
If you have any problems applying the demo to your needs show me your attempt and I can help straighten things out.

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

카테고리

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