Is there way to make heatmap with 3variabls?

4 views (last 30 days)
I have 3 dates and want to make the heatmap with 3datas.
z is normalized so that the maximum is 1 and the minimum is 1.
Does anyone know how to draw a heatmap with 3 variables?
Q =csvread('Q_10_1.csv');
Data=csvread('State_10_1.csv');
x = Data(:, 1);
y = Data(:, 2);
z = Q;
zmin = min(z);
zmax = max(z);
for i=1:length(z)
Z(i) = round((z(i) - zmin)/(zmax -zmin),5);
end
imagesc(x,y,Z)
colormap hot
colorbar

Accepted Answer

Dave B
Dave B on 13 Nov 2021
Edited: Dave B on 13 Nov 2021
To make an image like this, you need a matrix z which provides a point for each x and y.
That's pretty easy to generate with interpolation, but there are a few challenges with your data:
  • x and y are a different size from z. It's unclear how to match up 10000 points with 10002 points.
  • the data are very differently distrubuted. You have some dense regions and then some very sparse regions. When data aren't very grid-like, it can be difficult to make a grid-like visualization (like a heatmap)
I'll also make a few minor adjustments to your code:
  • the loop is unnecessary
  • As the csvread documentation page suggests: csvread is not recommended. Use readmatrix instead.
Part 1: read in data
Q = readmatrix('Q_10.csv');
Data = readmatrix('State_10.csv');
x = Data(:, 1);
y = Data(:, 2);
z = Q;
z = round(z-min(z)/(max(z)-min(z)),5);
Part 2: trim off 2 values from x and y...totally unclear which ones should go and it'll make a big difference. I'm arbitrarily taking the last two values...
x = x(1:10000);
y = y(1:10000);
Part 3: Make a grid of 200 linearly space values spanning x and y:
xx = linspace(min(x),max(x),200);
yy = linspace(min(y),max(y),200);
[xi,yi] = meshgrid(xx,yy);
Part 4: Interpolate
f = scatteredInterpolant(x,y,z)
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
f =
scatteredInterpolant with properties: Points: [8905×2 double] Values: [8905×1 double] Method: 'linear' ExtrapolationMethod: 'linear'
zi=f(xi,yi);
Part 5: Image:
imagesc(xx,yy,zi)
colormap hot
% some optional display tweaks:
colorbar
caxis([-50 50])
axis xy
Note that interpolation got really strange in the top right. You have a triangle of empty data which poses a problem for interpolation. Let's see what the raw data look like to understand what happened:
figure
scatter(x,y,[],z,'filled')
colormap hot
We can inspect these points and see that there's a line there...you don't have any data to the right of that line. How about if we ignored points to the right of that line?
figure
mask = yi>(314 + 100*xi); % I made up an equation for the line based on inspecting the scatter
imagesc(mask)
colormap gray
axis xy
There are a few options for how to use the mask. I'm going to just use it as transparency data (using the AlphaData property on Image), so that the ignored data are transparent, and then set the color limits to the min and max of the included data:
figure
imagesc(xx,yy,zi,'AlphaData',mask)
caxis([min(zi(mask)) max(zi(mask))])
axis xy
colormap hot
Note that we could use a similar strategy to ignore the missing data at the top...you'd just have to define what that region is.

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!