MATLAB Answers

How to convert the graph below in to heatmap?

11 views (last 30 days)
I have the graph below. It was plotted using the code below:
%% matrix (:,4) is the weight of the corresponding matrix(:1:2) branches. Ignore matrix(:,3).
nodes = [];
for i = 1:1:size(matrix,1)
if matrix(i,4) <= 10000
nodes = [nodes,matrix(i,1:2)];
end
end
nodes_cellarray{:} = nodes;
set(figure, 'Visible', 'on');
Graph = graph(matrix(:,1),matrix(:,2));
plot_array = plot(Graph, 'layout', 'auto');
% plot_array.EdgeColor = 'red';
highlight(plot_array,nodes_cellarray{:},'EdgeColor','r','NodeColor','r','LineWidth',4);
I have attached the matrix23.xlsx file that has the matrix 'matrix' used above.
figure23.jpg

  2 Comments

Jaykumar Vaidya
Jaykumar Vaidya on 1 Jan 2020
Can anyone suggest a solution? I was trying to convert it into the adjacency matrix and then make a heatmap from that. But that was giving me some weird adjacency matrix with many 0 values and hence a wrong heatmap as well. Since I am new to MATLAB, I would appreciate it if you have any ideas. Thank you.
Deepak Kumar
Deepak Kumar on 3 Jan 2020
Refer the below MATLAB documentation link to get more insight about "heatmap" function

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Jan 2020
t = readtable('matrix23.xlsx');
mask = t{:,3} == 65535 | t{:,4} == 65535;
t(mask,:) = [];
figure(1)
h = heatmap(t, 'Var1', 'Var2', 'ColorVariable', 'Var3');
h.GridVisible = false;
figure(2)
subplot(1,3,1);
scatter(t{:,1}, t{:,2}, [], t{:,3});
title('scatter');
A = sparse(t{:,1}, t{:,2}, t{:,3}, 2600, 2600);
subplot(1,3,2)
spy(A);
title('normal spy');
subplot(1,3,3)
r = symrcm(A);
spy(A(r,r));
title('spy symrcm');

  3 Comments

Jaykumar Vaidya
Jaykumar Vaidya on 3 Jan 2020
I used this, and put Var1 = x1 and Var2 = x52, what should I put in the ColorVariable and Var3? This is the error that is giving otherwise. 1.JPG
Walter Roberson
Walter Roberson on 3 Jan 2020
Add the option
'readvariablenames', false
To the readtable call. Then the four variables will be Var1 Var2 Var3 Var4. You can reassign the variable names by changing
t.Properties.VariableNames
I assumed that the first column correeponds to x values and the second corresponds to y values and the third corresponds to z values with the fourth being unknown purpose.
You got the x1 and x52 variables because readtable() did not notice that the first row was purely numeric and assumed that the first row was a header line giving the variable names. In sufficiently new versions of MATLAB, readtable() does recognize that the first line is completely numeric and does not create variable names from the data, equivalent to having specified 'readvariablenames', false
Jaykumar Vaidya
Jaykumar Vaidya on 3 Jan 2020
Thank you. The code worked fine after adding 'ReadVariableNames', false. But I don't know what to make out of the results. The heatmap I need is nothing but an image like a color map that I was asking in this thread. I have added the extra explanation there as well. https://www.mathworks.com/matlabcentral/answers/498629-how-to-color-a-graph-that-is-shown-below?s_tid=mlc_ans_email_view#comment_782675 Thank you.

Sign in to comment.

More Answers (1)

Jaykumar Vaidya
Jaykumar Vaidya on 5 Jan 2020
I wrote a for loop to convert the graph into a matrix that represents the weights of the edges.
See below (left: heatmap and the right one is the older graph)
clc
clear all
close all
% A= data imported
% A = xlsread('data.xlsx');
A = xlsread('matrix22.xlsx');
A(isnan(A)) = inf;
matrix = A;
nodes_x = 51;
nodes_y = nodes_x;
% A = A(:,:);
[r_A,c_A]= size(A); %getting row and column dimension of A
%max_A=max(A,[],2).'; %column vector of maximum node number to get max node number
%max_A(3)= max_A(4)= 0; %3,4 column is garbage
%max_A=max(max_A); %gives you the maximum node number
%dim_res= sqrt(max_A); %getting dimension of matrix C
dim_res=nodes_x;
B = zeros(r_A,4); % B will be the direction matrix ie B[;,1]= row number of each point B[;,2]= column number of each point B[;,3]= difference number of each point B[;,4]= weight value of each point
for i=1:r_A
B(i,1)=2*floor(A(i,1)/dim_res)+1; %row of each point cells are left to fill in edges
B(i,2)=2*mod(A(i,1),dim_res)+1; %column of each point cells are left to fill in edges
B(i,3)=A(i,2)-A(i,1); % direction vector of each point
B(i,4)=A(i,4);%assuming column 4 is the required column
end
C=zeros(101,99);
for i=1:r_A
if B(i,3)==1
C(B(i,1),B(i,2)+1)=B(i,4);% if the node is the next node, it should put the value in the adjacent cell in the same row
elseif B(i,3) == nodes_x
C(B(i,1)+1,B(i,2))=B(i,4);
end
end
%%
C(1,:) = [];
C(size(C,1)-1:size(C,1),:) = [];
C(:,1:2) = [];
%5 heatmap
h = heatmap(C,'Colormap', winter);
h.Colormap = parula;
h.ColorScaling = 'scaled';
h.ColorLimits = [0 10000];
h.GridVisible = 'off';
h.MissingDataColor = [0.8 0.8 0.8];
h.Title = 'Phase matrixs';
h.XLabel = '';
h.YLabel = '';
% caxis(h,[0 1]);
%% graph plot
%
nodes = [];
for i = 1:1:size(matrix,1)
if matrix(i,4) <= 10000
nodes = [nodes,matrix(i,1:2)];
end
end
nodes_cellarray{:} = nodes;
set(figure, 'Visible', 'on');
Graph = graph(matrix(:,1),matrix(:,2));
plot_array = plot(Graph, 'layout', 'auto');
highlight(plot_array,nodes_cellarray{:},'EdgeColor','r','NodeColor','r','LineWidth',4);
%

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2019b