1
votes

This is my data matrix in MATLAB:

a = [43.676289  -79.477386  1
     43.676370  -79.477107  5
     43.676517  -79.477375  20
     43.676417  -79.477509  8
     43.676129  -79.477278  15];

The first column is Y axis, the second column is X axis and the third column is my data. How can I draw a bar graph, and adjust the color of the bars according to the value of data (like colorbar in a surface plot) for each data point in MATLAB?

I added an example graph which I drew for another data matrix. In this example X, Y, and Z were linear and I could draw this graph using 'surf' command with no problem. I need to draw the same graph for mentioned data, but the unit of the XY axis is not compatible with Z, and this confused me.

Example graph

Just as an additional comment, if we plot only the XY plane, the result looks like the next picture:

scatter(a(:,2),a(:,1),'*')

XY PLANE ONLY

Moreover, this is a simple example that might be useful to expand it:

z = [5     0     2     0
     0     0     0     0
     0     0     0     0
     0     0     0     0];
[X,Y] = meshgrid(0:1:3);
surf(X,Y,Z)

Thanks

1
It would be helpful if you showed us the code/data from the plot which worked.Wolfie
Sorry @Wolfie i didn't know how to reply here so I edited the question by adding more information. About the code which worked, that is a simple 'meshgrid' command and 'surf' plot as you can see. Thanksuser8296976
Editing the question is the correct way to add more information. Keep code formatting simple and just use indentations - you cannot run MATLAB code snippets in the browser!Wolfie

1 Answers

1
votes

Here is something you can do - build Z as a matrix from your data:

a = [43.676289   -79.477386  1
     43.676370   -79.477107  5
     43.676517   -79.477375  20
     43.676417   -79.477509  8
     43.676129   -79.477278  15];
[X,Y] = meshgrid(sort(a(:,2)),sort(a(:,1)));
Z = zeros(size(X));
for k = 1:size(a,1)
    xind = abs(X-a(k,2))<eps;
    yind = abs(Y-a(k,1))<eps;
    Z(xind & yind) = a(k,3);
end

Typing surf(X,Y,Z) will give you this:

enter image description here

However, I think that bar3 might be a better choice:

b = bar3(sort(a(:,1)),Z);
xticklabels(sort(a(:,2)));
cdata_sz = size(b(1).CData);
z_color = repelem(Z,6,4);
z_color(abs(z_color)<1) = nan;
z_color = mat2cell(z_color,...
    cdata_sz(1),ones(1,size(Z,2))*cdata_sz(2));
set(b,{'CData'},z_color.')
view(-70,30)

bar3