import pandas as pd
import holoviews as hv
from holoviews import opts, dim
from bokeh.sampledata.les_mis import data
import numpy as np
hv.extension('bokeh')
hv.output(size=200)
The Chord
element allows representing the inter-relationships between data points in a graph. The nodes are arranged radially around a circle with the relationships between the data points drawn as arcs (or chords) connecting the nodes. The number of chords is scaled by a weight declared as a value dimension on the Chord
element.
If the weight values are integers, they define the number of chords to be drawn between the source and target nodes directly. If the weights are floating point values, they are normalized to a default of 500 chords, which are divided up among the edges. Any non-zero weight will be assigned at least one chord.
The Chord
element is a type of Graph
element and shares the same constructor. The most basic constructor accepts a columnar dataset of the source and target nodes and an optional value. Here we supply a dataframe containing the number of dialogues between characters of the Les Misérables musical. The data contains source
and target
node indices and an associated value
column:
italy_diet = pd.read_pickle("data/processed/Italy_opt_diet.pkl")
italy = italy_diet.sum(axis=0)
france_diet = pd.read_pickle("data/processed/France_opt_diet.pkl")
france = france_diet.sum(axis=0)
uk_diet = pd.read_pickle("data/processed/United Kingdom_opt_diet.pkl")
uk = uk_diet.sum(axis=0)
spain_diet = pd.read_pickle("data/processed/Spain_opt_diet.pkl")
spain = spain_diet.sum(axis=0)
germany_diet = pd.read_pickle("data/processed/Germany_opt_diet.pkl")
germany = germany_diet.sum(axis=0)
links_it = pd.DataFrame(italy).reset_index()
links_it["source"]="Italy"
links_it = links_it.rename(columns={"index":"target", 0:"value"})
links_it = links_it[["source", "target", "value"]]
links_fr = pd.DataFrame(france).reset_index()
links_fr["source"]="France"
links_fr = links_fr.rename(columns={"index":"target", 0:"value"})
links_fr = links_fr[["source", "target", "value"]]
links_uk = pd.DataFrame(uk).reset_index()
links_uk["source"]="United Kingdom"
links_uk = links_uk.rename(columns={"index":"target", 0:"value"})
links_uk = links_uk[["source", "target", "value"]]
links_ge = pd.DataFrame(germany).reset_index()
links_ge["source"]="Germany"
links_ge = links_ge.rename(columns={"index":"target", 0:"value"})
links_ge = links_ge[["source", "target", "value"]]
links_es = pd.DataFrame(spain).reset_index()
links_es["source"]="Spain"
links_es = links_es.rename(columns={"index":"target", 0:"value"})
links_es = links_es[["source", "target", "value"]]
links = links_it.append(links_fr).append(links_uk).append(links_ge).append(links_es)
tot_eu = links.groupby("source").sum().reset_index().rename(columns={"source":"name"})
tot_af = links.groupby("target").sum().reset_index().rename(columns={"target":"name"})
tot = tot_eu.append(tot_af)
tot.head()
name | value | |
---|---|---|
0 | France | 102881.879243 |
1 | Germany | 112066.319546 |
2 | Italy | 92060.460720 |
3 | Spain | 86235.003277 |
4 | United Kingdom | 101507.471800 |
from sklearn.preprocessing import OrdinalEncoder
enc_eu = OrdinalEncoder(dtype=int)
eu_nations = links.source.values.reshape(-1,1)
enc_eu.fit(eu_nations)
enc_af = OrdinalEncoder(dtype=int)
af_nations = links.target.values.reshape(-1,1)
enc_af.fit(af_nations)
links[["source"]] = enc_eu.transform(links[["source"]])
links[["target"]] = enc_af.transform(links[["target"]]) + 10
links
source | target | value | |
---|---|---|---|
0 | 2 | 17 | 25370.332652 |
1 | 2 | 19 | 10560.449765 |
2 | 2 | 32 | 8647.405612 |
3 | 2 | 22 | 7861.041846 |
4 | 2 | 33 | 6072.955554 |
... | ... | ... | ... |
20 | 3 | 11 | 260.635343 |
21 | 3 | 18 | 213.509272 |
22 | 3 | 16 | 163.345226 |
23 | 3 | 20 | 61.625137 |
24 | 3 | 15 | 59.402224 |
125 rows × 3 columns
def find_name(x):
if (x<=4):
return enc_eu.inverse_transform(np.array(x).reshape(-1,1)).flatten()[0]
else:
return enc_af.inverse_transform(np.array(x-10).reshape(-1,1)).flatten()[0]
def find_group(x):
if (x<=4):
return "Europe"
else:
return "Africa"
nodes_pd = pd.DataFrame(columns=["index", "name", "group"])
nodes_pd["index"] = list(links.source.values) + list(links.target.values)
nodes_pd["name"] = nodes_pd["index"].apply(lambda x: find_name(x))
nodes_pd["group"] = nodes_pd["index"].apply(lambda x: find_group(x))
nodes_pd = nodes_pd.groupby("name").first().reset_index()[["index", "name", "group"]]
nodes_pd = nodes_pd.sort_values("index")
nodes_pd = nodes_pd.merge(tot, on="name")
nodes_pd.replace({"United Republic of Tanzania":'Tanzania'}, inplace=True)
nodes_pd.replace({"Central African Republic":'Centr. Afr. Rep.'}, inplace=True)
nodes = hv.Dataset(nodes_pd, "index")
nodes.data.head()
index | name | group | value | |
---|---|---|---|---|
0 | 0 | France | Europe | 102881.879243 |
1 | 1 | Germany | Europe | 112066.319546 |
2 | 2 | Italy | Europe | 92060.460720 |
3 | 3 | Spain | Europe | 86235.003277 |
4 | 4 | United Kingdom | Europe | 101507.471800 |
# nodes = hv.Dataset(pd.DataFrame(data['nodes']), 'index')
# nodes.data.name
Additionally we can now color the nodes and edges by their index and add some labels. The labels
, node_color
and edge_color
options allow us to reference dimension values by name.
from math import pi
#0.019
def rotate_label(plot, element):
text_cds = plot.handles['text_1_source']
length = len(text_cds.data['angle'])
text_cds.data['angle'] = [pi/256]*length
xs = text_cds.data['x']
ys = text_cds.data['y']
text = np.array(text_cds.data['text'])
xs[xs<0] -= np.array([len(t)*0.0275 for t in text[xs<0]])
xs[xs>0] += np.array([len(t)*0.0035 for t in text[xs>0]])
xs[np.where(text=="Botswana")] -= 0.02
xs[np.where(text=="Centr. Afr. Rep.")] += 0.1
ys[np.where(text=="Centr. Afr. Rep.")] -= 0.02
xs[np.where(text=="Chad")] -= 0.02
ys[np.where(text=="Angola")] += 0.01
ys[np.where(text=="Eswatini")] -= 0.02
xs[np.where(text=="Eswatini")] += 0.03
xs[np.where(text=="Ethiopia")] += 0.035
xs[np.where(text=="Congo")] -= 0.03
ys[np.where(text=="Congo")] += 0.01
xs[np.where(text=="Djibouti")] += 0.03
xs[np.where(text=="Liberia")] += 0.175
ys[np.where(text=="Liberia")] -= 0.02
ys[np.where(text=="Lesotho")] -= 0.02
xs[np.where(text=="Lesotho")] += 0.01
move_up = ['Rwanda', 'Senegal', 'Sierra Leone', 'Sudan', 'Togo', 'Uganda']
ys[[np.where(text==c)[0][0] for c in move_up]] +=0.025
move_left = ['Madagascar', 'Malawi', 'Mozambique', 'Sierra Leone']
xs[[np.where(text==c)[0][0] for c in move_left]] -=0.035
move_down = ['Madagascar', 'Malawi', 'Mozambique']
ys[[np.where(text==c)[0][0] for c in move_down]] -=0.01
move_left2 = ['Tanzania', 'Zambia', 'Zimbabwe']
xs[[np.where(text==c)[0][0] for c in move_left2]] -=0.025
xs[np.where(text=="Namibia")] -= 0.01
xs[np.where(text=="Uganda")] -= 0.01
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
chord = hv.Chord((links, nodes))
chord.opts(
opts.Chord(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('source').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], bgcolor='rgba(0,0,0,0)', border=0,finalize_hooks=[rotate_label] ))
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
options = dict(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('source').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], bgcolor='rgba(0,0,0,0)', border=0, finalize_hooks=[rotate_label])
chord = hv.Chord((links, nodes)).options(**options)
hv.renderer('bokeh').save(chord, 'docs/_includes/chord')
tot_it = links_it.groupby("source").sum().reset_index().rename(columns={"source":"name"})
tot_it = tot_it.append(links_it[["target", "value"]].rename(columns={"target":"name"}))
tot_it.replace({"United Republic of Tanzania":'Tanzania'}, inplace=True)
tot_it.replace({"Central African Republic":'Centr. Afr. Rep.'}, inplace=True)
tot_it.head()
name | value | |
---|---|---|
0 | Italy | 92060.460720 |
0 | Ethiopia | 25370.332652 |
1 | Kenya | 10560.449765 |
2 | Tanzania | 8647.405612 |
3 | Madagascar | 7861.041846 |
links_it[["source"]] = enc_eu.transform(links_it[["source"]])
links_it[["target"]] = enc_af.transform(links_it[["target"]]) + 10
links_it.head()
source | target | value | |
---|---|---|---|
0 | 2 | 17 | 25370.332652 |
1 | 2 | 19 | 10560.449765 |
2 | 2 | 32 | 8647.405612 |
3 | 2 | 22 | 7861.041846 |
4 | 2 | 33 | 6072.955554 |
nodes_italy_pd = nodes.data.copy()
nodes_italy_pd = nodes_italy_pd[~(nodes_italy_pd.name.isin(["France", "Germany", "United Kingdom", "Spain"]))]
nodes_italy_pd.drop(columns="value", inplace=True)
nodes_italy_pd = nodes_italy_pd.merge(tot_it, on="name")
nodes_italy = hv.Dataset(nodes_italy_pd, "index")
nodes_italy.data
index | name | group | value | |
---|---|---|---|---|
0 | 2 | Italy | Europe | 92060.460720 |
1 | 10 | Angola | Africa | 1464.021255 |
2 | 11 | Botswana | Africa | 280.365196 |
3 | 12 | Centr. Afr. Rep. | Africa | 1029.741680 |
4 | 13 | Chad | Africa | 4046.982613 |
5 | 14 | Congo | Africa | 1168.294970 |
6 | 15 | Djibouti | Africa | 65.672100 |
7 | 16 | Eswatini | Africa | 176.567587 |
8 | 17 | Ethiopia | Africa | 25370.332652 |
9 | 18 | Guinea-Bissau | Africa | 230.086981 |
10 | 19 | Kenya | Africa | 10560.449765 |
11 | 20 | Lesotho | Africa | 68.043698 |
12 | 21 | Liberia | Africa | 578.177169 |
13 | 22 | Madagascar | Africa | 7861.041846 |
14 | 23 | Malawi | Africa | 2931.827882 |
15 | 24 | Mozambique | Africa | 3320.666955 |
16 | 25 | Namibia | Africa | 503.050673 |
17 | 26 | Rwanda | Africa | 1382.734793 |
18 | 27 | Senegal | Africa | 2946.557394 |
19 | 28 | Sierra Leone | Africa | 1203.713639 |
20 | 29 | Sudan | Africa | 4985.309801 |
21 | 30 | Togo | Africa | 488.006793 |
22 | 31 | Uganda | Africa | 4587.075291 |
23 | 32 | Tanzania | Africa | 8647.405612 |
24 | 33 | Zambia | Africa | 6072.955554 |
25 | 34 | Zimbabwe | Africa | 2091.378821 |
from bokeh.models import HoverTool
def rotate_label2(plot, element):
text_cds = plot.handles['text_1_source']
length = len(text_cds.data['angle'])
text_cds.data['angle'] = [pi/256]*length
xs = text_cds.data['x']
ys = text_cds.data['y']
text = np.array(text_cds.data['text'])
xs[xs<0] -= np.array([len(t)*0.0275 for t in text[xs<0]])
xs[xs>0] += np.array([len(t)*0.0035 for t in text[xs>0]])
xs[np.where(text=="Botswana")] -= 0.02
xs[np.where(text=="Centr. Afr. Rep.")] += 0.1
ys[np.where(text=="Centr. Afr. Rep.")] -= 0.02
xs[np.where(text=="Chad")] -= 0.02
ys[np.where(text=="Angola")] += 0.01
ys[np.where(text=="Eswatini")] -= 0.04
xs[np.where(text=="Eswatini")] += 0.04
xs[np.where(text=="Ethiopia")] += 0.035
xs[np.where(text=="Congo")] -= 0.03
ys[np.where(text=="Congo")] += 0.025
xs[np.where(text=="Djibouti")] += 0.03
xs[np.where(text=="Liberia")] += 0.19
ys[np.where(text=="Liberia")] -= 0.02
ys[np.where(text=="Lesotho")] -= 0.02
xs[np.where(text=="Lesotho")] += 0.01
move_up = ['Rwanda', 'Senegal', 'Sierra Leone', 'Sudan', 'Togo', 'Uganda']
ys[[np.where(text==c)[0][0] for c in move_up]] +=0.025
move_left = ['Madagascar', 'Malawi', 'Mozambique', 'Sierra Leone']
xs[[np.where(text==c)[0][0] for c in move_left]] -=0.03
move_down = ['Madagascar', 'Malawi', 'Mozambique']
ys[[np.where(text==c)[0][0] for c in move_down]] -=0.01
move_left2 = ['Tanzania', 'Zambia', 'Zimbabwe']
xs[[np.where(text==c)[0][0] for c in move_left2]] -=0.025
xs[np.where(text=="Namibia")] -= 0.01
xs[np.where(text=="Uganda")] -= 0.01
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
chord_it = hv.Chord((links_it, nodes_italy))
chord_it.opts(
opts.Chord(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], finalize_hooks=[rotate_label2]))
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
options = dict(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], bgcolor='rgba(0,0,0,0)', border=0, finalize_hooks=[rotate_label2])
chord_it = hv.Chord((links_it, nodes_italy)).options(**options)
hv.renderer('bokeh').save(chord_it, 'docs/_includes/chord_it')
tot_fr = links_fr.groupby("source").sum().reset_index().rename(columns={"source":"name"})
tot_fr = tot_fr.append(links_fr[["target", "value"]].rename(columns={"target":"name"}))
tot_fr.replace({"United Republic of Tanzania":'Tanzania'}, inplace=True)
tot_fr.replace({"Central African Republic":'Centr. Afr. Rep.'}, inplace=True)
tot_fr.head()
name | value | |
---|---|---|
0 | France | 102881.879243 |
0 | Ethiopia | 28324.819373 |
1 | Kenya | 11793.007659 |
2 | Tanzania | 9657.535962 |
3 | Madagascar | 8779.742545 |
links_fr[["source"]] = enc_eu.transform(links_fr[["source"]])
links_fr[["target"]] = enc_af.transform(links_fr[["target"]]) + 10
links_fr.head()
source | target | value | |
---|---|---|---|
0 | 0 | 17 | 28324.819373 |
1 | 0 | 19 | 11793.007659 |
2 | 0 | 32 | 9657.535962 |
3 | 0 | 22 | 8779.742545 |
4 | 0 | 33 | 6783.757422 |
nodes_fr_pd = nodes.data.copy()
nodes_fr_pd = nodes_fr_pd[~(nodes_fr_pd.name.isin(["Italy", "Germany", "United Kingdom", "Spain"]))]
nodes_fr_pd.drop(columns="value", inplace=True)
nodes_fr_pd = nodes_fr_pd.merge(tot_fr, on="name")
nodes_fr = hv.Dataset(nodes_fr_pd, "index")
nodes_fr.data
index | name | group | value | |
---|---|---|---|---|
0 | 0 | France | Europe | 102881.879243 |
1 | 10 | Angola | Africa | 1638.947392 |
2 | 11 | Botswana | Africa | 317.668942 |
3 | 12 | Centr. Afr. Rep. | Africa | 1154.174607 |
4 | 13 | Chad | Africa | 4522.226832 |
5 | 14 | Congo | Africa | 1308.837337 |
6 | 15 | Djibouti | Africa | 78.013715 |
7 | 16 | Eswatini | Africa | 201.802899 |
8 | 17 | Ethiopia | Africa | 28324.819373 |
9 | 18 | Guinea-Bissau | Africa | 261.544934 |
10 | 19 | Kenya | Africa | 11793.007659 |
11 | 20 | Lesotho | Africa | 80.661057 |
12 | 21 | Liberia | Africa | 650.107186 |
13 | 22 | Madagascar | Africa | 8779.742545 |
14 | 23 | Malawi | Africa | 3277.414284 |
15 | 24 | Mozambique | Africa | 3711.463249 |
16 | 25 | Namibia | Africa | 566.245815 |
17 | 26 | Rwanda | Africa | 1548.209842 |
18 | 27 | Senegal | Africa | 3293.856380 |
19 | 28 | Sierra Leone | Africa | 1348.374095 |
20 | 29 | Sudan | Africa | 5569.652292 |
21 | 30 | Togo | Africa | 549.452800 |
22 | 31 | Uganda | Africa | 5125.115492 |
23 | 32 | Tanzania | Africa | 9657.535962 |
24 | 33 | Zambia | Africa | 6783.757422 |
25 | 34 | Zimbabwe | Africa | 2339.247133 |
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
chord_fr = hv.Chord((links_fr, nodes_fr))
chord_fr.opts(
opts.Chord(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], finalize_hooks=[rotate_label2]))
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
options = dict(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], bgcolor='rgba(0,0,0,0)', border=0, finalize_hooks=[rotate_label2])
chord_fr = hv.Chord((links_fr, nodes_fr)).options(**options)
hv.renderer('bokeh').save(chord_fr, 'docs/_includes/chord_fr')
tot_ge = links_ge.groupby("source").sum().reset_index().rename(columns={"source":"name"})
tot_ge = tot_ge.append(links_ge[["target", "value"]].rename(columns={"target":"name"}))
tot_ge.replace({"United Republic of Tanzania":'Tanzania'}, inplace=True)
tot_ge.replace({"Central African Republic":'Centr. Afr. Rep.'}, inplace=True)
links_ge[["source"]] = enc_eu.transform(links_ge[["source"]])
links_ge[["target"]] = enc_af.transform(links_ge[["target"]]) + 10
nodes_ge_pd = nodes.data.copy()
nodes_ge_pd = nodes_ge_pd[~(nodes_ge_pd.name.isin(["France", "Italy", "United Kingdom", "Spain"]))]
nodes_ge_pd.drop(columns="value", inplace=True)
nodes_ge_pd = nodes_ge_pd.merge(tot_ge, on="name")
nodes_ge = hv.Dataset(nodes_ge_pd, "index")
nodes_ge.data.head()
index | name | group | value | |
---|---|---|---|---|
0 | 1 | Germany | Europe | 112066.319546 |
1 | 10 | Angola | Africa | 1787.136446 |
2 | 11 | Botswana | Africa | 348.906952 |
3 | 12 | Centr. Afr. Rep. | Africa | 1259.454702 |
4 | 13 | Chad | Africa | 4925.625179 |
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
chord_ge = hv.Chord((links_ge, nodes_ge))
chord_ge.opts(
opts.Chord(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], finalize_hooks=[rotate_label2]))
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
options = dict(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], bgcolor='rgba(0,0,0,0)', border=0, finalize_hooks=[rotate_label2])
chord_ge = hv.Chord((links_ge, nodes_ge)).options(**options)
hv.renderer('bokeh').save(chord_ge, 'docs/_includes/chord_ge')
tot_uk = links_uk.groupby("source").sum().reset_index().rename(columns={"source":"name"})
tot_uk = tot_uk.append(links_uk[["target", "value"]].rename(columns={"target":"name"}))
tot_uk.replace({"United Republic of Tanzania":'Tanzania'}, inplace=True)
tot_uk.replace({"Central African Republic":'Centr. Afr. Rep.'}, inplace=True)
links_uk[["source"]] = enc_eu.transform(links_uk[["source"]])
links_uk[["target"]] = enc_af.transform(links_uk[["target"]]) + 10
nodes_uk_pd = nodes.data.copy()
nodes_uk_pd = nodes_uk_pd[~(nodes_uk_pd.name.isin(["France", "Italy", "Germany", "Spain"]))]
nodes_uk_pd.drop(columns="value", inplace=True)
nodes_uk_pd = nodes_uk_pd.merge(tot_uk, on="name")
nodes_uk = hv.Dataset(nodes_uk_pd, "index")
nodes_uk.data.head()
index | name | group | value | |
---|---|---|---|---|
0 | 4 | United Kingdom | Europe | 101507.471800 |
1 | 10 | Angola | Africa | 1616.749298 |
2 | 11 | Botswana | Africa | 312.960106 |
3 | 12 | Centr. Afr. Rep. | Africa | 1138.393266 |
4 | 13 | Chad | Africa | 4461.863870 |
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
chord_uk = hv.Chord((links_uk, nodes_uk))
chord_uk.opts(
opts.Chord(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], finalize_hooks=[rotate_label2]))
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
options = dict(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], bgcolor='rgba(0,0,0,0)', border=0, finalize_hooks=[rotate_label2])
chord_uk = hv.Chord((links_uk, nodes_uk)).options(**options)
hv.renderer('bokeh').save(chord_uk, 'docs/_includes/chord_uk')
tot_es = links_es.groupby("source").sum().reset_index().rename(columns={"source":"name"})
tot_es = tot_es.append(links_es[["target", "value"]].rename(columns={"target":"name"}))
tot_es.replace({"United Republic of Tanzania":'Tanzania'}, inplace=True)
tot_es.replace({"Central African Republic":'Centr. Afr. Rep.'}, inplace=True)
links_es[["source"]] = enc_eu.transform(links_es[["source"]])
links_es[["target"]] = enc_af.transform(links_es[["target"]]) + 10
nodes_es_pd = nodes.data.copy()
nodes_es_pd = nodes_es_pd[~(nodes_es_pd.name.isin(["France", "Italy", "United Kingdom", "Germany"]))]
nodes_es_pd.drop(columns="value", inplace=True)
nodes_es_pd = nodes_es_pd.merge(tot_es, on="name")
nodes_es = hv.Dataset(nodes_es_pd, "index")
nodes_es.data.head()
index | name | group | value | |
---|---|---|---|---|
0 | 3 | Spain | Europe | 86235.003277 |
1 | 10 | Angola | Africa | 1370.083228 |
2 | 11 | Botswana | Africa | 260.635343 |
3 | 12 | Centr. Afr. Rep. | Africa | 963.030392 |
4 | 13 | Chad | Africa | 3791.108316 |
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
chord_es = hv.Chord((links_es, nodes_es))
chord_es.opts(
opts.Chord(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], finalize_hooks=[rotate_label2]))
from bokeh.models import HoverTool
hover = HoverTool(
tooltips=[
( 'Country', '@name' ),
( 'Group', '@group' ), # use @{ } for field names with spaces
( 'Import/Export', '@value tons'),
],
)
options = dict(cmap='Dark2', edge_cmap='Dark2', edge_color=dim('target').str(), node_size=12, edge_alpha=0.15,\
labels='name', node_color=dim('index').str(), edge_hover_line_color=None, node_hover_line_color=None, node_hover_fill_color=None,\
edge_selection_line_color=None, tools=[hover, 'tap'], bgcolor='rgba(0,0,0,0)', border=0, finalize_hooks=[rotate_label2])
chord_es = hv.Chord((links_es, nodes_es)).options(**options)
hv.renderer('bokeh').save(chord_es, 'docs/_includes/chord_es')