|
| 1 | +from typing import Iterable, Optional, Tuple |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import numpy as np |
| 4 | + |
| 5 | + |
| 6 | +def plot_degree_distribution( |
| 7 | + graph, |
| 8 | + *, |
| 9 | + x_scale: str = "log", |
| 10 | + y_scale: str = "log", |
| 11 | + ax: Optional[plt.Axes] = None, |
| 12 | + normalize: bool = True, |
| 13 | + include_zero_degree: bool = False, |
| 14 | + label: Optional[str] = None, |
| 15 | + marker: str = "o", |
| 16 | + markersize: float = 5.0, |
| 17 | +) -> Tuple[plt.Figure, plt.Axes]: |
| 18 | + """ |
| 19 | + Plot the degree distribution p(k) vs k for a single graph. |
| 20 | +
|
| 21 | + Parameters |
| 22 | + ---------- |
| 23 | + graph |
| 24 | + Object exposing `.degree()` -> 1-D numpy array of node degrees. |
| 25 | + x_scale, y_scale |
| 26 | + Matplotlib scales ("linear", "log", "symlog", etc.). Defaults "log" / "log". |
| 27 | + ax |
| 28 | + Optional axes to draw on. If None, a new figure and axes are created. |
| 29 | + normalize |
| 30 | + If True, plot probability mass p(k) = count(k) / N. If False, plot raw counts. |
| 31 | + include_zero_degree |
| 32 | + If True, include k=0 in the plot (useful if not using log scale). |
| 33 | + Note: when using log scale, k=0 cannot be shown and will be dropped. |
| 34 | + label |
| 35 | + Optional label for legend. |
| 36 | + marker |
| 37 | + Matplotlib marker style for the scatter points. |
| 38 | + markersize |
| 39 | + Size of scatter markers. |
| 40 | +
|
| 41 | + Returns |
| 42 | + ------- |
| 43 | + (fig, ax) |
| 44 | + The Matplotlib figure and axes used for plotting. |
| 45 | +
|
| 46 | + Notes |
| 47 | + ----- |
| 48 | + - When `x_scale` or `y_scale` is "log", any k=0 or p(k)=0 entries are removed |
| 49 | + to avoid invalid values on a log axis. |
| 50 | + """ |
| 51 | + degrees = graph.degree() |
| 52 | + if degrees.ndim != 1: |
| 53 | + raise ValueError("graph.degree() must return a 1-D array of degrees.") |
| 54 | + if degrees.size == 0: |
| 55 | + # Create empty plot but still return fig/ax for consistency |
| 56 | + if ax is None: |
| 57 | + fig, ax = plt.subplots() |
| 58 | + else: |
| 59 | + fig = ax.figure |
| 60 | + ax.set_xlabel("Degree k") |
| 61 | + ax.set_ylabel("p(k)" if normalize else "count(k)") |
| 62 | + ax.set_xscale(x_scale) |
| 63 | + ax.set_yscale(y_scale) |
| 64 | + ax.set_title(getattr(graph, "name", "Degree distribution (empty graph)")) |
| 65 | + return fig, ax |
| 66 | + |
| 67 | + if np.any(degrees < 0): |
| 68 | + raise ValueError("Degrees must be nonnegative.") |
| 69 | + |
| 70 | + # Compute counts per unique degree efficiently |
| 71 | + # Using np.bincount for speed (requires nonnegative ints) |
| 72 | + if not np.issubdtype(degrees.dtype, np.integer): |
| 73 | + # allow float degrees that are whole numbers |
| 74 | + if np.all(np.mod(degrees, 1) == 0): |
| 75 | + degrees = degrees.astype(int) |
| 76 | + else: |
| 77 | + raise ValueError("Degrees must be integers for degree distributions.") |
| 78 | + |
| 79 | + counts = np.bincount(degrees) # index = k, value = count(k) |
| 80 | + ks = np.nonzero(counts)[0] # degrees that actually appear |
| 81 | + freqs = counts[ks].astype(float) |
| 82 | + |
| 83 | + if normalize: |
| 84 | + total = freqs.sum() |
| 85 | + if total == 0: |
| 86 | + raise ValueError("No nodes with valid degrees found.") |
| 87 | + pk = freqs / total |
| 88 | + else: |
| 89 | + pk = freqs |
| 90 | + |
| 91 | + # Handle zero-degree inclusion/exclusion |
| 92 | + if not include_zero_degree or x_scale == "log": |
| 93 | + mask = ks > 0 |
| 94 | + ks, pk = ks[mask], pk[mask] |
| 95 | + |
| 96 | + # On log y, remove zero probabilities (shouldn't occur if computed as above) |
| 97 | + if y_scale == "log": |
| 98 | + nz = pk > 0 |
| 99 | + ks, pk = ks[nz], pk[nz] |
| 100 | + |
| 101 | + # Prepare axes |
| 102 | + if ax is None: |
| 103 | + fig, ax = plt.subplots() |
| 104 | + else: |
| 105 | + fig = ax.figure |
| 106 | + |
| 107 | + ax.scatter(ks, pk, marker=marker, s=markersize**2, label=label) |
| 108 | + ax.set_xlabel("Degree k") |
| 109 | + ax.set_ylabel("p(k)" if normalize else "count(k)") |
| 110 | + ax.set_xscale(x_scale) |
| 111 | + ax.set_yscale(y_scale) |
| 112 | + |
| 113 | + title_default = getattr(graph, "name", None) |
| 114 | + if title_default: |
| 115 | + ax.set_title(f"Degree distribution: {title_default}") |
| 116 | + else: |
| 117 | + ax.set_title("Degree distribution") |
| 118 | + |
| 119 | + if label is not None: |
| 120 | + ax.legend() |
| 121 | + |
| 122 | + ax.grid(True, which="both", linestyle=":", linewidth=0.5, alpha=0.6) |
| 123 | + |
| 124 | + return fig, ax |
| 125 | + |
| 126 | + |
| 127 | +def plot_degree_distributions_grid( |
| 128 | + graphs: Iterable, |
| 129 | + *, |
| 130 | + ncols: int = 3, |
| 131 | + x_scale: str = "log", |
| 132 | + y_scale: str = "log", |
| 133 | + normalize: bool = True, |
| 134 | + include_zero_degree: bool = False, |
| 135 | + figsize: Optional[Tuple[float, float]] = None, |
| 136 | + tight_layout: bool = True, |
| 137 | + sharex: bool = False, |
| 138 | + sharey: bool = False, |
| 139 | +) -> Tuple[plt.Figure, np.ndarray]: |
| 140 | + """ |
| 141 | + Plot a grid of degree distribution plots for multiple graphs. |
| 142 | +
|
| 143 | + Parameters |
| 144 | + ---------- |
| 145 | + graphs |
| 146 | + Iterable of Graph-like objects exposing `.degree() -> np.ndarray`. |
| 147 | + ncols |
| 148 | + Number of columns in the grid. |
| 149 | + x_scale, y_scale |
| 150 | + Axis scales for all subplots (defaults to "log"). |
| 151 | + normalize |
| 152 | + If True, plot probability mass p(k). If False, raw counts. |
| 153 | + include_zero_degree |
| 154 | + Whether to include k=0 (will be dropped automatically on log x). |
| 155 | + figsize |
| 156 | + Optional figure size. If None, inferred from grid size. |
| 157 | + tight_layout |
| 158 | + Whether to call `fig.tight_layout()` at the end. |
| 159 | + sharex, sharey |
| 160 | + Whether to share x/y axes across subplots. |
| 161 | +
|
| 162 | + Returns |
| 163 | + ------- |
| 164 | + (fig, axes) |
| 165 | + Figure and 2D ndarray of Axes (some entries may be unused if the grid |
| 166 | + is larger than the number of graphs). |
| 167 | + """ |
| 168 | + graphs = list(graphs) |
| 169 | + n = len(graphs) |
| 170 | + if n == 0: |
| 171 | + raise ValueError("`graphs` must contain at least one graph.") |
| 172 | + |
| 173 | + nrows = np.ceil(n / ncols) |
| 174 | + if figsize is None: |
| 175 | + # heuristic: wider for more columns, taller for more rows |
| 176 | + figsize = (4 * ncols, 3.2 * nrows) |
| 177 | + |
| 178 | + fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=figsize, sharex=sharex, sharey=sharey) |
| 179 | + |
| 180 | + # Ensure axes is 2D array for consistent indexing |
| 181 | + if isinstance(axes, plt.Axes): |
| 182 | + axes = np.array([[axes]]) |
| 183 | + elif axes.ndim == 1: |
| 184 | + axes = axes.reshape(1, -1) |
| 185 | + |
| 186 | + # Plot each graph |
| 187 | + for i, graph in enumerate(graphs): |
| 188 | + r, c = divmod(i, ncols) |
| 189 | + ax = axes[r, c] |
| 190 | + label = getattr(graph, "label", None) # optional custom label field |
| 191 | + plot_degree_distribution( |
| 192 | + graph, |
| 193 | + x_scale=x_scale, |
| 194 | + y_scale=y_scale, |
| 195 | + ax=ax, |
| 196 | + normalize=normalize, |
| 197 | + include_zero_degree=include_zero_degree, |
| 198 | + label=label, |
| 199 | + ) |
| 200 | + # Prefer a clean, concise title per subplot |
| 201 | + title = getattr(graph, "name", None) or f"Graph {i+1}" |
| 202 | + ax.set_title(title) |
| 203 | + |
| 204 | + # Hide any unused axes |
| 205 | + for j in range(n, nrows * ncols): |
| 206 | + r, c = divmod(j, ncols) |
| 207 | + axes[r, c].set_visible(False) |
| 208 | + |
| 209 | + if tight_layout: |
| 210 | + fig.tight_layout() |
| 211 | + |
| 212 | + return fig, axes |
0 commit comments