Skip to content

Instantly share code, notes, and snippets.

@keimina
Created February 20, 2021 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keimina/78caef973a38356ff69b94d943937b9c to your computer and use it in GitHub Desktop.
Save keimina/78caef973a38356ff69b94d943937b9c to your computer and use it in GitHub Desktop.
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
data_points = np.array([[0,0],[0,1],[1,1],[1,0],[2,2],[2,3],[3,3],[3,2]])
df = pd.DataFrame(data_points, columns=["x", "y"])
fig, ax = plt.subplots(3, 8, figsize=(24,9), sharex=True, sharey=True)
df2 = df.copy()
df2 = df2.set_index(["x", "y"])
df2.loc[:, "xy"] = df2.index
df2.loc[:, "key"] = 1
df3 = pd.merge(df2, df2, on="key", suffixes=["_a", "_b"])
del df3["key"]
df3.loc[:,"pairs"] = df3.apply(lambda s: s.tolist(), axis=1)
df3 = df3.pivot(index="xy_a", columns="xy_b", values="pairs")
df4 = df3.copy()
df4 = df4.applymap(lambda x: np.array(x[0]) - np.array(x[1]))
df5 = df4.applymap(lambda x: np.linalg.norm(x))
df6 = df5.copy()
df6 = df6.applymap(lambda i: np.exp(-i**2))
df6.values[np.diag_indices_from(df6.values)] = 0.0
df6 = df6.apply(lambda s: s/s.sum(), axis=1)
# create low dimensional data point
np.random.seed(1)
data_points_2 = np.random.normal(size=(len(data_points), 1, 1)).tolist()
df7 = pd.DataFrame(data_points_2, columns=["xy"])
df7 = df7.applymap(lambda i: tuple(i) + tuple([0]))
df7.set_index(["xy"])
df7.loc[:, "key"] = 1
df7 = pd.merge(df7, df7, on="key", suffixes=["_a", "_b"])
del df7["key"]
df7.loc[:,"pairs"] = df7.apply(lambda s: s.tolist(), axis=1)
df7 = df7.pivot(index="xy_a", columns="xy_b", values="pairs")
def vec_diff(x):
return np.array(x[1]) - np.array(x[0])
def annotate1(i, ax):
ax.annotate("", xy=i[1], xytext=i[0], arrowprops=dict(arrowstyle="->"))
ax.text(*i[1], "{:.02f}".format(np.linalg.norm(vec_diff(i))))
def annotate2(s, ax, zero_index):
s2 = s.apply(lambda i: np.exp(-np.linalg.norm(vec_diff(i))**2))
s2.iloc[zero_index] = 0.0
s2 = s2/s2.sum()
s2 = s2.apply(lambda i: [i])
s += s2
s.apply(lambda i: ax.annotate("", xy=i[1], xytext=i[0], arrowprops=dict(arrowstyle="->")))
s.apply(lambda i: ax.text(*i[1], "{:.02f}".format(i[2])))
def annotate3(s, ax, zero_index):
s2 = s.apply(lambda i: np.exp(-np.linalg.norm(vec_diff(i))**2))
s2.iloc[zero_index] = 0.0
s2 = s2/s2.sum()
s2 = s2.apply(lambda i: [i])
s += s2
t = s.iloc[0]
s.apply(lambda i: ax.annotate("", xy=i[1], xytext=i[0], arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=-0.8")))
s.apply(lambda i: ax.text(*i[1], "{:.02f}".format(i[2])))
for i in range(8):
df_tmp = df3.iloc[[i],:]
df_tmp.applymap(lambda j: annotate1(j, ax[0][i]))
for i in range(8):
df_tmp = df3.iloc[[i],:]
df_tmp.apply(lambda s: annotate2(s, ax[1][i], i), axis=1)
df_tmp = df7.iloc[[0],:]
df_tmp.apply(lambda s: annotate3(s, ax[2][0], 0), axis=1)
# df2 = df2.index.to_frame()
#df2.loc[:,"xy"] = df2.apply(lambda r: r.tolist(), axis=1)
# visualize table and dataframe
set_aspect = np.vectorize(lambda ax: ax.set_aspect("equal"))
set_xlim = np.vectorize(lambda ax: ax.set_xlim([0,4]))
set_ylim = np.vectorize(lambda ax: ax.set_ylim([0,4]))
set_xlim(ax)
set_ylim(ax)
set_aspect(ax)
fig.subplots_adjust(hspace=0.0, wspace=0.0)
fig.savefig("./fig.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment