Xtick Notes and Xtick Labels on Heatmap in Matlab

Environment: Windows 7 64 bit, Matlab 2014a

Purpose:

  • Draw a heatmap of errors to optimize two parameters.
  • To set the correct tick marks and values
  • Draw the grid lines in the correct place.

The task: to order the positions and values ​​of the X and Y position. When the last value ("end") of the vectors along the x and y axes is the same, the code that I am using correctly sets the ticks and values. However, when the final values ​​are different it doesn't do it and creates something really weird.

Below I have included the code that I modified so that you can run it without having to add anything. Of course, in my case the error vector is error values, not random numbers. To see the "end value" problem, use the second vector b.

fontsize = 20
k = [2^-5, 2^-3, 2^-1, 2^0, 2^1, 2^3, 2^5, 2^7, 2^9, 2^11, 2^13, 2^15]
b = [2^-5, 2^-3, 2^-1, 2^0, 2^1, 2^3, 2^5, 2^7, 2^8, 2^9, 2^10,  2^11, 2^13, 2^15]
% b = [2^-5, 2^-3, 2^-1, 2^0, 2^1, 2^3, 2^5, 2^7, 2^8, 2^9, 2^10,  2^11, 2^13, 2^19]

errorVector = randi(20, 1, length(b)*length(k))'
figure
% Create a matrix from error vector (size of error vector is [length(k)*length(b),1])
B = reshape(errorVector, [length(b), length(k)])
B = flipud(B)

% imagesc(x,y,C)
imagesc(b, k, B)
title('Heatmap Parameters Percent Error', 'FontSize', fontsize);

% Set colorbar limits
caxis([0 15])
colorbar;

ax1 = gca;

xTickLabel = (k)'
xTick = linspace(k(1), k(end), numel(xTickLabel))';
set(ax1, 'XTick', xTick, 'XTickLabel', xTickLabel)
xlabel('k parameter', 'FontSize', fontsize)

yTickLabel = (b)'
yTick = linspace(b(1), b(end), numel(yTickLabel))';
set(ax1, 'YTick', yTick, 'YTickLabel', flipud(yTickLabel(:)))
ylabel('b parameter', 'FontSize', fontsize)

set(ax1,'FontSize', fontsize)

      

Here, change any of the final values ​​of the vectors b or k, and the program will display a graph where the X and Y checkboxes are completely wrong.

Also, I would like to draw grid lines. When I use a "grid" it draws grid lines right at the elevations which are incorrect in the case of a heatmap. Because the label marks will be in the center of the columns or rows, the gridlines must be at the boundaries between the columns or rows.

By the way, if you know the best way to build a plot map in Matlab, please tell me.

Please help me to solve this problem. Any help is appreciated,

Ilyas

+3


source to share


1 answer


First of all - you don't need a flipud

matrix B

- by default it imagesc

displays inverse y-data, but you can fix that with the following statement:

set(gca,'ydir','normal');

      

This also means you don't have to fiddle with flipping the labels. You can get the labels on the right by doing the following:

% replace the imagesc call with:
imagesc(B);
set(gca,'ydir','normal');

% formatting stuff
...

% replace the set commands with:
set(ax1, 'XTick', 1:length(k), 'XTickLabel', k)
set(ax1, 'YTick', 1:length(b), 'YTickLabel', b)

      

By default, if you do not supply x and y data to a command imagesc

, they will linearly number them (1,2,3 ...). Basically what we've done here is make sure it has labels for each of the elements B

and k

then set labels to the values ​​of the corresponding vectors.



Unfortunately I'm not sure if there is a way to get the grid using imagesc

or not. You could try using instead pcolor

, which has its own set of problems, but allows you to get mesh lines (sorts) between elements. It also allows for an interpolated shading mode that will make your subject look more like a typical heat map.

Instead of using, pcolor

you just need to replace imagesc

:

% imagesc(B);
% set(gca,'ydir','normal');
pcolor(B);
% this uses a smoother shading method, for a more 'heatmap' like output
% shading interp

      

Everything else should work as intended, I suppose.

+2


source







All Articles