def set_labels(ylabel: str, xlabel: str): for i, y, x in [ (1, True, False), (2, False, False), (3, True, False), (4, False, False), (5, True, True), (6, False, True), ]: plt.subplot(3, 2, i) ax = plt.gca() if y: plt.ylabel(ylabel) else: ax.axes.yaxis.set_ticklabels([]) if x: plt.xlabel(xlabel) ax.axes.xaxis.set_ticklabels([]) ymin, ymax = np.inf, -np.inf for i in range(1, 6 + 1): plt.subplot(3, 2, i) ymin = min(ymin, plt.ylim()[0]) ymax = max(ymax, plt.ylim()[1]) for i in range(1, 6 + 1): plt.subplot(3, 2, i) plt.ylim((ymin, ymax))
def demo(name, mu, sigma, strategies): t0 = time() Q, R, A = multistrat(mu=mu, sigma=sigma, strategies=strategies, epochs=1100) multiplot(A, R, strategies) plt.ylim(1) print name, "ran in", time() - t0, "s" show(name.replace(' ', '_')) plot_qs(Q, mu, strategies) show(name.replace(' ', '_') + "-q")
def matrix_subplots( c: Config, resp_matrix: ResponsesMatrix, num_subplots: int, num_x: int, z_frac: float, rows: int = 4, save: str = None, plot_func=None, ): """For each subplot plot matrix fem using the given function.""" cols = int(num_subplots / rows) if cols != num_subplots / rows: print_w(f"Rows don't divide number of simulations, cols = {cols}" + f", sims = {num_subplots / rows}" + f", num_subplots = {num_subplots}, rows = {rows}") cols += 1 y_min, y_max = 0, 0 # Plot each IL and bridge deck side. for i, response_frac in enumerate(np.linspace(0, 1, rows * cols)): plt.subplot(rows, cols, i + 1) plot_bridge_deck_side(c.bridge, show=False, equal_axis=False) rs = plot_func(c, resp_matrix, i, response_frac, z_frac=z_frac, num_x=num_x) plt.axvline(x=c.bridge.x(x_frac=response_frac), color="red") # Keep track of min and max on y axis (only when non-zero fem). if any(rs): _y_min, _y_max = plt.gca().get_ylim() y_min, y_max = min(y_min, _y_min), max(y_max, _y_max) # Ensure y_min == -y_max. y_min = min(y_min, -y_max) y_max = max(-y_min, y_max) for i, _ in enumerate(np.linspace(0, 1, rows * cols)): plt.subplot(rows, cols, i + 1) plt.ylim(y_min, y_max) plt.tight_layout() if save: plt.savefig(save) plt.close()
colors = "rgbky" for si, sigma in enumerate([SIGMA1, SIGMA2, SIGMA3]): t0 = time() Q, R, A = multistrat(mu=MU, sigma=sigma, strategies=strats, epochs=5000) fig = plt.figure(figsize=(12, 5)) ax = fig.add_subplot(spec[0]) for i, s in enumerate(strats): plt.plot(R[i].mean(axis=0), label=s.__name__, alpha=0.5, c=colors[i]) plt.legend(fontsize=10, loc="lower right") plt.title("Average reward over {} runs".format(len(R[0]))) fig.add_subplot(spec[1], sharey=ax) bp = plt.boxplot(R.mean(axis=2).T, labels=[s.__name__ for s in strats]) for box, color in zip(bp['boxes'], colors): box.set_color(color) plt.xticks([]) plt.ylim(3) name = "Ex_2_sigma{}".format(si + 1) print name, time() - t0, "s" show(name)
def zoom_in(): plt.ylim(min_z, max_z) plt.xlim(min_x, max_x)