def plot_heatmap_at_iteration(self, iteration, **options): '''I line up the actual grid with interpolating grid so there is no interpolation, actually. Options: min_max_scale: [min, max] of the scale colorbar_decimals: number of decimals used in the colorbar ''' ax = plt.subplot() ternary.plot_heatmap(lambda x: self.interp_histogram_at_iteration(iteration, x), steps = self.num_individuals, boundary=True, **options) scale = self.num_individuals ternary.draw_boundary(scale=scale, ax=ax) plt.gca().set_aspect('equal', adjustable='box') plt.xlim([-.1*scale,1.1*scale]) plt.ylim([-0.13*scale, scale]) plt.text(1.01 * scale, -.05*scale, r'$f_1$', fontsize=35 ) plt.text(-.08 * scale, -.05*scale, r'$f_3$', fontsize=35 ) plt.text(.46*scale, .9*scale, r'$f_2$', fontsize=35) plt.gca().yaxis.set_visible(False) plt.gca().xaxis.set_visible(False) plt.grid(False) # Plot the fractional generation generation_formatted = '%.2f' % self.frac_gen[iteration] textbox = r'$N=' + str(self.num_individuals) + r'$' '\n' textbox += 'Num Simulations: ' + r'$10^' + str(int(np.log10(self.num_simulations))) + r'$' + '\n' textbox += 'Generation: ' + generation_formatted plt.text(.71*scale, .85*scale, textbox, fontsize=15, linespacing=1.75)
def plot_shell(steps=10, fill_color="black"): """Plot an empty heatmap shell for illustration.""" for i in range(steps + 1): for j in range(steps - i): vertices = ternary.triangle_coordinates(i, j, alt=False) x, y = ternary.unzip(vertices) pyplot.fill(x, y, facecolor=fill_color, edgecolor="black") for i in range(steps + 1): for j in range(steps - i - 1): vertices = ternary.triangle_coordinates(i, j, alt=True) x, y = ternary.unzip(vertices) pyplot.fill(x, y, facecolor="w", edgecolor="black") ternary.draw_boundary(scale=steps, linewidth=1)
def basic_example(): # Projection dynamic. initial_state = normalize(numpy.array([1,1,4])) m = rock_scissors_paper(a=1., b=-2.) fitness = linear_fitness(m) incentive = replicator_incentive_power(fitness, 0) mu = uniform_mutation_matrix(3, ep=0.2) t = compute_trajectory(initial_state, incentive, escort=power_escort(0), iterations=10000, verbose=True, mu=mu) ternary.plot(t, linewidth=2) ternary.draw_boundary() ## Lyapunov Quantities pyplot.figure() # Replicator Lyapunov e = normalize(numpy.array([1,1,1])) v = [kl_divergence(e, x) for x in t] pyplot.plot(range(len(t)), v, color='b') d = q_divergence(0) v = [d(e, x) for x in t] pyplot.plot(range(len(t)), v, color='r') pyplot.show()
def plot_heatmap_at_iteration(self, iteration, **options): '''I line up the actual grid with interpolating grid so there is no interpolation, actually. Options: min_max_scale: [min, max] of the scale colorbar_decimals: number of decimals used in the colorbar ''' ax = plt.subplot() ternary.plot_heatmap( lambda x: self.interp_histogram_at_iteration(iteration, x), steps=self.num_individuals, boundary=True, **options) scale = self.num_individuals ternary.draw_boundary(scale=scale, ax=ax) plt.gca().set_aspect('equal', adjustable='box') plt.xlim([-.1 * scale, 1.1 * scale]) plt.ylim([-0.13 * scale, scale]) plt.text(1.01 * scale, -.05 * scale, r'$f_1$', fontsize=35) plt.text(-.08 * scale, -.05 * scale, r'$f_3$', fontsize=35) plt.text(.46 * scale, .9 * scale, r'$f_2$', fontsize=35) plt.gca().yaxis.set_visible(False) plt.gca().xaxis.set_visible(False) plt.grid(False) # Plot the fractional generation generation_formatted = '%.2f' % self.frac_gen[iteration] textbox = r'$N=' + str(self.num_individuals) + r'$' '\n' textbox += 'Num Simulations: ' + r'$10^' + str( int(np.log10(self.num_simulations))) + r'$' + '\n' textbox += 'Generation: ' + generation_formatted plt.text(.71 * scale, .85 * scale, textbox, fontsize=15, linespacing=1.75)
lines = terntransform(a, b, c, d) step = 0.2 plt.figure() for line,color,label in zip(lines,colors,datalabels): tern.plot(line, color = color, linewidth=2.0, label = label) ip = baryIntersect(lines) tern.plotIntersect(ip) tern.gridlines(step, '0.7') tern.draw_boundary() tern.addlabels(endmembers) plt.legend() #pyplot.box(on='off') h = plt.gca() #plt.axis('off') xmin = -0.1 xmax = 1.1 ymin = -0.1 ymax = 1 v = [xmin, xmax, ymin, ymax] plt.axis(v) h.axes.get_xaxis().set_visible(False) h.axes.get_yaxis().set_visible(False)
def heatmap_example(func, steps=100, boundary=True): ternary.plot_heatmap(func, steps=steps, boundary=boundary) ternary.draw_boundary(scale=steps)
"""Plot an empty heatmap shell for illustration.""" for i in range(steps + 1): for j in range(steps - i): vertices = ternary.triangle_coordinates(i, j, alt=False) x, y = ternary.unzip(vertices) pyplot.fill(x, y, facecolor=fill_color, edgecolor="black") for i in range(steps + 1): for j in range(steps - i - 1): vertices = ternary.triangle_coordinates(i, j, alt=True) x, y = ternary.unzip(vertices) pyplot.fill(x, y, facecolor="w", edgecolor="black") ternary.draw_boundary(scale=steps, linewidth=1) if __name__ == "__main__": pyplot.figure() plot_shell(16) pyplot.figure() heatmap_example(shannon_entropy, steps=100, boundary=True) pyplot.figure() func = dirichlet([6, 10, 13]) heatmap_example(func, steps=100, boundary=False) pyplot.figure() points = [] with open("curve.txt") as handle: for line in handle: points.append(map(float, line.split(" "))) ternary.plot(points, linewidth=2.0) ternary.draw_boundary() pyplot.show()
"""Computes the Shannon Entropy at a distribution in the simplex.""" s = 0. for i in range(len(p)): try: s += p[i] * math.log(p[i]) except ValueError: continue return -1.*s if __name__ == '__main__': ## Boundary and Gridlines pyplot.figure() steps = 30 gs = gridspec.GridSpec(1,2) ax = pyplot.subplot(gs[0,0]) ax = ternary.draw_boundary(steps, color='black', ax=ax) ternary.draw_gridlines(steps, ax=ax, color='black') ax.set_title("Simplex Boundary and Gridlines") ## Various lines ax = pyplot.subplot(gs[0,1]) ternary.draw_boundary(steps, linewidth=2., color='black', ax=ax) ternary.draw_horizontal_line(ax, steps, 16) ternary.draw_left_parallel_line(ax, steps, 10, linewidth=2., color='red', linestyle="--") ternary.draw_right_parallel_line(ax, steps, 20, linewidth=3., color='blue') p1 = ternary.project_point((12,8,10)) p2 = ternary.project_point((2, 26, 2)) ternary.draw_line(ax, p1, p2, linewidth=3., marker='s', color='green', linestyle=":") ax.set_title("Various Lines") ## Heatmap roundup
colors = ['b', 'g', 'c', 'm', 'r', 'y'] lines = terntransform(*data) for line, color, label in zip(lines, colors, datalabels): tern.plot(line, color=color, linewidth=2.0, label=label if (ind == 0) else None) ip = baryIntersect(lines) tern.plotIntersect(ip, marker='o' + colors[ind], label=region[0]) step = 0.2 tern.gridlines(step, '0.7') tern.draw_boundary() tern.addlabels(endmembers) plt.legend() #pyplot.box(on='off') h = plt.gca() #plt.axis('off') xmin = -0.1 xmax = 1.1 ymin = -0.1 ymax = 1 v = [xmin, xmax, ymin, ymax] plt.axis(v) h.axes.get_xaxis().set_visible(False) h.axes.get_yaxis().set_visible(False)