Reputation: 13
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.
Just as an additional comment, if we plot only the XY plane, the result looks like the next picture:
scatter(a(:,2),a(:,1),'*')
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
Upvotes: 1
Views: 221
Reputation: 10440
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:
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)
Upvotes: 1