cachai.chplot.chord
- cachai.chplot.chord(corr_matrix, names=None, colors=None, *, ax=None, radius=1, position=(0, 0), optimize=True, filter=True, bezier_n=30, show_diag=False, threshold=0.1, node_linewidth=10, node_gap=0.1, node_labelpad=0.2, blend=True, blend_resolution=200, chord_linewidth=1, chord_alpha=0.7, off_alpha=0.1, positive_hatch=None, negative_hatch='---', fontsize=15, font=None, min_dist=np.float64(0.2617993877991494), scale='linear', max_rho=0.4, max_rho_radius=0.7, show_axis=False, legend=False, positive_label=None, negative_label=None, rasterized=False, **kwargs)[source]
A Chord Diagram from a correlation matrix, with customizable threshold, style, and colors.
In science, high-dimensional data are common, the choice of visualization tools directly affects both interpretation and communication. Chord diagrams are particularly valuable for illustrating weighted, non-directional connections — such as (anti-)correlations — between variables, treating parameters as nodes and their correlations as links.
- Parameters
- corr_matrix
numpy.ndarrayorpandas.DataFrame Correlation matrix for the chord diagram. This matrix has to be 2-dimensional, not empty, symmetric, and filled just with
intorfloatvalues.- names / n
list, optional Names for each node (default: ‘Ni’ for the i-th node)
- colors / c
list, optional Custom colors for nodes (default: seaborn hls palette)
- ax
matplotlib.axes.Axes, optional Axes to plot on (default: current pyplot axis)
- corr_matrix
- Returns
ChordDiagram[source]An instance of the ChordDiagram class
- Other Parameters
- radius / r
float Radius of the diagram (default: 1.0)
- position / p
tuple Position of the center of the diagram (default: (0,0))
- optimize
bool Whether to optimize node order (default: True)
- filter
bool Whether to remove nodes with no correlation (default: True)
- bezier_n
int Bezier curve resolution (default: 30)
- show_diag
bool Show self-connections (default: False)
- threshold / th
float Minimum correlation threshold to display (default: 0.1)
- node_linewidth / nlw
float Line width for nodes (default: 10)
- node_gap / ngap
float Gap between nodes (0-1) (default: 0.1)
- node_labelpad / npad
float Label position adjustment (default: 0.2)
- blend
bool Whether to blend chord colors (default: True)
- blend_resolution
int Color blend resolution (default: 200)
- chord_linewidth / clw
float Line width for chords (default: 1)
- chord_alpha / calpha
float Alpha of the facecolor for chords (default: 0.7)
- off_alpha
float Alpha for non-highlighted chords (default: 0.1)
- positive_hatch
str Hatch for positive correlated chords (default: None)
- negative_hatch
str Hatch for negative correlated chords (default: ‘—‘)
- fontsize
int Label font size (default: 15)
- font
dictorstr Label font parameters (default: None)
- min_dist
float Minimum angle distance from which apply radius rule (default: 15 [degrees])
- scale
str Scale use to set chord’s thickness, wheter “linear” or “log” (default: “linear”)
- max_rho
float Maximum chord’s thickness (default: 0.4)
- max_rho_radius
float Maximum normalized radius of the chords relative to center (default: 0.7)
- show_axis
bool Whether to show the axis (default: False)
- legend
bool Adds default positive and negative labels in the legend (default: False)
- positive_label
str Adds positive label in the legend (default: None)
- negative_label
str Adds negative label in the legend (default: None)
- rasterized
bool Whether to force rasterized (bitmap) drawing for vector graphics output (default: False)
- radius / r
Examples
First, import cachai and the necessary libraries.
import matplotlib.pyplot as plt import cachai.chplot as chp
The only mandatory parameter for
chord()is the correlation matrix. Assuming you already have your matrix, this line will show the default plot (in some IDLEs you need to addplt.show()):chp.chord(corr_matrix)
Now, if you want to add a legend distinguishing positive and negative correlations, just set
legend=True. Check the next example:plt.figure(figsize=(6,6)) chp.chord(corr_matrix, threshold=0.3, negative_hatch='///', legend=True, rasterized=True) plt.legend(loc='center',bbox_to_anchor=[0.5,0],ncols=2,fontsize=13,handletextpad=0) plt.show()
To see more examples on how to use
chplot.chord()check out the examples section.Methods
- ChordDiagram.highlight_node(node, chords=None, alpha=None)[source]
Highlights a specific node. This affects the node and all its chords. If you want to highlight only some chords in the node, you can indicate this with
chords.Nodes are indexed based on their circular arrangement around the origin (center of the Chord Diagram). Index
0corresponds to the first node in the first quadrant, with numbering proceeding counterclockwise around the diagram. Chords within each node are also indexed counterclockwise, starting from the outermost chord.- Parameters
- node
int Index of the node to highlight (starting in 0).
- chords
listorarray-like, optional List of chord indices to highlight (default: all chords).
- alpha
float, optional Transparency level for highlighting.
- node
- ChordDiagram.highlight_chord(node, chord, alpha=None)[source]
Highlights a specific chord connected to a particular node.
- Parameters
- node
int Index of the node where the chord originates.
- chord
int Index of the chord to highlight (starting in 0).
- alpha
float, optional Transparency level for highlighting.
- node
- ChordDiagram.set_chord_alpha(alpha)[source]
Sets the transparency level for all chords in the diagram.
- Parameters
- alpha
float Transparency value applied to all chords.
- alpha