Reputation: 469
Consider the example shown in the following figure,
The first subplot shows 3-axis accelerometer data(not relevant, just an example!) and the second plot shows the corresponding computed body postures in a pcolor plot.
The problem here is there are 6 possible postures, but not all the postures will be covered for a given accelerometer dataset. We may have 3,4,5 or 6 postures and that many colors in the pcolor plot depending on the dataset. In this case how can we define the legend since the number of colors are not fixed?
In any case, even with prior knowledge of the number of postures the pcolor plot is displaying only one legend entry(see subplot 2 in above figure) with the following code:
subplot(2,1,2);
%sleep_status(1:6) = 0:5;
h=pcolor([sleep_status; sleep_status]), shading flat, colormap(jet(6))
legend(h,{'Unknown','Upright','Lying Left','Lying Right','Lying on Chest','Lying on back'})
set(gca, 'YTickLabel', [])
ylabel('Sleep State')
axis tight
Upvotes: 0
Views: 3012
Reputation: 13945
I don't know how to adjust the legend, but what about using a colorbar?
For instance, the code taken from here centers YTickLabels
along the colorbar. Modifying it to fit your data, that would look like this:
Here is the whole code:
clear
clc
%// Test data
sleep_status = [randi([1,10],1,12)];
h=pcolor([sleep_status; sleep_status]), shading flat, colormap(jet(6));
set(gca, 'YTickLabel', [])
ylabel('Sleep State')
axis tight
%// Place label at the center.
%// Source: http://www.mathworks.com/matlabcentral/answers/102056-how-can-i-make-the-ticks-in-the-colorbar-appear-at-the-center-of-each-color-in-matlab-7-0-r14
numcolors = 6;
caxis([1 numcolors]);
cbarHandle = colorbar('YTick',...
[1+0.5*(numcolors-1)/numcolors:(numcolors-1)/numcolors:numcolors],...
'YTickLabel',{'Unknown','Upright','Lying Left','Lying Right','Lying on Chest','Lying on back'}, 'YLim', [1 numcolors]);
Hope that helps!
Upvotes: 1