MATLAB Answers

How to color a graph that is shown below?

3 views (last 30 days)
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;
% new_matrix(i,1:2) = 0;
new_matrix(i,3) = 0;
nodes_cellarray{:} = nodes;
set(figure, 'Visible', 'on');
G = graph(matrix(:,1),matrix(:,2));
plot_array = plot(G, 'layout', 'auto');
% plot_array.NodeColor = 'white';
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.


Jaykumar Vaidya
Jaykumar Vaidya on 4 Jan 2020
Mr. Roberson, these are similar looking color maps for the same thing from different papers (neglect rotation of images) just for reference. Do you think that there is some way out? Thanks.
Walter Roberson
Walter Roberson on 4 Jan 2020
You have Node A as an integer, and given the node number and your numbering diagram, you can translate the node number into X and Y coordinates.
Likewise you have Node B that can be translated into X and Y coordinates.
But what does the Weight column convey ? You have an edge with finite value, and those edges are always either adjacent to each other horizontally or vertically, but what should be done with the weight value?
Like do the inf entries mark nucleation, and I should be plotting those as yellow instead of discarding them? If so then that would not agree with the pattern in the last part of the file, where every 4th entry is reliably inf.
Jaykumar Vaidya
Jaykumar 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?

Sign in to comment.

Accepted Answer

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)
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
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
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(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)];
nodes_cellarray{:} = nodes;
set(figure, 'Visible', 'on');
Graph = graph(matrix(:,1),matrix(:,2));
plot_array = plot(Graph, 'layout', 'auto');


Sign in to comment.

More Answers (0)