heatmap with categorical color scale and quantitative color scale in matlab

To create a heatmap with both a categorical color scale and a quantitative color scale in MATLAB:

  1. Load your data into MATLAB and convert it to a matrix format.
  2. Determine which variable you want to represent on the horizontal axis and which one you want to represent on the vertical axis.
  3. Create a heatmap using the heatmap function in MATLAB.
  4. Use the colormap function to set a color-mapping function for both the categorical and quantitative scales.
  5. Use the Colorbar function to add a color bar that indicates the scale of each color.

Here is an example code snippet to create a heatmap with categorical color scale and quantitative color scale in MATLAB:

main.m
% load data
load carbig
x = [Cylinders, Horsepower, Weight, MPG];

% create categorical variable
[counts, centers] = hist(x(:,1),3);
cylinders = discretize(x(:,1), centers, {'2', '4', '6'});

% create quantitative color scale
cm = colormap(jet(size(x,1)));
cmap = flipud(cm);
color_idx = ceil((x(:,2) - min(x(:,2))) ./ (max(x(:,2)) - min(x(:,2))) * size(cmap, 1));
color_idx(color_idx < 1) = 1;

% create heatmap
heatmap(cylinders, x(:,3), x(:,4), ...
    'Colormap', cmap, ...
    'ColorMethod', 'quantile', ...
    'ColorData', color_idx);
colorbar

% label axis
xlabel('Cylinders')
ylabel('Weight')
602 chars
25 lines

In this example, the categorical variable is the number of cylinders (converted to a categorical variable with only three categories), and the quantitative variable is the horsepower. The weight represents the vertical axis, and MPG represents the data values.

The jet colormap is used to map the quantitative color data, and the discrete color categories are defined using a separate categorical variable.

The heatmap function is used to create the heatmap, with the ColorMethod set to 'quantile', which allows the heatmap to use the full range of the colormap. The ColorData parameter is used to specify the colors for each value in the heatmap based on the quantitative data. The colorbar function is used to show the color scale. The xlabel and ylabel functions are used to label the x-axis and y-axis, respectively.

gistlibby LogSnag