Heatmap Axis Labels Printing Vertically?

Quick question, the x-labels on my heatmap are showing up vertically. I can't find the reason for this. This is what I have to generate the figure
x1 = linspace(17,23.5,14)
x2 = linspace(0,16.5,34)
xx = [x1 x2]
subplot('Position',pos1);
heatmap(xx, snow_height,snow11, 'Colormap', gray)
ax = gca;
tmp = ax.XDisplayLabels;
tmp(setdiff(1:end,1:6:end)) = {''};
ax.XDisplayLabels = tmp;
and this is what is showing up. How can I get the numbers oriented properly (horizontally) on the x-axis?

2 Kommentare

dpb
dpb am 27 Jun. 2020
" the x-labels on my heatmap are showing up vertically. I can't find the reason for this..."
The reason is that there are so many initially before you thin them out it tries to make fit as best it can.
Try commenting out the lines after the call to heatmap to see what looks like before...
Camille Woicekowski
Camille Woicekowski am 28 Jun. 2020
Right, that's why I added those additional lines because it was completelly illegible before since there were far too many values. So, once I have it displaying as is, is there a way to rotate the labels? I need all the columns of data displayed, and heat map requires the number of x labels (yes, labels, not just values) to be the same as the number of columns mapped, so I haven't been able to find another option for this.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Yair Altman
Yair Altman am 29 Jun. 2020

2 Stimmen

The heatmap's X labels orientation is dynamically determined based on the number of labels and the axes width. In other words, if you make the axes wider [for example by enlarging the figure window], the X labels may change orientation from vertical to horizontal.
You can control the labes orientation programmatically as follows (undocumented):
h = heatmap(...);
s = struct(h);
s.XAxis.TickLabelRotation = 90; % vertical
s.XAxis.TickLabelRotation = 60; % angled
s.XAxis.TickLabelRotation = 0; % horizontal
Yair Altman

1 Kommentar

dpb
dpb am 29 Jun. 2020
That's the ticket with struct! I was unaware of being able to do that.
Same thing in the end a little more directly; problem now is OP says has no effect on her plot.
But so far she hasn't provided full working example that illustrates the problem on her system to verify here.

Melden Sie sich an, um zu kommentieren.

dpb
dpb am 26 Jun. 2020

0 Stimmen

Oh, TMW has done it again -- they've buried stuff inside opaque (to the user) objects that should be visible. I wish they would quit doing this... :(
If you don't have it, go to FEX and download Yair Altman's utility for finding hidden objects...it's the only tool readily available for such.
From it
>> hHM=heatmap(xx,hh,ss)
hHM =
HeatmapChart with properties:
XData: {48×1 cell}
YData: {100×1 cell}
ColorData: [100×48 double]
Show all properties
>> undocumented(hHM)
ans =
struct with fields:
ApplicationData: [1×1 struct]
Axes: '???'
...
DefaultPropMap_Internal: [0×0 struct]
Description: 'HeatmapChart'
DescriptionMode: 'manual'
Description_I: 'HeatmapChart'
...
MissingDataValue: '???'
NodeChildren: [3×1 Graphics]
NodeParent: [1×1 ScribeLayer]
NotUsingTableForData: '???'
...
>>
Ah!! That looks interesting--let's see what we can tell about it...
>> hHM.NodeChildren
ans =
3×1 graphics array:
ColorBar
ColorBar
Axes
>>
And we were able to find an axes object.
>> hAx=hHM.NodeChildren(3)
hAX =
Axes with properties:
XLim: [17 16.5]
YLim: [0 1]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [0.1300 0.1100 0.7179 0.8150]
Units: 'normalized'
...
>>
Under it we can find and set the tick label properties including rotation...
hAx.XAxis.TickLabelRotation=0;
IMO, there's absolutely no excuse for TMW having done such stuff.

12 Kommentare

