| 1 |
""" |
| 2 |
Functions for plotting OT matrices |
| 3 |
|
| 4 |
.. warning:: |
| 5 |
Note that by default the module is not import in :mod:`ot`. In order to |
| 6 |
use it you need to explicitly import :mod:`ot.plot` |
| 7 |
|
| 8 |
|
| 9 |
""" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
import numpy as np |
| 16 |
import matplotlib.pylab as pl |
| 17 |
from matplotlib import gridspec |
| 18 |
|
| 19 |
|
| 20 |
def plot1D_mat( |
| 21 |
a, |
| 22 |
b, |
| 23 |
M, |
| 24 |
title="", |
| 25 |
plot_style="yx", |
| 26 |
a_label="", |
| 27 |
b_label="", |
| 28 |
color_source="b", |
| 29 |
color_target="r", |
| 30 |
coupling_cmap="gray_r", |
| 31 |
): |
| 32 |
r"""Plot matrix :math:`\mathbf{M}` with the source and target 1D distributions. |
| 33 |
|
| 34 |
Creates a subplot with the source distribution :math:`\mathbf{a}` and target |
| 35 |
distribution :math:`\mathbf{b}`t. |
| 36 |
In 'yx' mode (default), the source is on the left and |
| 37 |
the target on the top, and in 'xy' mode, source on the bottom (upside |
| 38 |
down) and the target on the left. |
| 39 |
The matrix :math:`\mathbf{M}` is shown in between. |
| 40 |
|
| 41 |
Parameters |
| 42 |
---------- |
| 43 |
a : ndarray, shape (na,) |
| 44 |
Source distribution |
| 45 |
b : ndarray, shape (nb,) |
| 46 |
Target distribution |
| 47 |
M : ndarray, shape (na, nb) |
| 48 |
Matrix to plot |
| 49 |
title : str, optional |
| 50 |
Title of the plot |
| 51 |
plot_style : str, optional |
| 52 |
Style of the plot, 'yx' or 'xy'. 'yx' places the source on the left and |
| 53 |
the target on the top, 'xy' places the source on the bottom (upside |
| 54 |
down) and the target on the left. |
| 55 |
a_label : str, optional |
| 56 |
Label for source distribution |
| 57 |
b_label : str, optional |
| 58 |
Label for target distribution |
| 59 |
color_source : str, optional |
| 60 |
Color of the source distribution |
| 61 |
color_target : str, optional |
| 62 |
Color of the target distribution |
| 63 |
coupling_cmap : str, optional |
| 64 |
Colormap for the coupling matrix |
| 65 |
|
| 66 |
Returns |
| 67 |
------- |
| 68 |
ax1 : source plot ax |
| 69 |
ax2 : target plot ax |
| 70 |
ax3 : coupling plot ax |
| 71 |
|
| 72 |
.. seealso:: |
| 73 |
:func:`rescale_for_imshow_plot` |
| 74 |
""" |
| 75 |
assert plot_style in ["yx", "xy"], "plot_style should be 'yx' or 'xy'" |
| 76 |
na, nb = M.shape |
| 77 |
|
| 78 |
gs = gridspec.GridSpec( |
| 79 |
3, 3, height_ratios=[1, 1, 1], width_ratios=[1, 1, 1], hspace=0, wspace=0 |
| 80 |
) |
| 81 |
|
| 82 |
xa = np.arange(na) |
| 83 |
xb = np.arange(nb) |
| 84 |
|
| 85 |
|
| 86 |
def _set_ticks_and_spines(ax, empty_ticks=True, visible_spines=False): |
| 87 |
if empty_ticks: |
| 88 |
ax.set_xticks(()) |
| 89 |
ax.set_yticks(()) |
| 90 |
|
| 91 |
ax.spines["top"].set_visible(visible_spines) |
| 92 |
ax.spines["right"].set_visible(visible_spines) |
| 93 |
ax.spines["bottom"].set_visible(visible_spines) |
| 94 |
ax.spines["left"].set_visible(visible_spines) |
| 95 |
|
| 96 |
if plot_style == "xy": |
| 97 |
|
| 98 |
ax1 = pl.subplot(gs[2, 1:]) |
| 99 |
ax1.plot(xa, np.max(a) - a, color=color_source, linewidth=2) |
| 100 |
ax1.fill( |
| 101 |
xa, |
| 102 |
np.max(a) - a, |
| 103 |
np.max(a) * np.ones_like(a), |
| 104 |
color=color_source, |
| 105 |
alpha=0.5, |
| 106 |
) |
| 107 |
ax1.set_title(a_label, y=-0.15) |
| 108 |
|
| 109 |
|
| 110 |
ax2 = pl.subplot(gs[0:2, 0]) |
| 111 |
ax2.plot(b, xb, color=color_target, linewidth=2) |
| 112 |
ax2.fill(b, xb, color=color_target, alpha=0.5) |
| 113 |
ax2.invert_xaxis() |
| 114 |
ax2.invert_yaxis() |
| 115 |
ax2.set_title(b_label) |
| 116 |
|
| 117 |
_set_ticks_and_spines(ax1, empty_ticks=True, visible_spines=False) |
| 118 |
_set_ticks_and_spines(ax2, empty_ticks=True, visible_spines=False) |
| 119 |
|
| 120 |
|
| 121 |
ax3 = pl.subplot(gs[0:2, 1:], sharey=ax2, sharex=ax1) |
| 122 |
ax3.imshow(M.T, interpolation="nearest", origin="lower", cmap=coupling_cmap) |
| 123 |
ax3.set_title(title) |
| 124 |
_set_ticks_and_spines(ax3, empty_ticks=False, visible_spines=True) |
| 125 |
|
| 126 |
pl.subplots_adjust(hspace=0, wspace=0) |
| 127 |
return ax1, ax2, ax3 |
| 128 |
|
| 129 |
else: |
| 130 |
|
| 131 |
ax1 = pl.subplot(gs[1:, 0]) |
| 132 |
ax1.plot(a, xa, color=color_source, linewidth=2) |
| 133 |
ax1.fill(a, xa, color=color_source, alpha=0.5) |
| 134 |
ax1.invert_xaxis() |
| 135 |
ax1.set_title(a_label) |
| 136 |
|
| 137 |
|
| 138 |
ax2 = pl.subplot(gs[0, 1:]) |
| 139 |
ax2.plot(xb, b, color=color_target, linewidth=2) |
| 140 |
ax2.fill(xb, b, color=color_target, alpha=0.5) |
| 141 |
ax2.set_title(b_label) |
| 142 |
|
| 143 |
_set_ticks_and_spines(ax1, empty_ticks=True, visible_spines=False) |
| 144 |
_set_ticks_and_spines(ax2, empty_ticks=True, visible_spines=False) |
| 145 |
|
| 146 |
|
| 147 |
ax3 = pl.subplot(gs[1:, 1:], sharey=ax1, sharex=ax2) |
| 148 |
ax3.imshow(M, interpolation="nearest", cmap=coupling_cmap) |
| 149 |
|
| 150 |
ax3.text( |
| 151 |
0.5, |
| 152 |
-0.025, |
| 153 |
title, |
| 154 |
ha="center", |
| 155 |
va="top", |
| 156 |
transform=ax3.transAxes, |
| 157 |
fontsize="large", |
| 158 |
) |
| 159 |
_set_ticks_and_spines(ax3, empty_ticks=False, visible_spines=True) |
| 160 |
|
| 161 |
pl.subplots_adjust(hspace=0, wspace=0) |
| 162 |
return ax1, ax2, ax3 |
| 163 |
|
| 164 |
|
| 165 |
def rescale_for_imshow_plot(x, y, n, m=None, a_y=None, b_y=None): |
| 166 |
r""" |
| 167 |
Gives arrays xr, yr that can be plotted over an (n, m) |
| 168 |
imshow plot (in 'xy' coordinates). If `a_y` or `b_y` is provided, |
| 169 |
y is sliced over its indices such that y stays in [ay, by]. |
| 170 |
|
| 171 |
Parameters |
| 172 |
---------- |
| 173 |
x : ndarray, shape (nx,) |
| 174 |
y : ndarray, shape (nx,) |
| 175 |
n : int |
| 176 |
x-axis size of the imshow plot on which to plot (x, y) |
| 177 |
m : int, optional |
| 178 |
y-axis size of the imshow plot, defaults to n |
| 179 |
a_y : float, optional |
| 180 |
Lower bound for y |
| 181 |
b_y : float, optional |
| 182 |
Upper bound for y |
| 183 |
|
| 184 |
Returns |
| 185 |
------- |
| 186 |
xr : ndarray, shape (nx,) |
| 187 |
Rescaled x values (due to slicing, may have less elements than x) |
| 188 |
yr : ndarray, shape (nx,) |
| 189 |
Rescaled y values (due to slicing, may have less elements than y) |
| 190 |
|
| 191 |
.. seealso:: |
| 192 |
:func:`plot1D_mat` |
| 193 |
|
| 194 |
""" |
| 195 |
|
| 196 |
a_x, b_x = np.min(x), np.max(x) |
| 197 |
assert x.shape[0] == y.shape[0], "x and y arrays should have the same size" |
| 198 |
if a_y is None: |
| 199 |
a_y = np.min(y) |
| 200 |
if b_y is None: |
| 201 |
b_y = np.max(y) |
| 202 |
if m is None: |
| 203 |
m = n |
| 204 |
idx = (y >= a_y) & (y <= b_y) |
| 205 |
x_rescaled = (x[idx] - a_x) * (n - 1) / (b_x - a_x) |
| 206 |
y_rescaled = (y[idx] - a_y) * (m - 1) / (b_y - a_y) |
| 207 |
return x_rescaled, y_rescaled |
| 208 |
|
| 209 |
|
| 210 |
def plot2D_samples_mat(xs, xt, G, thr=1e-8, **kwargs): |
| 211 |
r"""Plot matrix :math:`\mathbf{G}` in 2D with lines using alpha values |
| 212 |
|
| 213 |
Plot lines between source and target 2D samples with a color |
| 214 |
proportional to the value of the matrix :math:`\mathbf{G}` between samples. |
| 215 |
|
| 216 |
|
| 217 |
Parameters |
| 218 |
---------- |
| 219 |
xs : ndarray, shape (ns,2) |
| 220 |
Source samples positions |
| 221 |
b : ndarray, shape (nt,2) |
| 222 |
Target samples positions |
| 223 |
G : ndarray, shape (na,nb) |
| 224 |
OT matrix |
| 225 |
thr : float, optional |
| 226 |
threshold above which the line is drawn |
| 227 |
**kwargs : dict |
| 228 |
parameters given to the plot functions (default color is black if |
| 229 |
nothing given) |
| 230 |
""" |
| 231 |
|
| 232 |
if ("color" not in kwargs) and ("c" not in kwargs): |
| 233 |
kwargs["color"] = "k" |
| 234 |
mx = G.max() |
| 235 |
if "alpha" in kwargs: |
| 236 |
scale = kwargs["alpha"] |
| 237 |
del kwargs["alpha"] |
| 238 |
else: |
| 239 |
scale = 1 |
| 240 |
for i in range(xs.shape[0]): |
| 241 |
for j in range(xt.shape[0]): |
| 242 |
if G[i, j] / mx > thr: |
| 243 |
pl.plot( |
| 244 |
[xs[i, 0], xt[j, 0]], |
| 245 |
[xs[i, 1], xt[j, 1]], |
| 246 |
alpha=G[i, j] / mx * scale, |
| 247 |
**kwargs, |
| 248 |
) |