heatmap but with 2 values, one has positive and negative value (positive value = blue, negative value = red) and one is pvalue (the color graduates) in matlab

One way to do this is to use the heatmap function in combination with the colormap function in MATLAB. Here's an example code:

main.m
% Generate sample data
data = [randn(50, 50)*2 + 1, randn(50, 50)*2 - 1]; % Positive and negative values
pvalue = abs(randn(50, 50)*0.1); % P values

% Create a heatmap with positive/negative values
heatmap(data, 'Colormap', flipud(diverging_map(10)), 'ColorLimits', [-6, 6]);

% Add pvalue to the heatmap
hold on;
[imageData, xData, yData] = heatmap(pvalue, 'Colormap', colormap('pink'), 'ColorbarVisible', 'off');
cdata = imageData.CData;
cdata(cdata ~= 0) = nan;
set(imageData, 'AlphaData', double(isnan(cdata)));
set(imageData, 'XData', xData, 'YData', yData);
565 chars
15 lines

The diverging_map function is a custom function to create a colormap that goes from blue (positive values) to red (negative values). Here's the code for that function as well:

main.m
function cmap = diverging_map(n)
% Create a colormap with n colors going from blue to red (RGB)
% by Fabrizio Conti (2021)

if nargin < 1
    n = 64;
end

cmap = zeros(n, 3);

for i = 1:n
    t = (i-1)/(n-1);
    r = 1 - max(0, (t - 0.5) * 2);
    b = max(0, (t - 0.5) * 2);
    g = 1 - (r+b);
    cmap(i, :) = [r, g, b];
end
end
330 chars
19 lines

This should create a heatmap with positive and negative values where positive values are blue and negative values are red, and the color of each cell graduates based on the pvalue.

gistlibby LogSnag