def __plot_bunches(self, fig: plt.Figure, ax: plt.Axes, point: FiniteMetricVertex, name: str = "u") -> None: """ Plot all points and highlight the bunches for the given point on the provided figure/axes. :param fig: The matplotlib figure to plot on. :param ax: The matplotlib axes to plot on. :param point: The vertex whose bunches we wish to plot. :param name: The name to use to label the vertex/bunches. """ ax.cla() # Plot all points and color by set A_i ax.scatter([v.i for v in self.vertices], [v.j for v in self.vertices], s=4, color="black", marker=".", label="Points") # Plot and label the point itself ax.scatter([point.i], [point.j], s=12, color="red", marker="*", label=name) ax.annotate(name, (point.i, point.j), color="red") # Force the xlim and ylim to become fixed ax.set_xlim(*ax.get_xlim()) ax.set_ylim(*ax.get_ylim()) # For the current point, mark and label its p_i s # and add circles p_i = [self.p[point][i] for i in range(self.k)] for i in range(1, self.k): if p_i[i] is None: continue ax.annotate("p_{}({})".format(i, name), (p_i[i][0].i, p_i[i][0].j), xytext=(5, 5), textcoords="offset pixels", color="violet") circ = plt.Circle((point.i, point.j), p_i[i][1], fill=False) ax.add_patch(circ) # Plot the points in the bunch B = [w for w in self.B[point]] ax.scatter([w.i for w in B], [w.j for w in B], s=12, color="lime", marker="*", label="B({})".format(name)) ax.set_title("Bunch B({})".format(name)) ax.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left') plt.tight_layout() fig.show()
def plot_roc(roc: ROC, title: str = "", label: str = "", show=False, save=False, fig: plt.Figure = None): if fig is None: fig = plt.figure() ax = fig.add_axes([0.1, 0.1, 0.85, 0.8]) else: ax = fig.get_axes() assert len(ax) == 1 ax = ax[0] lw = 2 fpr, tpr, _ = roc ax.plot(fpr, tpr, lw=lw, label=label) ax.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--') ax.set(xlabel='FPR', ylabel='TPR', ylim=[0.0, 1.05], xlim=[0.0, 1.0]) ax.set_title(title, fontsize=15) ax.legend(loc="lower right") # fig.tight_layout() if show: fig.show() if save: fig.savefig(f'figs/{title}_{label}.png') return fig
def __plot_p_i(self, fig: plt.Figure, ax: plt.Axes, point: FiniteMetricVertex, name: str = "u") -> None: """ Plot all points and highlight the witnesses p_i for the given point along with corresponding rings on the given figure and axes. :param fig: The matplotlib figure to plot on. :param ax: The matplotlib axes to plot on. :param point: The vertex whose witnesses/rings we wish to plot. :param name: The name to use to label the vertex/bunches. """ ax.cla() # Plot all points and color by set A_i for i, a_i in enumerate(self.A): ax.scatter([v.i for v in a_i], [v.j for v in a_i], s=8, marker="o", label="A_{}".format(i)) # Plot and label the point itself ax.scatter([point.i], [point.j], s=12, color="red", marker="*", label=name) ax.annotate(name, (point.i, point.j), color="red") # Force the xlim and ylim to become fixed ax.set_xlim(*ax.get_xlim()) ax.set_ylim(*ax.get_ylim()) # For the current point, mark and label its p_i s # and add circles p_i = [self.p[point][i] for i in range(self.k)] for i in range(1, self.k): if p_i[i] is None: continue ax.annotate("p_{}({})".format(i, name), (p_i[i][0].i, p_i[i][0].j), xytext=(5, 5), textcoords="offset pixels", color="violet") circ = plt.Circle((point.i, point.j), p_i[i][1], fill=False) ax.add_patch(circ) ax.set_title("Witnesses p_i({}) and rings.".format(name)) ax.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left') plt.tight_layout() fig.show()
def show_figure(figure: plt.Figure) -> None: """Shows the figure, even if the figure was shown and therefore closed before""" try: figure.show() plt.show() except AttributeError: # the exception is raised iff the figure was closed. In this case we need a dummy figure ans its manager # https://stackoverflow.com/questions/49503869/attributeerror-while-trying-to-load-the-pickled-matplotlib-figure dummy = plt.figure() new_manager = dummy.canvas.manager new_manager.canvas.figure = figure figure.set_canvas(new_manager.canvas) figure.show() plt.show()
def __plot_A_i(self, fig: plt.Figure, ax: plt.Axes) -> None: """ Plot all points and highlight the sampled sets A_i. :param fig: The matplotlib figure to plot on. :param ax: The matplotlib axes to plot on. """ ax.cla() colors = get_cmap("Dark2").colors ax.set_prop_cycle(color=colors) for i, a_i in enumerate(self.A): ax.scatter([v.i for v in a_i], [v.j for v in a_i], s=8, marker="o", label="A_{}".format(i)) ax.set_title("Sampled sets A_i") ax.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left') plt.tight_layout() fig.show()
def fig2widget(fig: Figure, **kwargs) -> widgets.Widget: out = widgets.Output() with out: fig.show() return out
def __plot_query_state(self, fig: plt.Figure, ax: plt.Axes, u: GraphVertex, v: GraphVertex, w: GraphVertex, i: int, final: bool = False) -> None: """ Plot a single frame/plot that depicts the current state of the ADO query. Clears and overwrites any previous contents of the plot. :param ax: A Matplotlib Axes object representing the subplot being modified/displayed. :param u: The current value of u in the ADO query algorithm. :param v: The current value of v in the ADO query algorithm. :param w: The current value of w in the ADO query algorithm. :param i: The iteration of the ADO query algorithm. :param final: Whether or not this is the final query state/final iteration of the algorithm. """ ax.cla() p_i_u = defaultdict(list) for i_val, (p, _) in self.p[u].items(): p_i_u[p].append(i_val) # Color most nodes, black, but give special coloring to: # u, v, w, p_i(u)s and B(v)s. # Label u, v, w, p_i(u)s, and B(v)s node_colors = [] node_labels = {} for n in self.vertices: if n == u: node_colors.append("red") node_labels[n.nx_node] = "u" elif n == v: node_colors.append("lime") node_labels[n.nx_node] = "v" elif n == w: node_colors.append("orange") node_labels[n.nx_node] = "w" elif n in p_i_u: node_colors.append("violet") i_values_str = ",".join(str(i) for i in p_i_u[n]) node_labels[n.nx_node] = "p_{" + i_values_str + "}(u)" elif n in self.B[v]: node_colors.append("lime") node_labels[n.nx_node] = "B(v)" else: node_colors.append("black") # For the final plot, highlight the found path if final: path = self.__get_path(u, v, w) path_edges = set((path[i].nx_node, path[i + 1].nx_node) for i in range(len(path) - 1)) edge_colors = [ "lime" if (u, v) in path_edges or (v, u) in path_edges else "black" for u, v in self.g.edges() ] else: edge_colors = "black" pos = {(x, y): (y, -x) for x, y in self.g.nodes()} nx.draw(self.g, pos=pos, ax=ax, node_size=100, font_size=10, node_color=node_colors, edge_color=edge_colors, labels=node_labels) # Add a plot title title = "Iteration {} (final)".format(i) \ if final else "Iteration {}".format(i) ax.set_title(title) fig.show()
def __plot_query_state(self, fig: plt.Figure, ax: plt.Axes, u: FiniteMetricVertex, v: FiniteMetricVertex, w: FiniteMetricVertex, i: int, final: bool = False) -> None: """ Plot a single frame/plot that depicts the current state of the ADO query. Clears and overwrites any previous contents of the plot. :param fig: A Matplotlib figure object representing the figure being modified/displayed. :param ax: A Matplotlib Axes object representing the subplot being modified/displayed. :param u: The current value of u in the ADO query algorithm. :param v: The current value of v in the ADO query algorithm. :param w: The current value of w in the ADO query algorithm. :param i: The iteration of the ADO query algorithm. :param final: Whether or not this is the final query state/final iteration of the algorithm. """ ax.cla() # Plot all the points in the graph ax.scatter([v.i for v in self.vertices], [v.j for v in self.vertices], s=4, color="black", marker=".", label="Points") # Plot u, v, w with special symbols/colors ax.scatter([u.i], [u.j], s=12, color="red", marker="*", label="u") ax.annotate("u", (u.i, u.j), color="red") ax.scatter([v.i], [v.j], s=12, color="green", marker="*", label="v") ax.annotate("v", (v.i, v.j), color="green") ax.scatter([w.i], [w.j], s=5, color="orange", marker="p", label="w") ax.annotate("w", (w.i, w.j), color="orange", xytext=(-15, -10), textcoords="offset pixels") # For the current u, mark and label its p_i(u)s p_i_u = [self.p[u][i] for i in range(self.k)] ax.scatter([v[0].i for v in p_i_u], [v[0].j for v in p_i_u], s=4, color="violet", marker="o", label="p_i(u)") for j in range(1, self.k): ax.annotate("p_{}(u)".format(j), (p_i_u[j][0].i, p_i_u[j][0].j), xytext=(5, 5), textcoords="offset pixels", color="violet") # For the current v, highlight its batch B(v) in a different color B_v = [w for w in self.B[v]] ax.scatter([w.i for w in B_v], [w.j for w in B_v], s=4, color="lime", marker="*", label="B(v)") # Draw line from u to current w ax.add_line(Line2D([u.i, w.i], [u.j, w.j], color="pink")) # For the final plot, draw a line from w to current v as well if final: ax.add_line(Line2D([w.i, v.i], [w.j, v.j], color="palegreen")) title = "Iteration {} (final)".format(i) \ if final else "Iteration {}".format(i) ax.set_title(title) ax.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left') plt.tight_layout() fig.show()
def fig2widget(fig: Figure, **kwargs) -> widgets.Widget: out = widgets.Output() with out: fig.show() show_inline_matplotlib_plots() return out