Drawing¶
Clusters¶
Useful functions to draw clusters
- plot_average_silhouettes(cluster_sets, ax=None)[source]¶
Plot the average silhouettes across this range of cluster sets
- Parameters
cluster_sets (ClusterSets)
ax (matplotlib.Axes, optional) – Axes on which to plot
- Returns
- Return type
None
- plot_cluster_set(cluster_set, ax=None, y_height=0, cmap=<matplotlib.colors.ListedColormap object>, number_of_colors=10, colors=None)[source]¶
Plots this cluster set as a scatter graph
- Parameters
cluster_set
ax (matplotlib.Axes, optional) – Axes on which to plot
y_height (int or float, optional) – Height at which to plot (default 0)
cmap (matplotlib.cm, optional) – Desired colour map (default ‘tab10’)
number_of_colors (int, optional) – Desired number of colours to use for the colormap (default 10)
- Returns
- Return type
None
- plot_cluster_sets(cluster_sets, axs=None, coloring='consistent', with_silhouettes=False, with_n_clusters=False)[source]¶
Plot these cluster sets as a scatter graph
- Parameters
cluster_sets (tempnetx ClusterSets)
axs (matplotlib.Axes, optional) – Matplotlib axes on which to plot
coloring (str, optional)
with_silhouettes (bool, optional)
with_n_clusters (bool, optional)
- Returns
- Return type
None
- plot_ns_clusters(cluster_sets, ax=None)[source]¶
Plot the average cluster set sizes across this range of cluster sets
- Parameters
cluster_sets (ClusterSets)
ax (matplotlib.Axes, optional) – Axes on which to plot
- Returns
- Return type
None
- plot_randindex_bars_over_methods_and_sizes(valid_cluster_sets, reference_method='ward', ax=None, plot_ref=False)[source]¶
Plot Rand Index as bars, to compare any method to a reference method, for all combinations of methods and number of clusters
- relabel_clusters_sorted(clusters, final_labels=None)[source]¶
Returns array of cluster labels sorted in order of appearance, with clusters unchanged
- Parameters
clusters (array of int) – Cluster labels
final_labels (array of int) – Sorted cluster labels
Example
>>> clusters = np.array([2, 2, 2, 3, 3, 1, 1, 1]) >>> relabel_clusters_sorted(clusters) [ 1 1 1 2 2 3 3 3 ]
- relabel_clustersets(cluster_sets, method='consistent')[source]¶
Relabels clusters in each cluster set, so that clusters are labeled consistently at different numbers of clusters.
This is especially useful when plotting cluster sets, to have consistent colouring.
- Parameters
cluster_sets (ClusterSets)
method ({‘consistent’, ‘ascending’})
- relabel_next_clusterset_sorted(cluster_sets, cluster_sets_sorted, i)[source]¶
Relabels the clusters in i+1th cluster set so that it is consistent with ith cluster set
This is especially useful when plotting cluster sets, to have consistent colouring.
- Parameters
cluster_sets (ClusterSets) – Original cluster sets
cluster_sets_sorted (ClusterSets) – Cluster sets being sorted, already sorted up to i-1
i (int)
- Returns
cluster_sets_sorted
- Return type
Networks¶
Functions to visualise networks and temporal networks
- animate_temporal_network(temporal_network, color_temporal='red', color_constant='silver', width_scale=1.5, with_labels=True, layout=None, ax=None, interval=20, frames=None)[source]¶
Return animation of the temporal network evolving over time
- Parameters
temporal_network (phasik.TemporalNetwork) – Temporal network to visualise
color_temporal (str) – Color of the time-varying edges, defaults to ‘red’
color_constant (str) – Color of the constant edges (defaults to ‘silver’), i.e. for which we have no temporal information
width_scale (float) – Scale factor for width of the temporal edges compared to the constant ones
layout (dict) – Dictionary of node positions
ax (matplotlib.axis) – Axes to plot the animation on
interval (int) – Interval of time between frames, in ms.
frames (int) – Number of frames of the animation (should be at most the number of timepoints (default))
- Returns
- Return type
matplotlib.animation
- draw_graph(graph, ax=None, label_nodes=True, color='mediumseagreen', layout=None)[source]¶
Basic graph drawing function
- Parameters
graph (networkx.Graph) – Graph to visualise
ax (matplotlib.Axes, optional) – Axes on which to draw the graph
label_nodes (bool, optional) – Whether to label the nodes or just leave them as small circles (default True)
color (str, optional) – Color to use for the graph nodes and edges (default ‘mediumseagreen’)
- Returns
- Return type
None
- highlight_subgraphs(graphs, colors, ax=None, layout=None, label_nodes=True)[source]¶
Draw multiple nested subgraphs on the same axes
- Parameters
graphs (list of networkx.Graph)
colors (list of str) – List of colors, one for each of the graphs in ‘graphs’
ax (matplotlib.Axes, optional) – Axes to plot on
label_nodes (bool, optional) – Whether or not to label the graph nodes or leave them as circles
- Returns
- Return type
None
- standard_edge_params(color)[source]¶
Returns a dictionary containing standard values of edge plotting parameters
- standard_label_params(color)[source]¶
Returns a dictionary containing standard values of label plotting parameters
Drawing¶
Useful drawing functions
- plot_events(events, ax=None, text_y_pos=None, text_x_offset=0, period=None, n_periods=1, add_labels=True, orientation='vertical', zorder=- 1, alpha=1, va='bottom')[source]¶
Plot events as vertical lines on axes
- Parameters
events (list of tuples (time, name, line_style)) – time - time at which the event occurred name - the name of the event line_style - any string accepted by matplotlib.lines.Line2D.set_linestyle
ax (matplotlib.Axes, optional) – Axes on which to plot the events
text_y_pos (float, optional) – Height at which to place the name of the event (default None)
text_x_offset (float, optional) – Distance along x-axis by which to offset the placement of the event name (default 0)
period (float or None, optional) – Length of time of one period, if events repeat periodically.
n_periods (int, optional) – Number of periods to draw, if events repeat periodically.
add_labels (bool, optional) – Wether to display the label of each vertical line, True by default.
- Returns
- Return type
None
- plot_interval(binary_series, times, y=0, peak=None, color='k', ax=None, zorder=0)[source]¶
Plot a binary series as a sequence of coloured intervals
Specifically, when a binary series has value 1, plot it as a continuous rectangular interval. When it has value 0 do nothing.
- Parameters
binary_series (ndarray) – 2D array of binary data to plot
times (ndarray) – 1D array consisting of the corresponding time points
y (float, optional) – Height (y-axis value) at which to plot the interval (default 0)
color (str, optional) – Color to use for the intervals (default ‘k’)
ax (matplotlib.Axes, optional) – Axes to plot on
zorder (int, optional) – Height on the z-axis which to plot the interval (default 0)
- Returns
- Return type
None
- plot_phases(phases, ax=None, y_pos=None, ymin=0, ymax=1, t_offset=0)[source]¶
Plot phases as shaded regions on axes
- Parameters
phases (list of tuples (start_time, end_time, name))
ax (matplotlib.Axes) – Axes on which to plot the phases
y_pos (float or None, optional) – Height at which to place the name of the phase
ymin (float, optional) – Height at which to start shaded region (default 0)
ymax (float, optional) – Height at which to stop shaded region (default 1)
t_offset (float, optional) – Offset of phase on the time axis
- Returns
- Return type
None
- threshold_plot(x, y, threshold, color_below_threshold, color_above_threshold, ax=None)[source]¶
Plot values above a certain threshold in a particular colour
- Parameters
x (array) – 1D array of alues to plot along x-axis
y (array) – 1D array of values to plot along y-axis
threshold (float) – Only plot in colour the points (x,y) with y >= threshold
colour_below_threshold (str) – Colour to use for points below threshold
colour_above_threshold (list of str) – Colour to use for points above threshold
ax (matplotlib.Axes, optional) – Axes to use
- Returns
- Return type
None
Utils¶
General utility functions for plots
- adjust_margin(ax=None, top=0, bottom=0, left=0, right=0)[source]¶
Extend the margin of a plot by a percentage of its original width/height
- Parameters
ax (matplotlib.Axes, optional) – Axes whose margins to adjust
top (float, optional) – Percentage (as decimal) by which to increase top margin
bottom (float, optional) – Percentage (as decimal) by which to increase bottom margin
left (float, optional) – Percentage (as decimal) by which to increase left margin
right (float, optional) – Percentage (as decimal) by which to increase right margin
- Returns
- Return type
None
Method to tidy up cases where we have a grid of plots with shared axes, by deleting unused subplots (if number of of subplots is not rectangular) and adding axes ticks
- Parameters
rows (int) – Number of rows in the grid of subplots
columns (int) – Number of columns in the grid of subplots
total_subplots (int) – Number of subplots in the grid; need not be a ‘rectangular’ number
xlabel (str) – Label of the x-axis
ylabel (str) – Label of the y-axis
fig (matplotlib.Figure) – Figure that the subplots are a part of
axes (list of matplotlib.Axes) – Axes containing the subplots
- Returns
- Return type
None