-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_speakers.py
64 lines (57 loc) · 1.59 KB
/
plot_speakers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from umap import UMAP
import matplotlib.pyplot as plt
import numpy as np
_default_colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]
_my_colors = (
np.array(
[
[0, 127, 70],
[255, 0, 0],
[255, 217, 38],
[0, 135, 255],
[165, 0, 165],
[255, 167, 255],
[97, 142, 151],
[0, 255, 255],
[255, 96, 38],
[142, 76, 0],
[33, 0, 127],
[0, 0, 0],
[183, 183, 183],
[76, 255, 0],
],
dtype=float,
)
/ 255
)
def get_speaker_plot(
embeds,
speakers,
ax=None,
colors=None,
markers=None,
legend=True,
title="",
**kwargs
):
if ax is None:
_, ax = plt.subplots(figsize=(6, 6))
# Compute the 2D projections. You could also project to another number of dimensions (e.g.
# for a 3D plot) or use a different different dimensionality reduction like PCA or TSNE.
reducer = UMAP(**kwargs)
projs = reducer.fit_transform(embeds)
# Draw the projections
speakers = np.array(speakers)
colors = colors or _my_colors
for i, speaker in enumerate(np.unique(speakers)):
speaker_projs = projs[speakers == speaker]
marker = "o" if markers is None else markers[i]
label = speaker if legend else None
ax.scatter(*speaker_projs.T, c=[colors[i]], marker=marker, label=label)
if legend:
ax.legend(title="Speakers", ncol=2)
ax.set_title(title)
ax.set_xticks([])
ax.set_yticks([])
ax.set_aspect("equal")
return ax