def visualize( self, fig: plt.Figure, X: np.ndarray, energy: Callable[[np.ndarray], np.ndarray] = None, ): """ Visualize a set of samples, returning a figure. Args: X (tensor ~ (n_samples, n_features): Returns: Figure """ fig.clear() ax = fig.subplots(1, 1) if X.shape[1] == 1: ax.hist(X.reshape([-1]), density=True, label="Data") x_min = X.min() x_max = X.max() xs = np.linspace(x_min, x_max, 100) if hasattr(self, "logpdf"): ys_ = np.exp(self.logpdf(xs.reshape([-1, 1]))) ys = ys_.reshape(-1) ax.plot(xs, ys, label="Actual") if energy is not None: ys_ = np.exp(-energy(xs.reshape([-1, 1]))) ys = ys_.reshape(-1) Z = ys_.mean() * (x_max - x_min) ax.plot(xs, ys / Z, label="Energy", color="red") ax.legend() elif X.shape[1] == 2: ax.scatter(X[:, 0], X[:, 1], label="Data") x_min, x_max = X[:, 0].min(), X[:, 0].max() y_min, y_max = X[:, 1].min(), X[:, 1].max() x_support = np.linspace(x_min, x_max, 100) y_support = np.linspace(y_min, y_max, 100) xx, yy = np.meshgrid(x_support, y_support) XY = np.hstack([xx.reshape([-1, 1]), yy.reshape([-1, 1])]) if hasattr(self, "logpdf"): z_ = np.exp(self.logpdf(XY)) z = z_.reshape(xx.shape) ax.contour(xx, yy, z, 10) if energy is not None: z_ = np.exp(-energy(XY)) z = z_.reshape(xx.shape) ax.contour(xx, yy, z, 10, cmap="Reds") ax.legend() else: from sklearn.manifold import TSNE tsne = TSNE(n_components=2) emb = tsne.fit_transform(X) ax.scatter(emb[:, 0], emb[:, 1])
def plot_image_samples(im_size: Tuple[int, ...], binarize, fig: plt.Figure, X: np.ndarray, energy=None): """Plots model samples on the given figure.""" fig.clear() if binarize: X = X > 0.5 n = min(int(sqrt(X.shape[0])), 7) ax = fig.subplots(ncols=n, nrows=n) for i in range(n): for j in range(n): ax[i, j].imshow(X[i + n * j, :].reshape(im_size), cmap="gray", vmin=-1, vmax=1) ax[i, j].axis("off")
def npsPlotByKey(pkg: NotePkg, fig:plt.Figure = None, shape: Tuple = None, window=1000, stride=None, title=True, legend=True, barKwargs=None) -> plt.Figure: """ This creates an NPS Plot with the axes. :param pkg: Any Note Package :param fig: The figure to plot on, if None, we use gcf() :param shape: The shape of the axes to take. (rows, columns) :param window: The window of the roll :param stride: The stride length of the roll :param title: Whether to show the key titles :param legend: Whether to show legend. False to show none, True to show on first, 'all' to show on all :param barKwargs: The kwargs to pass into plot() """ if fig is None: fig = plt.gcf() if barKwargs is None: barKwargs = {} keys = pkg.maxColumn() + 1 # This gives us the keys if shape is None: rows = keys cols = 1 shape = (rows, cols) else: assert shape[0] * shape[1] >= keys, "Shape must be able to hold all keys." ax: np.ndarray = fig.subplots(nrows=shape[0], ncols=shape[1], sharex='all', sharey='all') ax = ax.flatten() for key in range(keys): if legend == 'all': npsPlot(pkg.inColumns([key]), ax=ax[key], window=window, stride=stride, legend=True, barKwargs=barKwargs) elif legend is True and key == 0: npsPlot(pkg.inColumns([key]), ax=ax[key], window=window, stride=stride, legend=True, barKwargs=barKwargs) else: npsPlot(pkg.inColumns([key]), ax=ax[key], window=window, stride=stride, legend=False, barKwargs=barKwargs) ax: List[plt.Axes] if title: ax[key].set_title(f"Key: {key}") fig.tight_layout() return fig
from matplotlib.backends.backend_tkagg import ( FigureCanvasTkAgg, # not FigureCanvasTk NavigationToolbar2Tk # not NavigationToolbar2TkAgg ) import tkinter as tk # --- random data --- import random y = [random.randint(-100, 100) for x in range(200)] # --- GUI --- root = tk.Tk() #fig = plt.Figure() fig = Figure() canvas = FigureCanvasTkAgg(fig, root) canvas.get_tk_widget().pack() navbar = NavigationToolbar2Tk(canvas, root) #navbar.update() # not needed #ax = fig.add_subplot(111) ax = fig.subplots() ax.plot(y) root.mainloop()