Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update plot.py #514

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions ot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ def plot1D_mat(a, b, M, title=''):
pl.subplots_adjust(wspace=0., hspace=0.2)


def plot2D_samples_mat(xs, xt, G, thr=1e-8, **kwargs):
def plot2D_samples_mat(xs, xt, G, draw_arrows: bool = True, thr=1e-8, **kwargs):
r""" Plot matrix :math:`\mathbf{G}` in 2D with lines using alpha values

Plot lines between source and target 2D samples with a color
proportional to the value of the matrix :math:`\mathbf{G}` between samples.


Parameters
----------
xs : ndarray, shape (ns,2)
Expand All @@ -75,6 +74,8 @@ def plot2D_samples_mat(xs, xt, G, thr=1e-8, **kwargs):
Target samples positions
G : ndarray, shape (na,nb)
OT matrix
draw_arrows : bool, optional
If True, draw directional arrows in the middle of the lines
thr : float, optional
threshold above which the line is drawn
**kwargs : dict
Expand All @@ -93,5 +94,17 @@ def plot2D_samples_mat(xs, xt, G, thr=1e-8, **kwargs):
for i in range(xs.shape[0]):
for j in range(xt.shape[0]):
if G[i, j] / mx > thr:
pl.plot([xs[i, 0], xt[j, 0]], [xs[i, 1], xt[j, 1]],
alpha=G[i, j] / mx * scale, **kwargs)
plt.plot([xs[i, 0], xt[j, 0]], [xs[i, 1], xt[j, 1]],
alpha=G[i, j] / mx * scale, **kwargs)

if draw_arrows:
# Calculate the midpoint
mid_x = (xs[i, 0] + xt[j, 0]) / 2
mid_y = (xs[i, 1] + xt[j, 1]) / 2

# Annotate with an arrowhead at the midpoint
plt.annotate('',
xy=(mid_x, mid_y),
xytext=(mid_x - 0.5 * (xt[j, 0] - xs[i, 0]), mid_y - 0.5 * (xt[j, 1] - xs[i, 1])),
arrowprops=dict(arrowstyle='-|>', color=kwargs['color'], alpha=G[i, j] / mx * scale)
)
Loading