Callback functions for formatting
cluster_id_if_cluster(data, linkage_id)
Returns cluster ID if a node belongs to one cluster, otherwise an empty string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
ClusteringData
|
idendrogram.ClusteringData object |
required |
linkage_id |
int
|
linkage ID |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Cluster ID or empty string. |
Source code in idendrogram/callbacks.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
|
cluster_labeller(fmt_string='Cluster {cluster} ({cluster_size} data points)')
Returns a callable designed to be used as a callback to axis_label_func
parameter of idendrogram.idendrogram.create_dendrogram.
Returns a formatted string for the first encountered node in a cluster, otherwise an empty string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fmt_string |
str
|
Formatting string. Variables available at the time of evaluation are |
'Cluster {cluster} ({cluster_size} data points)'
|
Returns:
Type | Description |
---|---|
Callable[[ClusteringData, int], str]
|
Callable[[ClusteringData, int], str]: Callable designed to be used as a callback to to |
Source code in idendrogram/callbacks.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
counts(data, linkage_id)
Returns the number of original observations associated with the linkage ID. Used as the default for axis label callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
ClusteringData
|
idendrogram.ClusteringData object |
required |
linkage_id |
int
|
linkage ID |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
number of original observations (as string) |
Source code in idendrogram/callbacks.py
5 6 7 8 9 10 11 12 13 14 15 16 |
|
default_hover(data, linkage_id)
For a given linkage ID, returns a dictionary with two keys: linkage id and # of items. Used as the default for tooltips.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
ClusteringData
|
idendrogram.ClusteringData object |
required |
linkage_id |
int
|
linkage ID |
required |
Returns:
Name | Type | Description |
---|---|---|
Dict |
Dict
|
Dictionary with attributes |
Source code in idendrogram/callbacks.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
link_painter(colors=dict(), above_threshold='#1f77b4')
Creates a callable compatible with link_color_func
argument of idendrogram.idendrogram
that will color nodes based on the cluster they belong to, with a separate color for nodes containing multiple clusters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
colors |
Dict
|
Dictionary mapping cluster IDs to colors. Defaults to Matplotlib 10-color scheme. |
dict()
|
above_threshold |
str
|
Color to be used for nodes containing multiple clusters. |
'#1f77b4'
|
Returns:
Type | Description |
---|---|
Callable[[ClusteringData, int], str]
|
Callable[[ClusteringData, int], str]: Callable to be used as |
Example
#your clustering workflow
Z = scipy.cluster.hierarchy.linkage(...)
cluster_assignments = scipy.cluster.hierarchy.fcluster(Z, threshold=threshold, ...)
# let's assume clustering resulted in 3 clusters and we want to have them as red/blue/green
# cluster_assignments.unique == 3
# define a custom coloring function
painter = idendrogram.callbacks.link_painter(
colors={
1: 'red',
2: 'blue',
3: 'green',
},
above_threshold='black'
)
#create the dendrogram
dd = idendrogram.idendrogram()
dd.set_cluster_info(
idendrogram.ClusteringData(
linkage_matrix=Z,
cluster_assignments=cluster_assignments,
threshold=threshold
)
)
dd.create_dendrogram(link_color_func = painter).to_plotly()
Source code in idendrogram/callbacks.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|