| 19 | |
| 20 | |
| 21 | def _plot(d_x2d, d_y, d_yunique, p_x2d, p_y, data_mode, plot_t, batch_cnt, legend, save_img_path, figsize=(8, 8)): |
| 22 | from matplotlib import pyplot as plt |
| 23 | import matplotlib |
| 24 | from matplotlib import rcParams |
| 25 | |
| 26 | matplotlib.rc('text', usetex=True) |
| 27 | matplotlib.rcParams['text.latex.preamble'] = [r"\usepackage{amsmath}"] |
| 28 | rcParams['font.family'] = 'DejaVu Serif' |
| 29 | rcParams['font.sans-serif'] = ['DejaVuSerif'] |
| 30 | |
| 31 | # PLOT |
| 32 | fig, ax = plt.subplots(figsize=figsize) |
| 33 | ax.axis('off') # No borders/nbs |
| 34 | |
| 35 | # plt.figure() |
| 36 | labels = ['Class {}'.format(i) for i in d_yunique] |
| 37 | # colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k', 'w', 'orange', 'purple'][:len(labels)] |
| 38 | colors = ['C{}'.format(i) for i in range(10)][:len(labels)] |
| 39 | plots = [] |
| 40 | for y, col, label in zip(d_yunique, colors, labels): |
| 41 | plot = plt.scatter(d_x2d[d_y == y, 0], d_x2d[d_y == y, 1], c=col, label=label, marker='.', alpha=0.3) |
| 42 | plots.append(plot) |
| 43 | |
| 44 | # ADD prototypes in different color/marker |
| 45 | if len(p_y) > 0: |
| 46 | pcol = 'black' |
| 47 | pmarker = 'o' |
| 48 | plt.scatter(p_x2d[:, 0], p_x2d[:, 1], c=pcol, marker=pmarker) |
| 49 | |
| 50 | for i in range(len(p_y)): |
| 51 | ax.annotate(r'${\bf p}^' + '{}$'.format(str(p_y[i])), xy=(p_x2d[i, 0], p_x2d[i, 1]), |
| 52 | xytext=(-10, 14), |
| 53 | textcoords='offset points', # ha='center', va='bottom', |
| 54 | bbox=dict(boxstyle='round,pad=0.2', fc='white', alpha=0.7), |
| 55 | size=22 |
| 56 | ) |
| 57 | |
| 58 | # LEGEND (exclude p) |
| 59 | if legend: |
| 60 | lgnd = plt.legend(plots, labels, |
| 61 | loc='center right', bbox_to_anchor=(1.5, 0.5), |
| 62 | fontsize=22, |
| 63 | prop={'size': 20}, |
| 64 | ) |
| 65 | |
| 66 | for hndlr in lgnd.legendHandles: |
| 67 | hndlr.set_alpha(1) |
| 68 | hndlr.set_sizes([80]) # Dot in the legend |
| 69 | |
| 70 | # SAVE/SHOW PLOT |
| 71 | plt.tight_layout() |
| 72 | |
| 73 | if save_img_path is not None: |
| 74 | import datetime |
| 75 | now = str(datetime.datetime.now().date()) + "_" + ':'.join(str(datetime.datetime.now().time()).split(':')[:-1]) |
| 76 | name = '{}_Task{}_batchcnt={}_{}'.format(data_mode, plot_t, batch_cnt, now) |
| 77 | # Save 3 formats |
| 78 | ext = '.pdf' |