File Exchange

## circularGraph

version 2.0.0.0 (828 KB) by Paul Kassebaum

### Paul Kassebaum (view profile)

Plot an interactive circular graph to illustrate connections in a network.

Updated 27 Sep 2016

Plot an interactive circular graph to illustrate connections in a network in MATLAB.

### Cite As

Paul Kassebaum (2019). circularGraph (https://www.github.com/paul-kassebaum-mathworks/circularGraph), GitHub. Retrieved .

Ryan McDonough

### Ryan McDonough (view profile)

Also change

[row2,col2,v2] = find(correlationMatrix); %v2 will create Nx1 matrix of all correaltion values

to

diag = tril(correlationMatrix);
for n = 1:size(diag,1)
v2(n,1) = max(diag(n,:));
end

Ryan McDonough

### Ryan McDonough (view profile)

Follow up to my comment. Change both instances of

>'Color', this.ColorMap(row(i),:),...

to

>'Color', defaultColorMap(i,:),...

Ryan McDonough

### Ryan McDonough (view profile)

For those of you who want to control the line colors based on correlation value I have found a solution

Add a second input to the function call to include another matrix (ignore if only using one matrix, just replace correlationMatrix with adjacencyMatrix or matrix of your choice)

After the line

>p = inputParser;

Paste the following to convert your assembled array of correlation values into instance-ordered RGB values:

>[row2,col2,v2] = find(correlationMatrix); %v2 will create Nx1 matrix of all correaltion values
>map = colormap(hot); %intialize colormap, change the color map scheme to one of your choice
>minv = min(v2(:)); %min max to establish bounds of colormap
>maxv = max(v2(:));
>ncol = size(map,1);
>s = round(1+(ncol-1)*(v2-minv)/(maxv-minv)); %scale indicies for conversion
>rgb_image = ind2rgb(s,map); %convert scaled indicies to RGB, gives Nx1X3 matrix
>defaultColorMap = reshape(rgb_image, [length(v2) 3]); %reshape into Nx3 matrix

Change current line 46 (will become line 54)

to

and current line 121 (will become line 130)

>'Color', this.ColorMap(row(i),:),...

You can also create a custom colormap that is the length of the number of node connections you have and feed that into the the code (might require changing a couple lines). Hope this helps!

to

>'Color', defaultColorMap(i,:),...

Ryan McDonough

Tianshi Xiong

Walter

### Walter (view profile)

@Richard Thank you for fixing the lines outside disk bug!
For others, comment out this section:
if u(1) >= 0 && v(1) >= 0
% ensure the arc is within the unit disk
theta = [linspace(max(thetaLim),pi,50),...
linspace(-pi,min(thetaLim),50)].';
else
theta = linspace(thetaLim(1),thetaLim(2)).';
end

sortedthetaLim=sort(thetaLim);
if sortedthetaLim(2)-sortedthetaLim(1)>pi
thetaLim(1)=sortedthetaLim(2)-(2*pi);
thetaLim(2)=sortedthetaLim(1);
end
theta = linspace(thetaLim(1),thetaLim(2)).';

Pramod Somvanshi

### Pramod Somvanshi (view profile)

Hi, is there a way to adjust the colormap range from -1 to 1 while plotting the correlation coefficients?

bing yang

Thomas Pfeffer

### Thomas Pfeffer (view profile)

thanks for the function, i really like it. is there a way to turn off the buttons? i tried to comment the respective parts of the code out, but somehow it doesn't affect the appearance... thanks!

Arun Kumar

Valentina Zapata

### Valentina Zapata (view profile)

Hi, is there any way to change the font size of the node numbers all at the same time without having to do them individually?

Dan

Fidan Sümbül

### Fidan Sümbül (view profile)

Elizabeth Drybrugh

### Elizabeth Drybrugh (view profile)

Hi , when I create a new label it is just overwriting it. How do I delete default labels first? New to matlab.

Cheers.

Elizabeth Drybrugh

### Elizabeth Drybrugh (view profile)

Thanks for sharing, cannot wait to try this.

Pietro Elia

Poulomi Ganguli

Poulomi Ganguli

Naba Ahsan

### Naba Ahsan (view profile)

How can I specify the number of nodes in the graph?

Lei Hao

Mylena Reis

### Mylena Reis (view profile)

@Roman How did you made the circular graph and the colorbar?

Mylena Reis

Tommaso Sanzin

Richard

### Richard (view profile)

@Paul there seems to be a problem with some arcs going outside rather than inside the circle; I think it can happen if the number of nodes is divisible by 4. In this case thetaLim(2) is +pi but the algorithm is expecting -pi and ends up drawing the wrong of the two possible arcs. I fixed this by explicitly checking which arc is shorter (sort thetaLim, and if the difference between the values is >pi subtract 2*pi from the larger one and put that first) and drawing that, rather than doing the u(1),v(1)>=0 check. Hope that helps!

Paul Kassebaum

### Paul Kassebaum (view profile)

@John, thanks! (:

John Mungall

### John Mungall (view profile)

Many thanks for this much appreciated code. I've wanted to try it for some time and "today was the day!"

Paul Kassebaum

### Paul Kassebaum (view profile)

@Roman, looks like an interesting article! Thanks for citing my code. I'm glad it was useful. I've shared your article with some friends at MathWorks who have been thinking about computation in chemistry / ChE curricula. Cheers!

Roman Voronov

Derek Walpole

### Derek Walpole (view profile)

@Paul Great tool, thanks. One issue I'm having is some of my graphs have a few of the connections between nodes on the outside. Looks a bit weird and crosses over labels. Are you familiar with this and how to fix it? Many thanks.

Gyan Raj Koirala

### Gyan Raj Koirala (view profile)

@Iness
You do not have control over the colors with this code (See answer to my question from Paul below).
In order to change the width of the lines: In circularGraph.m go to
%Calculate line width based on values of s (stored in v) and adjust the parameters
For example:
minLineWidth=5;
lineWidthCoef=5;
lineWidth=v;
Hope it works.
Regards,

Iness Mahjoub

### Iness Mahjoub (view profile)

Please I want to change the colors of lines and their widths, how can I do?

Markus Gschwind

### Markus Gschwind (view profile)

Dear all is it possible to start the plot a clock position 12:00 and then go clockwise, and not at 9:00? How can I manage this?

Paul Kassebaum

### Paul Kassebaum (view profile)

@Gyan
Your use case is beyond what my design can do. I designed the nodes and connecting lines as a single graphical object: a line() with a single marker. So, when you change the color of a node, you change the color of all the connections that come out of it. Your use case is likely quite common. If I find time, I'll consider overhauling this design to enable it. In the meanwhile, you can get quite far with the network graph capabilities in MATLAB nowadays. I wrote this before MATLAB included network graphs as fundamental data types. Check this documentation page out:

https://www.mathworks.com/help/matlab/graph-and-network-algorithms.html

Gyan Raj Koirala

### Gyan Raj Koirala (view profile)

Thank you @Mathworks Physics Team
But I still have the problem to control the connectivity lines color.
Could you please help me to generate the connection lines based on the thresholds (say if the connectivity value is 1 then the connection lines are #red and if the connectivity value is 2 then the connection lines are #blue).

MathWorks Physics Team

### MathWorks Physics Team (view profile)

@Gyan
Thanks for your note and rating.

In makeLine() of Node.m, in the line object construction, add this line:
'MarkerFaceColor', this.Color,...

Add this line of code to updateColor() in Node.m
this.NodeMarker.MarkerFaceColor = this.Color;

Add this line of code to the end of updateTextLabel() in Node.m
this.TextLabel.FontWeight = 'bold';

To specify the color of each node:
c = circularGraph(x); % for some x
cmap = colormap(cool(length(c.Node))); % some colormap
for k = 1 : length(c.Node)
c.Node(k).Color = cmap(k,:);
end

Gyan Raj Koirala

### Gyan Raj Koirala (view profile)

Dear Paul,
1. Fill the specific nodes with a fixed colors.
2. Control the color of the lines.
3. Make the label font bold.

Thanks for this great tool.

Leo Contraire

Roman Voronov

### Roman Voronov (view profile)

Dear Paul, would it be possible to give meaning to the color of the connections? For example, can they be colored according to the strength of the connection, kind of like a heat map? Can you please give me some tips for how to accomplish that?

Roman Voronov

Gemma Williams

### Gemma Williams (view profile)

This is really helpful, thank you... I'm also wondering how to reduce the font size? Could you give any more information than that below? Also, how would i add connections from more than one node please? At the moment i'm only able to add connections for one node.
Thanks, Gemma

Lawrence Frank

### Lawrence Frank (view profile)

magdalena san roman

Edgar Guevara

### Edgar Guevara (view profile)

@Paul: Great visualization tool, simple, yet effective. Always wanted some version of Circos for MATLAB. On a side note, I had to modify Lines 120 & 143: from lineWidth(i) to abs(lineWidth(i)), so I can plot negative connections (anticorrelations)

cheng joylin

ATIF KHAN

### ATIF KHAN (view profile)

@Paul, Thank you very much. I really appreciate your prompt response and help.

Paul Kassebaum

### Paul Kassebaum (view profile)

@ATIF, thanks for the compliment and rating.

I recommend editing the updateTextLabel() method in the node class (defined in node.m of this package). In updateTextLabel(), an underlying text() object is created (a class built-in to MATLAB), which is a good place to assign the text() object's FontSize property.

Let me know if you need further help.
-
Paul

ATIF KHAN

### ATIF KHAN (view profile)

This is awesome. Thanks for sharing.
I have a problem with the label fonts. I am plotting 50 nodes that have long labels. In my case, circular graph suppress the size of the actual graph and magnifies the label font size but I want it the other way around. Is there a way I can decrease the font of node label and magnify the graph itself?

ATIF KHAN

Gareth Thomas

### Gareth Thomas (view profile)

1 word: Awesome:)

Paul Kassebaum

### Paul Kassebaum (view profile)

@Donald, in the file called node.m look for the line that reads

set(this.Connection,'Color',0.9*[1 1 1]);

and change it to

set(this.Connection,'Color',[1 1 1]);

Donald McMenemy

### Donald McMenemy (view profile)

Paul,

This is useful. Is there a way to make the hidden connections turn white (to match the background) instead of gray?

Don

Paul Kassebaum

### Paul Kassebaum (view profile)

@Ivan, thank you (:

Ivan

### Ivan (view profile)

Functionality can be extended a lot, but, still, a good job. Thank you.

Paul Kassebaum

### Paul Kassebaum (view profile)

@MarcVanKralingen might you have an example code snippet that demonstrates the issue?

Marc van Kralingen

### Marc van Kralingen (view profile)

One of the lines is placed outside the circle. I cannot find the bug.

Ligong Han

Paul Kassebaum

### Paul Kassebaum (view profile)

@Cristi
You can fix the bug by changing line 112 in circularGraph.m from
if row(i) < col(i)
to
if row(i) ~= col(i)

Cristi

### Cristi (view profile)

Hi, very nice looking graph! Is it also working with directed graphs, that is having 1s in both upper and lower triangle of the matrix?
I ran the code on a matrix "hc", which has 1 values coresponding to the following labels:
>> c.Label(find(hc(:,4)>0))

ans = 'A' 'A'' 'ACC' 'MOFC'' 'MTG'' 'OF' 'PHG'' 'PMC' 'R'' 'TP''

>> c.Label(find(hc(4,:)>0))

ans = 'A'' 'ACC' 'PCC'' 'VLPFC'' 'aI'' 'pI''

However, when only highlighting node 4, labeled ACC' by clicking on it, only 4 lines are drawn, leading from ACC' to PCC', VLPFC', aI' and pI'.
Also, when looking at node 4, it returns 5 connections, although only the 4 mentioned above are visible on the graph:
>> c.Node(4)

ans =

node with properties:

Label: 'ACC''
Connection: [1x5 Line]
Position: [-0.9629 -0.2698]
Color: [0.2095 0.2331 0.6660]
Visible: 1
Extent: 0.2833

Any thoughts on this issue? Apparently it only draws connections with nodes that have a higher number, in this example ignoring nodes 2 (A') and 3 (ACC).

Thanks,
Cristi