# Chord diagram

**URL:** <https://root-forum.cern.ch/t/chord-diagram/48785>\
**Category:** Evolution Proposals, Apps & Ideas\
**Tags:** graphics\
**Created:** [February 14, 2022, 12:59am UTC](https://root-forum.cern.ch/t/chord-diagram/48785 "2022-02-14T00:59:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [February 14, 2022, 12:59am UTC](https://root-forum.cern.ch/t/chord-diagram/48785/1 "2022-02-14T00:59:41Z")

</div>

Hello,

I was wondering whether ROOT has the option for plotting chord diagrams, see: [Help Online - Origin Help - Chord Diagram/Chord Diagram with Ratio Layout](https://www.originlab.com/doc/Origin-Help/Chord-Diagram)  
[circularGraph - File Exchange - MATLAB Central](https://es.mathworks.com/matlabcentral/fileexchange/48576-circulargraph)

If not, maybe it can be implemented in the future as a class derived from TGraphPolar using TPolyLines?

Thanks for the attention.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 14, 2022, 8:53am UTC](https://root-forum.cern.ch/t/chord-diagram/48785/2 "2022-02-14T08:53:09Z")

</div>

ROOT does not have such diagram. It would required a new implementation. It is circular but I am not sure it should derive from TGraphPolar.

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [February 14, 2022, 9:02am UTC](https://root-forum.cern.ch/t/chord-diagram/48785/3 "2022-02-14T09:02:07Z")

</div>

I do not think that `TGraphPolar` is good starting point - it always drawing polar coordinates.  
`Chord diagram` and `circular graph` is absolutely new kind of draw options, which could be implemented for the `TH2` class. I could try to implement this in JSROOT.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 14, 2022, 9:17am UTC](https://root-forum.cern.ch/t/chord-diagram/48785/4 "2022-02-14T09:17:26Z")

</div>

What would be the format of data ? what kind of ROOT data do you want to show ?  
As said [here](https://www.data-to-viz.com/graph/chord.html):

_A `chord diagram` represents flows or connections between several entities (called `nodes` )._

I am not sure which kind of ROOT data will map this.

May be a TH2 with the same alpha numeric labels along X and Y ? each label would be “a node” and the content of each bin gives the strength of the connection between two nodes and therefore the thickness of the line connecting them ?  
So that will be a special case of TH2

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [February 14, 2022, 9:31am UTC](https://root-forum.cern.ch/t/chord-diagram/48785/5 "2022-02-14T09:31:07Z")

</div>

In MATLAB, the data is expressed as a 2D matrix.

The strength of connection between nodes i and j are just matrix[i][j]. The diagonal matrix[i][i] is zero. The matrix should be symmetric matrix[i][j] = matrix[j][i]. (Or it should be specified if it’s the lower or the upper triangular matrix that will be considered for plotting)

I agree, a TH2 could work well, but it would be good if it would also support passing a raw C array (2D matrix).

Also, it would be great if the transparency and color of the connecting line, not just the thickness, can be set as function of strength.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 14, 2022, 12:10pm UTC](https://root-forum.cern.ch/t/chord-diagram/48785/6 "2022-02-14T12:10:24Z")

</div>

Here is a “quick and dirty” macro producing a circular graph. It is really just the basic structure … nothing you can use … just the idea:

```auto
TCanvas *c1;

void chord(TH2F *h)
{
   c1->cd(2)->Range(-1, -1, 1, 1);;
   TEllipse *circle = new TEllipse(0., 0., 0.8);
   circle->SetLineWidth(1);

   circle->Draw("L");
   TAxis *xaxis;
   xaxis=h->GetXaxis();
   THashList *labels = xaxis->GetLabels();
   int ndiv = xaxis->GetLast()-xaxis->GetFirst()+1;
   float da = 2*TMath::Pi()/ndiv;

   float ax = 0 ;
   float ay = 0;
   for (int i=1; i<=ndiv; i++) {
      for (int j=1; j<=ndiv; j++) {
         if (h->GetBinContent(i,j)>0){
            auto l = new TLine(0.8*TMath::Cos(ax), 0.8*TMath::Sin(ax),
                               0.8*TMath::Cos(ay), 0.8*TMath::Sin(ay));
            l->SetLineWidth(h->GetBinContent(i,j));
            l->SetLineColor(h->GetBinContent(i,j));
            l->Draw();
         }
         ay = ay+da;
      }
      ay = 0;
      ax = ax+da;
   }

   TIter next(labels);
   auto t= new TText();
   t->SetTextAlign(22);
   t->SetTextSize(0.03);
   auto m = new TMarker();
   m->SetMarkerStyle(20);

   float a = 0;
   while (TObject *obj = next()) {
      t->DrawText(0.85*TMath::Cos(a), 0.85*TMath::Sin(a), obj->GetName());
      m->DrawMarker(0.8*TMath::Cos(a), 0.8*TMath::Sin(a));
      a = a+da;
   }
}

void chord_diagrams()
{
   const Int_t n = 12;
   const char *L[n] = {"A","B","C","D","E","F","G","H","I","J","K","L"};

   c1 = new TCanvas("c","c",10,10,1200,600);
   c1->Divide(2,1);

   auto *h = new TH2F("h","",n,0,1,n,0,1);
   h->SetStats(0);

   for (int i=0;i<n;i++) h->Fill(L[i],L[i],0);

   h->Fill(L[2],L[3],1);
   h->Fill(L[3],L[2],1);
   h->Fill(L[5],L[3],2);
   h->Fill(L[3],L[5],2);
   h->Fill(L[9],L[4],5);
   h->Fill(L[4],L[9],5);

   c1->cd(1)->SetGrid();
   h->Draw("text");
   chord(h);
}

```

 ![Screenshot 2022-02-14 at 14.30.08](https://root-forum.cern.ch/uploads/default/original/3X/4/e/4ed0b719b4fbfa83803f1d85445db64a251823f7.png)

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [February 14, 2022, 12:30pm UTC](https://root-forum.cern.ch/t/chord-diagram/48785/7 "2022-02-14T12:30:02Z")

</div>

Cool, thanks

I guess that the second for loop should stop at j\<i ? (To not draw twice the same, as it’s symmetrical?)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 14, 2022, 12:42pm UTC](https://root-forum.cern.ch/t/chord-diagram/48785/8 "2022-02-14T12:42:54Z")

</div>

> [@ferhue](#):
>
> I guess that the second for loop should stop at j\<i ? (To not draw twice the same, as it’s symmetrical?)

As I said it is just a quick and dirty prototype. Fell free to improve it. The initialization and filling of the histogram can be improved too as the histogram used has some strict characteristics.

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [February 14, 2022, 5:27pm UTC](https://root-forum.cern.ch/t/chord-diagram/48785/9 "2022-02-14T17:27:27Z")

</div>

Hi,

I slightly modify Olivier macro:

```auto
void chord_diagrams()
{
   const Int_t n = 12;
   const char *L[n] = {"A","B","C","D","E","F","G","H","I","J","K","L"};

   c1 = new TCanvas("c","c",10,10,1200,600);
   c1->Divide(2,1);

   auto *h = new TH2F("h","",n,0,1,n,0,1);
   h->SetStats(0);

   for (int i=0;i<n;i++) h->Fill(L[i],L[i],0);

   h->Fill(L[2],L[3],1);
   h->Fill(L[3],L[2],1);
   h->Fill(L[5],L[3],2);
   h->Fill(L[3],L[5],2);
   h->Fill(L[9],L[4],5);
   h->Fill(L[4],L[9],5);

   c1->cd(1)->SetGrid();
   h->Draw("text");
   c1->cd(2);
   h->Draw("circular");
}

```

This is produced canvas:

 ![Canvas](https://root-forum.cern.ch/uploads/default/original/3X/c/6/c67df61bc348dd3babe48dfe02a86d0662d5b6f3.png)

To get it working, with current root master do:

```auto
[shell] export JSROOTSYS=https://jsroot.gsi.de/dev/
[shell] root --web chord_diagrams.C

```

Later I will commit modified JSROOT code into ROOT repository.  
After that ROOT will work directly - without need to set `JSROOTSYS` variable.

I also want to implement `chord diagram` - it should be also relatively simple.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 15, 2022, 7:24am UTC](https://root-forum.cern.ch/t/chord-diagram/48785/10 "2022-02-15T07:24:03Z")

</div>

Looks nice ! The next step might be to make some special methods to book and fill “chord histograms” as they are really special:

- Same label along X and Y axis
- Diagonal = 0
- Symmetrical.

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [February 15, 2022, 11:28am UTC](https://root-forum.cern.ch/t/chord-diagram/48785/11 "2022-02-15T11:28:20Z")

</div>

Hi @ferhue,

I now implement “chord” drawing option, taking already provided functionality  
of d3.js - see [Chord Diagram / D3 / Observable](https://observablehq.com/@d3/chord-diagram)

If in the macro “chord” instead of “circle” option is specified, output will be:

 ![Canvas](https://root-forum.cern.ch/uploads/default/original/3X/f/6/f6133d401fffea305cca6d8eb11fbad09106c77a.png)

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [February 15, 2022, 12:12pm UTC](https://root-forum.cern.ch/t/chord-diagram/48785/12 "2022-02-15T12:12:52Z")

</div>

So beautiful!  
Thanks so much for the prompt implementation!!
