Heatmap labels based on another matrix

Hi
Im trying to get the heatmap function to work with my data. I want to show a different matrix of data as 'text' labels (numbers) on top of my heatmap, but I can't find how to link the new matrix to the heatmap function. I already found the following tutorial, but it doesnt work. It doesnt know 'labels_small'.
I know I have to convert the matrix to a cell with strings:
% matrix for heatmap [2x6]
thermala;
% additional matrix for 'text' labels
c_small = [1 2 3 4 5 6;
1 2 3 4 5 6];
c_label = sprintfc('%d',c_small);
heatmap(thermala);
Thank you in advance.

 Accepted Answer

dpb
dpb on 3 Apr 2018
See the examples; you didn't call it with anything excepting the data array so it can't "know" anything about labels...

17 Comments

-Joris Answer moved to Comment--dpb-
Thanks. Yes I know. I tried the format from the tutorial but it doesn’t know the expression labels_small. I want to know what I have to add to the function heatmap() to call the new matrix.
One example syntax is:
heatmap(spreads_small, labels_small, labels_small, '$%0.2f', 'TickAngle', 45);
All the arguments are arrays either from the input after executing
load heatmapData
or created therefrom.
You didn't include anything in the call excepting thermala in anything you've shown. You'll have to create variables for each input argument you want to use and pass them as do the examples.
I suspect if you go back and work through the examples sequentially as they're given you'll get a better understanding of how it works; I've never seen it before now but I'm certain each of those arguments is an input array and if you build and pass the equivalent it'll probably work as advertised.
I totally agree with you. Besides the fact I already went through the tutorial. I understand I have to create all the variables for each input argument, but I don't fully understand the format of the labels.
%%Heatmap tutorial
heatmapData = [1 2 3 4 5 6;
6 5 4 3 2 1];
labels = [1 2 3 4 5 6;
6 5 4 3 2 1];
labels_small = sprintfc('%d',labels);
clf
heatmap(heatmapData,1:6,labels_small)
After I get this error code:
Error using heatmap (line 53)
Arguments must be a table followed by 2 table subscripts, a 2-dimensional numeric matrix, or 2 vectors followed by a 2-dimensional numeric matrix.
Error in heatmap_tutorial (line 12)
heatmap(heatmapData,1:6,labels_small)__
What am I doing wrong?
That's an array followed by a vector and a 2D character array...
Huh. I thunk before it was a FileExchange submittal; now I see there was a heatmap function introduced in R2017 (which I've not installed as yet). Is this what you've got?
Perhaps reading the direct doc may make more sense than the help...I see it is table -aware as well as data array.
What does
which heatmap
return on your system; which release?
I created a heatmap based on a matrix, not a table. And that should be supported (see direct doc.).
I've installed Matlab R2018a.
Oh...thanks, TMW! :(
Reading the doc more closely, they reordered inputs for array data as opposed to table--
h = heatmap(tbl,xvar,yvar)
h = heatmap(tbl,xvar,yvar,'ColorVariable',cvar)
the tbl is first but for cdata,
h = heatmap(xvalues,yvalues,cdata)
if use more than one input argument. That's terrible UI design; I've noted they have become more and more prone to such foopahs over the last few years that nobody is paying attention to such during design reviews it appears in the mad rush to introduce new features every rollout. :(
>> which heatmap -all
/Applications/MATLAB_R2018a.app/toolbox/matlab/specgraph/heatmap.m
>>
See my amended on arguments order...comments passed in the ether and I edited previous when I realized what was going on.
Thanks for your reply. I made some progress. Managed to plot the heatmap with x- and y-axis labels using the correct format :-)
h = heatmap(xvalues,yvalues,heatmapData);
It is indeed not logical the format is so different from the heatmap based on a table. Still, it is not clear for me what format is used for text labels when using a matrix. I assume it is similar to the format of the axis labels?
Which text labels do you mean? There's a link to the complete set of heatmap object properties that takes you to <heatmapchart-properties> that describes every available property.
I am trying to mimic this passage from the tutorial:
A completely different matrix of data can be shown as text labels on top of the original heatmap, enabling you to overlay another dataset
% Convert matrix c_small into a cell array of formatted strings
clabel = arrayfun(@(x){sprintf('%0.2f',x)}, c_small);
clf
heatmap(spreads_small, labels_small, labels_small, clabel, 'TickAngle', 45);
Can only guess; that syntax with four arguments other than named ones isn't shown in the documentation. The previous examples all seemed to be using the table which had to have the data array first while you're using an array in which it is in the third position; you can try adding it as
heatmap(x,y,data,labels)
or
heatmap(x,y,labels,data)
One guess, maybe they haven't done enough testing for cases other than using a table object and it just won't work with an array; it is a very convoluted set of inputs to parse; wouldn't surprise me a bit if there are still bugs what with it being so new.
Only other suggestion I'd have would be to convert your array to a table and try that route mimicking the example entirely in form but only with different data and see if that works.
Other than that, try reading the m-file and see if there are enough comments in the input parsing to figure out the order in which those inputs are expected if, indeed, the case is handled for array input.
Failing any/all of those, submit a bug report/service request for support.
Many thanks for your help. I will trial-and-error some more :-) Heatmap (in this stage) seems more like a beta-like feature.
Are you sure you do have the supplied version in R2018 and not the FEX version I also first found per Walter's comment? That could be a difference if so.
Ah I see now... In the Mathworks version I don't see the option with text labels on top of a Heatmap listed. So I think the option is exclusively for FEX? Anyhow, I found another way to display the information I initially wanted to.
Probably the difference, yes. You could probably have both if you were to make a slight rename of the FEX version or explicitly call the one desired if it has features you'd like...or, I presume you just text ed over the existing which is probably almost as simple.
@Joris Puttenstein I would like to know how you finally managed to display information on the heatmap.
Thnak you in advance

Sign in to comment.

More Answers (1)

The heatmap function you are looking at is a file exchange contribution, not the same as the Mathworks heatmap function.

Categories

Community Treasure Hunt

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

Start Hunting!