def apply_to_graph(self, show_graph=True):
"""
Applies the given algorithm to given graph and displays it
:param show_graph: Weather to show the graph in final result or not
"""
# Draw the original matrix
if show_graph:
if self.pos is None:
self.pos = nx.nx_pydot.graphviz_layout(self.graph)
nx.draw_networkx_nodes(self.graph, self.pos, ax=self.ax1, node_color='g', alpha=0.8,
node_size=self.node_size).set_edgecolor('k')
nx.draw_networkx_edges(self.graph, self.pos, ax=self.ax1, alpha=0.5)
if self.weights:
nx.draw_networkx_edge_labels(self.graph, self.pos, ax=self.ax1,
edge_labels=nx.get_edge_attributes(self.graph, 'weight'))
if self.lables:
nx.draw_networkx_labels(self.graph, self.pos, ax=self.ax1)
# Draw its adjacancy matrix
result, adj = None, None
for i, matrix in enumerate(self.fn(self.graph)):
if i == 0:
adj = matrix[0]
result = matrix[0]
# print(adj, result)
cmap = plt.get_cmap('jet')
cmap.set_bad('white', 1.)
vmin = 0
vmax = np.max(result)
from mpl_toolkits.axes_grid1 import make_axes_locatable
div = make_axes_locatable(self.ax2)
cax = div.append_axes('right', '5%', '5%')
cax.axis('off')
masked_array = np.ma.array(adj, mask=np.isinf(adj))
self.ax2.imshow(masked_array, interpolation='nearest', cmap=cmap, vmin=vmin, vmax=vmax)
if self.matrix_labels:
self.__plot_matrix_labels(adj, self.ax2)
# Now draw the final matrix
masked_array = np.ma.array(result, mask=np.isinf(result))
div = make_axes_locatable(self.ax3)
cax = div.append_axes('right', '5%', '5%')
if self.matrix_labels:
self.__plot_matrix_labels(result, self.ax3)
self.img = self.ax3.imshow(masked_array, interpolation='nearest', cmap=cmap, vmin=vmin, vmax=vmax)
self.fig.colorbar(self.img, cax=cax)
plt.show()
评论列表
文章目录