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');
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...
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'
Part 5: Image:
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:
scatter(x,y,[],z,'filled')
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?
mask = yi>(314 + 100*xi);
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:
imagesc(xx,yy,zi,'AlphaData',mask)
caxis([min(zi(mask)) max(zi(mask))])
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.