# How to color a graph that is shown below?

4 views (last 30 days)
Jay Vaidya on 1 Jan 2020
Answered: Jay Vaidya on 5 Jan 2020
I have graph below that was generated by the following code:
Also the data.xlsx has the matrix used below.
nodes = [];
new_matrix(:,1:2) = matrix(:,1:2);
for i = 1:1:size(matrix,1)
if matrix(i,4) <= 10000
nodes = [nodes,matrix(i,1:2)];
% new_matrix(i,1:2) = 0;
new_matrix(i,3) = 1;
else
% new_matrix(i,1:2) = 0;
new_matrix(i,3) = 0;
end
end
%
nodes_cellarray{:} = nodes;
set(figure, 'Visible', 'on');
G = graph(matrix(:,1),matrix(:,2));
plot_array = plot(G, 'layout', 'auto');
% plot_array.NodeColor = 'white';
highlight(plot_array,nodes_cellarray{:},'EdgeColor','r','NodeColor','red','LineWidth',4);
I get a graph plot as shown here:
I am aiming to color this graph such that it should look like the colorplot below:
This is a MATLAB generated plot from a research artcle.
Jay Vaidya on 4 Jan 2020
Ok, so about the weight column:
Weight column: is the 4th column where we have weights to every possible edge.
(3rd column is useless so just ignore for the entire process)
Nucleation: The numbers in the 4th column that are lesser than 10k (10000) are to be marked as yellow and rest all to be red. So replying to your question, the inf and 65535 values will be always red. Nucleation here means the lower values weights (resistance in this context).
So, according to the weight value, the color of the nodes will be either one or the other. The color choice doesn't matter. But the selection rule will be if data(i,4) <= k where k=10k (in this case) then it will be one color and if not then the other. Red and yellow are just two good looking colors for the sake of graphics. The k value will also be an external input from my rest of the code so it's better than if it is a variable. Of course, k will decide the nucleation filament width and hence the yellow color in this colormap. There might be situations where nucleation looks like the image I sent and there might not be because I have sent you one of the hundreds data.xlsx file and some or many cans and cannot give the same color map. Does that clear the confusion?
Thanks!!!

Jay 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(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);
%

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!