**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# How to color a graph that is shown below?

3 views (last 30 days)

Show older comments

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.

##### 15 Comments

Walter Roberson
on 1 Jan 2020

The bottom appears to be an colormapped image.

I am concerned that you might perhaps be getting confused about what "graph()" means to MATLAB. graph() in MATLAB is not a plot: it is a data entity in the "Graph Theory" sense of nodes and edges and paths. Graphs in the Graph Theory sense are abstract topological constructs, and have no inherent visual display and no inherent dimensionality. A Graph is not an image and a Graph is not a map.

Jay Vaidya
on 1 Jan 2020

Walter Roberson
on 2 Jan 2020

Jay Vaidya
on 3 Jan 2020

Walter Roberson
on 3 Jan 2020

No, I do not think that would be helpful.

If we interpret the first two columns to be coordinates, then there are three kinds of values in the file:

- values along the strict diagonal x = y
- values along the 52'nd diagonal, second column = first column + 51
- either of the above, but the data values are both exactly 65535

The only way I have been able to interpret the 65535 values is as indicating "missing data".

For most of the file, the missing-data values occur exactly 4 entries apart. This corresponds to broken 52'nd diagonal, with the pattern "value on main diagonal" then "value on diagonal 52 at same first coordinate", then "value on main diagonal at next coordinate", then "missing value on diagonal 52 at next coordinate".

With the main and 52'nd diagonal being exactly 1 and well seperated, there is no hope of getting the kind of graph you show at the bottom.

Heat map is not applicable to this situation because the coordinates are never exactly the same.

You might hypothesize that perhaps coordinates need to be grouped together, several in a row being binned together, but the problem with this hypothesis is that the two diagonals are strict, and any blocking would be the same thin region, except near the beginning of the file (pattern is slightly different in the beginning of the file.)

You could create connectivity graphs, but all they are going to show is the diagonal line for the main diagonal, and isolated unconnected dots on the 52'nd diagonal.

Jay Vaidya
on 3 Jan 2020

Walter Roberson
on 3 Jan 2020

It would help if you were to document what the columns mean in the context of the paper.

Jay Vaidya
on 3 Jan 2020

Hi Mr. Bobrov,

I have attached a demonstration of what the color map means in the sense of graph. Please note the graph numbers that are there in the colormap and the graph. Thanks.

Walter Roberson
on 3 Jan 2020

Walter Roberson
on 3 Jan 2020

Image Analyst
on 3 Jan 2020

Jay Vaidya
on 3 Jan 2020

Thank you Mr. Roberson. It would be great if you share how the node plot looks like.

Jay Vaidya
on 4 Jan 2020

Walter Roberson
on 4 Jan 2020

Edited: 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.

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!!!

### Accepted Answer

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 = 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);

%

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)