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
0 件のコメント
採用された回答
0 件のコメント
その他の回答 (1 件)
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.
0 件のコメント
参考
カテゴリ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!