Camille Woicekowski
Camille Woicekowski am 28 Jun. 2020
I tried adding hAx.XAxis.TickLableRotation=0; and it did not give an error message but the labels are still printing vertically. I tried different rotation values but still no change. Do I need any aditional commands?
dpb
dpb am 28 Jun. 2020
Bearbeitet: dpb am 28 Jun. 2020
You have to retrieve the handle to the axis...to do that you have to follow the steps I outlined above of first saving the handle of the HM object, then retrieving the axis handle from the NodeChildren array and then using that axis handle...
Using your variables above, it would be something like
hHM=heatmap(xx, snow_height,snow11, 'Colormap', gray);
hAx=hHM.NodeChildren(3);
hAx.XAxis.TickLabelRotation=0;
If you just used gca or tried to just set property of current axis, that's the problem--TMW has hidden the axis handle you need and gca will only return the HM graphics object instead. If you didn't retrieve the handle first, the above code will have created a struct variable hAx with fields as given and assigned it the value zero. That's why it didn't error; it's still valid MATLAB code syntax, just not at all what is intended.
So, I tried this, and still no change..
I have
hHM = heatmap(xx, snow_height,snow11, 'Colormap', gray);
ax = gca;
hAx=hHM.NodeChildren(3);
hAx.XAxis.TickLabelRotation = 0;
tmp = ax.XDisplayLabels;
tmp(setdiff(1:end,1:6:end)) = {''};
ax.XDisplayLabels = tmp;
I tried plaving the TickLabelRotation both before and after ax.XDisplayLabels and neither worked.
hHM = heatmap(xx, snow_height,snow11, 'Colormap', gray);
hAx=hHM.NodeChildren(3);
tmp = ax.XDisplayLabels;
tmp(setdiff(1:end,1:6:end)) = {''};
ax.XDisplayLabels = tmp;
hAx.XAxis.TickLabelRotation = 0;
I'd expect to work...did here, but with just a facsimile of your dataset.
Attach the .mat file with the data.
What does
hAx.XAxis
return at command line after the assignment line?
Better yet, what is
hHM.NodeChildren
?
From the HEATMAP() example--
cdata = [45 60 32; 43 54 76; 32 94 68; 23 95 58];
hHM = heatmap(cdata);
>> hHM.NodeChildren % the last is indeed the axes object
ans =
3×1 graphics array:
ColorBar
ColorBar
Axes
>> hAx=hHM.NodeChildren(3); % save a handle to it
>> hAx.XAxis.TickLabelRotation % see the current value...
ans =
0
>> hAx.XAxis.TickLabelRotation=90; % and rotate to 90...
Above yields
that shows the 90 rotation...
>> hAx.XAxis.TickLabelRotation
ans =
90
>>
shows did set the property and value reflects having done so...
>> hAx.TickLabelRotation
ans =
90
But setting it equal to zero doesn't change anything. I'm wondering if it's not possible because of the size of my data?
dpb
dpb am 29 Jun. 2020
No. I did it with a sample of the same size here with no trouble.
You didn't respond with any of the info asked for nor attach the dataset so not much else can do from here.
Oh...one other. Which release of MATLAB and OS are you using? The above was done w/ R2019b here on Win platform.
OK, here's what I did before to do the handle-diving exercise to find the axes handle...
x1 = linspace(17,23.5,14)
x2 = linspace(0,16.5,34)
xx = [x1 x2]
hh=linspace(0,1);
ss=rand(numel(hh),numel(xx));
subplot(2,1,1)
hHM1=heatmap(xx,hh,ss);
tmp = hHM1.XDisplayLabels;
tmp(setdiff(1:end,1:6:end)) = {''};
hHM1.XDisplayLabels = tmp;
From there I did precisely the same thing for a second subplot
subplot(2,1,2)
hHM2=heatmap(xx,hh,ss);
hHM2.XDisplayLabels = tmp;
hAx=hHM2.NodeChildren(3);
hAx.XAxis.TickLabelRotation=0;
As shown, the top subplot is rotated, the bottom isn't -- like the top, it was until change TickLabelRotation property.
The difference here is there are 100 points in y so those ticks are also too thick and I just used random values for the data since didn't have the real thing. But, I don't think that's the problem -- there's something else going on you're not showing or you have a version difference, maybe.
The info asked for would perhaps show what that might be -- a complete example from scratch attached with the data would be the definitive response.
dpb
dpb am 29 Jun. 2020
On version, I went back to R2017b; 'TickLabelRotation' works there same way as well as the axis being the last element in NodeChildren array.
Camille Woicekowski
Camille Woicekowski am 29 Jun. 2020
Well, this is for work and it's not a public dataset, so I'm not comfortable uploading it on a public platform. I do appreciate the help though.
I'm using version R2020a
dpb
dpb am 29 Jun. 2020
Just make a sample test case, then.
It shouldn't matter what the data are -- if you can't reproduce the above figure, then it would begin to appear you have uncovered a bug or have a system-related issue regarding the renderer and/or video driver...altho that would seem highly unlikely the cause of this symptom.
Attach a working sample script that has a problem there and I'll see if it reproduces issue here...
dpb
dpb am 29 Jun. 2020
Or, my address is at contact site to send privately...
But, also, you never responded to the other Q? of what various whos or other queries at command line show on your system...

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2020a

Gefragt:

am 25 Jun. 2020

Kommentiert:

dpb
am 29 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by