1
votes

I would like to plot a sort of "density map" in Matlab, but have not found the right tool yet.

I have "continuous" data with x between (x_min and x_max), and y between (y_min and y_max). At each of these pairs of points (x_i,y_i), there is associated to it a value between 0 and 1.

I would like to plot this information in a 2d graph, such that in each small square containing (x_i,y_i) the plot shades the square black for the value 0, white for the value 1, and the appropriate shade of gray for intermediate values.

Can this be done easily in Matlab?


http://www.mathworks.com/help/images/ref/mat2gray.html seems to do exactly what I need.

3
you mean a heat map ? - Jason S

3 Answers

3
votes

If the data is in a matrix A, you can just use

image(255*A); colormap gray(256); axis image;
1
votes

I'm not sure what you mean by continuous (uniformly spaced?), so my answer won't make too many assumptions other than that there is a reason why you mention the coordinates (if just a regular mesh, then just image or imagesc). So, only assuming your x and y coordinates are possibly non-uniformly spaced, but at least monotonically increasing samples, try surf with view(2):

surf(X,Y,data)
view(2)
colormap gray

By default surf sets the FaceColor property with the 'flat' option:

flat — The values of CData determine the color for each face of the surface. The color data at the first vertex determine the color of the entire face.

In other words, the value will determine the shade.

0
votes

Assuming your data is in data and your x and y coordinates are in x and y, here is how to do it:

imagesc(x, y, data) % to create a heat map
colormap(gray) % for gray levels
caxis([0 1]) % to set 0 to black and 1 to white
axis xy % if you want the y axis to point up
colorbar % to display the colorbar