So I wanna to make a 3x3 heatmap with all cells with a value of 1. I want the center grid to be a totally different color than the other grids without changing it's value

48 visualizaciones (últimos 30 días)
So I want to make a heatmap for a ones(3,3) matrix and then I want to color the center grid to green without changing its value
matrix = ones(3,3);
heatmap(matrix);
I want to color the center grid (matrix(2,2)) a differnet color (green or something) when plotting the heatmap

Respuesta aceptada

Epsilon
Epsilon el 14 de Nov. de 2024 a las 4:21
Hi Haoyan,
It might not be possible to create a 'heatmap' with a different central cell color and same values displayed at the same time. It is not even possible to overlay two different heatmaps and modify them for a similar effect as heatmap does not support 'hold'.
A workaround could be to create the whole plot to replicate a heatmap. Attaching a code as an example for the same:
data = [1, 1, 1;
1, 1, 1;
1, 1, 1];
data2 = [0, 0, 0;
0, 1, 0;
0, 0, 0];
% Create the plot
figure;
imagesc(data2);
colormap(jet);
axis equal;
axis tight;
xlabel('X-axis Label');
ylabel('Y-axis Label');
xticks(1:size(data, 2));
yticks(1:size(data, 1));
xticklabels({'1', '2', '3'});
yticklabels({'1', '2', '3'});
title('Custom 3x3 Heatmap with Grid Lines');
% Add grid lines
hold on;
for k = 0.5:size(data, 1)+0.5
line([0.5, size(data, 2)+0.5], [k, k], 'Color', 'k', 'LineWidth', 1);
end
for k = 0.5:size(data, 2)+0.5
line([k, k], [0.5, size(data, 1)+0.5], 'Color', 'k', 'LineWidth', 1);
end
% Display numbers in the center of each cell
for i = 1:size(data, 1)
for j = 1:size(data, 2)
text(j, i, num2str(data(i, j)), 'Color', 'white', ...
'FontWeight', 'bold', 'HorizontalAlignment', 'center');
end
end
Hope it helps!

Más respuestas (1)

Umar
Umar el 14 de Nov. de 2024 a las 3:05

Hi @Haoyan,

To create a heatmap for a 3 times 3 matrix filled with ones, while specifically coloring the center cell (which corresponds to the matrix element at position (2,2) green without altering its value, you can achieve this using MATLAB's heatmap functionality. Below is the complete code that adheres to your requirements, utilizing direct property manipulation of the heatmap object.

% Step 1: Create a 3x3 matrix of ones
matrix = ones(3,3);
% Step 2: Create the heatmap
h = heatmap(matrix);
% Step 3: Customize the heatmap
h.Colormap = parula; % Set a colormap (default is parula)
% Step 4: Modify color data to highlight the center cell
% Set the color data directly for all cells (as an example, we can use indices)
colorData = ones(size(matrix)); % Start with all ones
% Assign a different value for the center cell to make it green
colorData(2,2) = 0; % This will correspond to the first color in the parula     colormap
% Update color data of the heatmap
h.ColorData = colorData;
% Step 5: Change specific cell colors by modifying the ColorData property
h.CellLabelColor = 'none'; % Hide cell labels if needed
% Manually specify colors for center cell by setting its color directly
h.ColorData(2,2) = 0; % Keep it zero to use green color from parula
% Displaying the heatmap with center cell colored differently
title('Heatmap with Center Cell Highlighted');

Please see attached.

So, in the above code, ones(3,3) function creates a 3 times 3 matrix filled with ones. The heatmap(matrix) function generates a heatmap based on the provided matrix. The Colormap property is set to parula, which is a built-in colormap in MATLAB. A new variable colorData is created, initialized to all ones. The center cell's value is changed to zero. This step ensures that it uses the first color from the colormap (which is typically green in many colormaps). The CellLabelColor property is set to none if you want to hide cell labels for clarity. Now, choice of color for specific cells relies heavily on how MATLAB maps values in your data matrix to colors in the selected colormap. In this case, values closer to zero correspond to colors like green in the parula colormap. Also, you can further customize your heatmap by adjusting properties such as titles, axis labels, and more using other properties of the HeatmapChart object (h). This code should successfully create a heatmap as per your specifications, highlighting only the center grid without changing its value.

Hope this helps.

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by