Turbo
Turbo

Reputation: 73

Color coded 2D plot in MATLAB

I have a d1×d2 array A of integer values and want to color code display them on the xy-plane in a rectangle scaled to d1×d2 using colors that match the magnitude of the value of array at that location. I also want to display the color chart which indicates which color is which magnitude as in the following figure:-
Fig 3 of http://home.deib.polimi.it/tubaro/Conferences/2013_WASPAA_2.pdf

Is there a simple code that can do this?
Or is this kind of charting need special packages?


Will this work ('A' is matrix with non-negative entries)?

function plot2Ddistprf(A, Length, Breadth)

Amax=max(A(:));
A=A/Amax;

G1 = linspace(0,Length,size(A,1));
G2 = linspace(0,Breadth,size(A,2));

[X,Y] = meshgrid(G1,G2);

% plot data
figure;                     % create a new figure
contourf(X,Y,A);            % plot data as surface
caxis([1,100]);            % set limits of colormap
colorbar;                   % display the colorbar

Upvotes: 2

Views: 426

Answers (2)

Claes Rolen
Claes Rolen

Reputation: 1466

Try the contourf function and then add the colorbar

 contourf(A)
 colorbar

Upvotes: 1

Matt
Matt

Reputation: 13923

No external library or special package is needed to create such a plot. You can use contourf to plot the data. Then, set the colormap to gray. With caxis you can control the range of colors. colorbar shows the bar on the right side.

The result looks like this:

result

Here is the code:

% generate sample data
d1 = linspace(-3,3,200);
d2 = linspace(-3,3,200);
[X,Y] = meshgrid(d1,d2);
A = -abs(peaks(X,Y))+100;

% plot data
figure;                     % create a new figure
contourf(X,Y,A);            % plot data as surface

colormap(gray);             % use gray colormap
caxis([91,100]);            % set limits of colormap
colorbar;                   % display the colorbar

title('The Title');
xlabel('y');
ylabel('x');

Upvotes: 1

Related Questions